-
Notifications
You must be signed in to change notification settings - Fork 52
fix: detect JobRunner subprocess death and stop memory-profiler ESRCH spam #526
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
Open
Alpaca233
wants to merge
5
commits into
Cephla-Lab:master
Choose a base branch
from
Alpaca233:fix/jobrunner-death-detection
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
232422d
fix: detect JobRunner subprocess death and stop memory-profiler ESRCH…
Alpaca233 3d8058d
style: apply Black formatting
Alpaca233 5267dd4
fix: address Copilot review — terminate() + early handler registration
Alpaca233 24751ec
refactor: simplify watchdog intent tracking and trim comments
Alpaca233 9ae07e3
test: add watchdog regression tests; close pre-warm adoption window
Alpaca233 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
software/tests/control/core/test_job_runner_watchdog.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| """Tests for JobRunner watchdog (unexpected subprocess death detection). | ||
|
|
||
| These tests cover the watchdog thread that distinguishes intentional shutdown | ||
| from unexpected subprocess death (segfault, SIGKILL, OOM kill) and invokes a | ||
| registered handler so an acquisition can abort instead of silently rotting. | ||
| """ | ||
|
|
||
| import os | ||
| import signal | ||
| import threading | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from control.core.job_processing import JobRunner | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def runner(): | ||
| """Provide an unstarted JobRunner; ensure cleanup even if the test crashes mid-run.""" | ||
| r = JobRunner() | ||
| r.daemon = True | ||
| yield r | ||
| if r.is_alive(): | ||
| try: | ||
| r.kill() | ||
| r.join(timeout=2.0) | ||
| except Exception: | ||
| pass | ||
|
|
||
|
|
||
| # Watchdog runs in a daemon thread; allow it to finish after the sentinel fires. | ||
| _WATCHDOG_GRACE_S = 0.3 | ||
|
|
||
|
|
||
| class TestWatchdogUnexpectedDeath: | ||
| """Verify the watchdog detects unexpected subprocess death and invokes the handler.""" | ||
|
|
||
| def test_sigkill_fires_handler_with_negative_exitcode(self, runner): | ||
| handler_fired = threading.Event() | ||
| received_exitcode = [] | ||
|
|
||
| def handler(exitcode): | ||
| received_exitcode.append(exitcode) | ||
| handler_fired.set() | ||
|
|
||
| runner.set_unexpected_exit_handler(handler) | ||
| runner.start() | ||
| assert runner.wait_ready(timeout_s=5.0) | ||
|
|
||
| os.kill(runner.pid, signal.SIGKILL) | ||
|
|
||
| assert handler_fired.wait(timeout=5.0), "Watchdog handler did not fire after SIGKILL" | ||
| assert received_exitcode == [-signal.SIGKILL] | ||
|
|
||
|
|
||
| class TestWatchdogIntentionalExit: | ||
| """Verify intentional stop paths (kill/terminate/shutdown) do NOT fire the handler.""" | ||
|
|
||
| def test_kill_does_not_fire_handler(self, runner): | ||
| handler_fired = threading.Event() | ||
| runner.set_unexpected_exit_handler(lambda ec: handler_fired.set()) | ||
| runner.start() | ||
| assert runner.wait_ready(timeout_s=5.0) | ||
|
|
||
| runner.kill() | ||
| runner.join(timeout=2.0) | ||
| time.sleep(_WATCHDOG_GRACE_S) | ||
|
|
||
| assert not handler_fired.is_set(), "Handler fired despite intentional kill()" | ||
|
|
||
| def test_terminate_does_not_fire_handler(self, runner): | ||
| handler_fired = threading.Event() | ||
| runner.set_unexpected_exit_handler(lambda ec: handler_fired.set()) | ||
| runner.start() | ||
| assert runner.wait_ready(timeout_s=5.0) | ||
|
|
||
| runner.terminate() | ||
| runner.join(timeout=2.0) | ||
| time.sleep(_WATCHDOG_GRACE_S) | ||
|
|
||
| assert not handler_fired.is_set(), "Handler fired despite intentional terminate()" | ||
|
|
||
| def test_shutdown_does_not_fire_handler(self, runner): | ||
| handler_fired = threading.Event() | ||
| runner.set_unexpected_exit_handler(lambda ec: handler_fired.set()) | ||
| runner.start() | ||
| assert runner.wait_ready(timeout_s=5.0) | ||
|
|
||
| runner.shutdown(timeout_s=2.0) | ||
| time.sleep(_WATCHDOG_GRACE_S) | ||
|
|
||
| assert not handler_fired.is_set(), "Handler fired despite intentional shutdown()" | ||
|
|
||
|
|
||
| class TestWatchdogResilience: | ||
| """Verify the watchdog is robust to handler misbehavior and shutdown ordering.""" | ||
|
|
||
| def test_handler_exception_does_not_propagate(self, runner): | ||
| # The watchdog daemon thread must catch handler exceptions (it logs them). | ||
| # If propagation happened, the test process would not reach the post-join asserts. | ||
| runner.set_unexpected_exit_handler(lambda ec: (_ for _ in ()).throw(RuntimeError("boom"))) | ||
| runner.start() | ||
| assert runner.wait_ready(timeout_s=5.0) | ||
|
|
||
| os.kill(runner.pid, signal.SIGKILL) | ||
| runner.join(timeout=5.0) | ||
| time.sleep(_WATCHDOG_GRACE_S) | ||
|
|
||
| assert not runner.is_alive() | ||
|
|
||
| def test_intentional_exit_survives_shutdown_cleanup(self, runner): | ||
| """Regression: shutdown() nulls _shutdown_event during cleanup. The intent flag | ||
| must be a separate attribute that survives that nullification, or the watchdog | ||
| could read None and misclassify intentional shutdown as unexpected death. | ||
| """ | ||
| handler_fired = threading.Event() | ||
| runner.set_unexpected_exit_handler(lambda ec: handler_fired.set()) | ||
| runner.start() | ||
| assert runner.wait_ready(timeout_s=5.0) | ||
|
|
||
| runner.shutdown(timeout_s=2.0) | ||
|
|
||
| assert runner._intentional_exit is True | ||
| assert runner._shutdown_event is None | ||
|
|
||
| time.sleep(_WATCHDOG_GRACE_S) | ||
| assert not handler_fired.is_set() | ||
|
|
||
|
|
||
| class TestPreWarmedAdoption: | ||
| """Document the load-bearing assumption behind the is_alive() check at adoption.""" | ||
|
|
||
| def test_is_ready_returns_true_for_dead_subprocess(self, runner): | ||
| """is_ready() reads a multiprocessing.Event the subprocess sets early in run(). | ||
| After SIGKILL the Event remains set in shared memory, so is_ready() alone cannot | ||
| distinguish a live runner from a corpse. is_alive() must also be checked before | ||
| adopting a pre-warmed runner. | ||
| """ | ||
| runner.start() | ||
| assert runner.wait_ready(timeout_s=5.0) | ||
|
|
||
| os.kill(runner.pid, signal.SIGKILL) | ||
| runner.join(timeout=5.0) | ||
|
|
||
| assert runner.is_ready() is True, "is_ready() should still report True even after death" | ||
| assert runner.is_alive() is False, "is_alive() should report False after death" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The watchdog treats an exit as "expected" only when
_shutdown_eventis set. In this codebaseJobRunner.terminate()is used during shutdown (e.g., MultiPointController.close), butterminate()does not set_shutdown_event, so the watchdog will log "died UNEXPECTEDLY" and invoke the handler during intentional termination. Consider overridingterminate()(and any other explicit-stop path you use) to set_shutdown_eventthe same way askill()/shutdown()before signaling the process.