Repository navigation
Conversation
The ping process is a daemon, which multiprocessing only terminates from the worker's atexit hooks. A hard-killed worker (SIGKILL, the OOM killer, an unhandled SIGTERM) never runs them, so the ping process kept pinging forever and reported a dead worker as alive. It now waits on the worker's sentinel between pings and exits as soon as the worker does. The test suite hit this on every run: a test that kills a worker subprocess left its ping process (and resource tracker) running. - rp_ping: wait on the parent sentinel instead of sleeping - tests: regression test that kills a worker mid-interval - tests: session guard that kills and fails on leaked processes
There was a problem hiding this comment.
🟡 Changes recommended
The heartbeat can keep pinging a dead worker when a surviving child retains the sentinel pipe, while test helpers mishandle zombies and fixed markers can cause cleanup to kill another test run’s processes.
4 open issues
What changed in this PR
This PR aims to stop serverless heartbeats when their worker dies and detect orphaned processes left by tests.
Changes:
- Wait on the worker’s process sentinel between heartbeats.
- Add a session-level guard that detects, kills, and reports orphaned test processes.
- Add worker-kill regression coverage and process-guard tests.
| File | Description |
|---|---|
| tests/test_serverless/test_modules/test_ping_start_method.py | Tests heartbeat exit after a worker is killed. |
| tests/test_process_guard.py | Tests orphan detection and cleanup. |
| tests/process_guard.py | Implements session-marked orphan detection and cleanup. |
| tests/conftest.py | Installs session marking and leak-reporting hooks. |
| runpod/serverless/modules/rp_ping.py | Adds worker-aware waiting between pings. |
Review effort: Balanced
Comment on lines
+115
to
+116
| ready = multiprocessing.connection.wait([worker.sentinel], self.PING_INTERVAL) | ||
| return not ready |
| leaked = leaked_processes(marker, root) | ||
| while leaked and time.monotonic() < deadline: | ||
| time.sleep(POLL_SECONDS) | ||
| leaked = [proc for proc in leaked if proc.is_running()] |
|
|
||
|
|
||
| def _start_orphan(marker, seconds=SLEEP_SECONDS): | ||
| env = {**os.environ, SESSION_MARKER_ENV: marker} |
Comment on lines
+180
to
+181
| if not psutil.pid_exists(pid): | ||
| return True |
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.

What
The worker's heartbeat (ping) process now exits as soon as the worker process dies, including when the worker is hard-killed. A session-level guard in the test suite fails the run if any process a test started is left running.
Why
The ping process is started as a daemon. multiprocessing only terminates daemon children from the parent's
atexithooks, and a hard-killed worker (SIGKILL, the OOM killer, or a SIGTERM with no handler) never runs them. The ping process then kept running its loop indefinitely and kept pinging, so a dead worker could still look alive to the platform. This applies whenever the worker is not PID 1 in its container (for example, started from a shell wrapper); when PID 1 exits, the kernel takes the rest of the container down with it.The test suite hit the same path on every run:
test_process_without_refresh_keeps_servingkills a real worker subprocess, which left its ping process and the multiprocessing resource tracker running after the suite ended. These accumulated on developer machines, one pair per run.How
Heartbeat.ping_loopwaits between pings on the worker's process sentinel (multiprocessing.parent_process().sentinelviamultiprocessing.connection.wait) instead oftime.sleep. When the worker exits for any reason the wait returns immediately and the loop ends. Called in-process (no parent process), it sleeps as before.tests/process_guard.pyandtests/conftest.py: the session sets a uniqueRUNPOD_TEST_SESSIONmarker in the environment, which every process a test starts inherits. At session end, any process carrying the marker that is no longer a descendant of pytest gets a short grace period, is then killed, and fails the run with its command line listed.Test plan
test_heartbeat_exits_when_worker_is_killed: starts a worker with a 10-minute ping interval, SIGKILLs it after the first ping, and asserts the ping process exits within 5 seconds. Fails before the change, passes after.tests/test_process_guard.py: reports an orphan from this session, ignores other sessions and live children, kills and reports survivors, allows orphans that exit within the grace period.Main's current CI failure (opentelemetry-api 1.46 deprecation, fixed separately) also affects this branch until that fix merges.