-
Notifications
You must be signed in to change notification settings - Fork 119
fix(serverless): force-kill worker on fitness check failure #536
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
deanq
wants to merge
3
commits into
main
Choose a base branch
from
deanq/sls-379-fitness-force-kill
base: main
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.
+146
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
94 changes: 94 additions & 0 deletions
94
tests/test_serverless/test_modules/test_fitness/test_force_kill.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,94 @@ | ||
| """ | ||
| Regression tests for SLS-379. | ||
|
|
||
| A failed fitness check must force-kill the worker even when non-daemon | ||
| background threads are alive (e.g. vLLM's AsyncLLMEngine, which is | ||
| constructed at import time before fitness checks run). ``sys.exit(1)`` only | ||
| raises ``SystemExit`` and then blocks in interpreter shutdown joining those | ||
| threads, so the worker logs "unhealthy, exiting." but never terminates. | ||
| The exit must go through ``os._exit`` to terminate unconditionally. | ||
| """ | ||
|
|
||
| import subprocess | ||
| import sys | ||
| from unittest.mock import patch | ||
|
|
||
| from runpod.serverless.modules import rp_fitness | ||
|
|
||
|
|
||
| # Child program mirrors worker.py: a non-daemon thread is alive (like vLLM's | ||
| # engine loop) when a failing fitness check triggers the unhealthy exit. | ||
| _CHILD_PROGRAM = """ | ||
| import asyncio | ||
| import os | ||
| import threading | ||
| import time | ||
|
|
||
| os.environ["RUNPOD_SKIP_AUTO_SYSTEM_CHECKS"] = "true" | ||
| os.environ["RUNPOD_SKIP_GPU_CHECK"] = "true" | ||
|
|
||
| from runpod.serverless.modules.rp_fitness import ( | ||
| register_fitness_check, | ||
| run_fitness_checks, | ||
| ) | ||
|
|
||
|
|
||
| def _never_returns(): | ||
| while True: | ||
| time.sleep(0.5) | ||
|
|
||
|
|
||
| @register_fitness_check | ||
| def _failing_check(): | ||
| raise RuntimeError("simulated fitness failure") | ||
|
|
||
|
|
||
| threading.Thread(target=_never_returns, daemon=False).start() | ||
| asyncio.run(run_fitness_checks()) | ||
| """ | ||
|
|
||
|
|
||
| def test_unhealthy_exit_terminates_with_live_non_daemon_thread(): | ||
| """Worker must die (exit 1) within the timeout, not hang, when a | ||
| non-daemon thread is alive at the time of the fitness failure.""" | ||
| try: | ||
| result = subprocess.run( | ||
| [sys.executable, "-c", _CHILD_PROGRAM], | ||
| capture_output=True, | ||
| text=True, | ||
| timeout=15, | ||
| ) | ||
| except subprocess.TimeoutExpired as exc: | ||
| raise AssertionError( | ||
| "Worker hung instead of exiting on fitness failure " | ||
| "(sys.exit could not join the live non-daemon thread)." | ||
| ) from exc | ||
|
|
||
| assert result.returncode == 1, ( | ||
| f"expected exit code 1, got {result.returncode}. " | ||
| f"stdout:\n{result.stdout}\nstderr:\n{result.stderr}" | ||
| ) | ||
| assert "Worker is unhealthy, exiting." in (result.stdout + result.stderr) | ||
|
|
||
|
|
||
| def test_terminate_unhealthy_uses_os_exit(): | ||
| """The unhealthy exit must call os._exit (unconditional) rather than | ||
| sys.exit (cooperative).""" | ||
| with patch("runpod.serverless.modules.rp_fitness.os._exit") as mock_exit: | ||
| rp_fitness._terminate_unhealthy(1) | ||
|
|
||
| mock_exit.assert_called_once_with(1) | ||
|
|
||
|
|
||
| def test_terminate_unhealthy_exits_even_if_flush_raises(): | ||
| """A closed/broken stdio stream must not stop the hard exit; a failing | ||
| flush is swallowed so os._exit always runs.""" | ||
| with patch("runpod.serverless.modules.rp_fitness.os._exit") as mock_exit, \ | ||
| patch("sys.stdout") as mock_stdout, \ | ||
| patch("sys.stderr") as mock_stderr: | ||
| mock_stdout.flush.side_effect = ValueError("I/O operation on closed file") | ||
| mock_stderr.flush.side_effect = ValueError("I/O operation on closed file") | ||
|
|
||
| rp_fitness._terminate_unhealthy(1) | ||
|
|
||
| mock_exit.assert_called_once_with(1) |
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.
Uh oh!
There was an error while loading. Please reload this page.