-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
fix: add SIGCHLD reaper to prevent zombie defunct processes (#6145) #6284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -16,6 +16,30 @@ | |||||||||
| if _SIGPIPE is not None: | ||||||||||
| signal.signal(_SIGPIPE, signal.SIG_IGN) | ||||||||||
|
|
||||||||||
|
|
||||||||||
| def _reap_children(_signo, _frame): | ||||||||||
| """Reap any exited child processes so they never linger as zombies. | ||||||||||
|
|
||||||||||
| Uses ``os.waitpid(-1, os.WNOHANG)`` in a loop to collect every already-exited | ||||||||||
| child in a single signal delivery. The handler is non-blocking and never | ||||||||||
| disturbs running subprocesses. On modern CPython ``subprocess.wait()`` | ||||||||||
| tolerates an already-reaped child via ``ChildProcessError`` and stays correct. | ||||||||||
|
|
||||||||||
| This is a global safety net for every asynchronous spawn in the WebUI process | ||||||||||
| — including gateway restarts, terminal runners, and plugin workers — so that | ||||||||||
| a lost daemon-thread waiter can never leak a zombie. | ||||||||||
| """ | ||||||||||
| try: | ||||||||||
| while True: | ||||||||||
| pid, _status = os.waitpid(-1, os.WNOHANG) | ||||||||||
| if pid == 0: | ||||||||||
| break | ||||||||||
| except ChildProcessError: | ||||||||||
| pass | ||||||||||
|
|
||||||||||
|
|
||||||||||
| signal.signal(signal.SIGCHLD, _reap_children) | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Comment on lines
+20
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Context Used: AGENTS.md (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||
|
|
||||||||||
| # Test-mode network isolation keeps subprocess-backed tests hermetic. | ||||||||||
| if os.environ.get("HERMES_WEBUI_TEST_NETWORK_BLOCK", "").strip() in ("1", "true", "yes"): | ||||||||||
| _REAL_CREATE_CONN = socket.create_connection | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subprocess.Popen.wait()and silently zerosreturncodeWhen
_reap_childrencallsos.waitpid(-1, os.WNOHANG)and wins the race against an in-flightproc.wait()(e.g. the daemon thread ingateway_restart.py), CPython'ssubprocess._try_waitcatchesChildProcessError(ECHILD) and returnssts = 0, causingproc.returncodeto be set to0regardless of the actual exit code. Therestart_active_profile_gatewaycaller then evaluatesproc.returncode == 0at line 133 and returns"status": "completed"— silently converting a failed restart into a reported success. The discarded_statuslocal in this handler is the lost signal.