Skip to content

fix(serverless): stop the heartbeat process when its worker dies - #807

Open
deanq wants to merge 1 commit into
mainfrom
deanq/fix-test-process-leak
Open

deanq wants to merge 1 commit into
mainfrom
deanq/fix-test-process-leak

Conversation

@deanq

@deanq deanq commented Dec 5, 2026

Copy link
Copy Markdown
Member

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 atexit hooks, 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_serving kills 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_loop waits between pings on the worker's process sentinel (multiprocessing.parent_process().sentinel via multiprocessing.connection.wait) instead of time.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.py and tests/conftest.py: the session sets a unique RUNPOD_TEST_SESSION marker 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

  • New 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.
  • Without the library change, running the refresh-worker subprocess tests leaves one orphaned pair and the guard fails the session; with it, nothing is left.
  • Full suite: 4854 passed, 8 skipped, coverage 95.6%; no leaked processes after the run. ruff and mypy clean.

Main's current CI failure (opentelemetry-api 1.46 deprecation, fixed separately) also affects this branch until that fix merges.

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
Copilot AI balanced review requested due to automatic review settings December 5, 2026 09:21
@deanq deanq self-assigned this Dec 5, 2026
@deanq
deanq requested a review from fser December 5, 2026 09:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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
Comment thread tests/process_guard.py
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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants