Skip to content

[Core] Kill local processes only after the SIGTERM drain deadline has passed - #68457

Merged
rueian merged 1 commit into
ray-project:masterfrom
zhangsikai123:fix-sigterm-drain-deadline-68304
Nov 25, 2026
Merged

rueian merged 1 commit into
ray-project:masterfrom
zhangsikai123:fix-sigterm-drain-deadline-68304

Conversation

@zhangsikai123

Copy link
Copy Markdown
Contributor

Why are these changes needed?

Closes #68304.

On SIGTERM, ray start --block drains the local node with DRAIN_NODE_REASON_PREEMPTION and a deadline of now + RAY_GRACEFUL_SHUTDOWN_DRAIN_TIMEOUT_S (#64454). If the node is still busy when that time is up, _drain_node_before_shutdown returns and kill_all_processes() sends SIGTERM to the raylet right away.

The raylet records the node as preempted only if the drain deadline is strictly in the past when it receives SIGTERM (deadline_timestamp_ms < now, src/ray/raylet/main.cc). The poll loop ends on the monotonic clock at about the same instant as the wall-clock deadline, so the raylet often sees deadline == now and records EXPECTED_TERMINATION. Owners then count the failed tasks against max_retries (TaskManager only skips the count for AUTOSCALER_DRAIN_PREEMPTED), so #54182 does not take effect for busy nodes.

This PR keeps the raylet's rule as it is and fixes the caller: after the poll loop times out, it sleeps until wall-clock time is 10 ms past the deadline it sent, then kills local processes. The extra wait is capped at 1 s so that a backwards wall-clock jump cannot stall teardown. Nothing changes when the raylet exits on its own after draining, because in that case it takes the death reason from the drain request.

Related issue number

Closes #68304

Testing

Unit tests (python/ray/tests/test_draining.py):

  • _run_drain now fakes the wall clock in lockstep with the monotonic clock.
  • test_drain_timeout_kills_only_after_deadline (16 cases over timeout and poll interval) checks that a timed-out drain ends strictly after the deadline sent to the GCS, by at most the margin. Without the fix in node.py, all 16 cases fail.
  • test_drain_early_exit_does_not_wait_for_deadline checks that an early exit does not wait.
  • test_wait_until_past_drain_deadline covers the helper: before, at and after the deadline, and a backwards clock jump.
python -m pytest python/ray/tests/test_draining.py -k "drain_ or sigterm or wait_until"
35 passed, 10 deselected

End to end on Kubernetes (minikube, KubeRay v1.6.2, rayproject/ray:2.60.0, RAY_GRACEFUL_SHUTDOWN_DRAIN_TIMEOUT_S=3, grace period 10 s). A task pinned to each worker keeps the node busy, the worker pods are deleted, and the head reads each node's death reason from the GCS. Both runs mount node.py from a ConfigMap: the stock 2.60.0 file (same sha256 as the image) or the same file with this patch applied (the hunk applies to 2.60.0 unchanged).

Each run deletes 2 busy workers per round, for 15 rounds (30 nodes):

node.py AUTOSCALER_DRAIN_PREEMPTED EXPECTED_TERMINATION
stock 2.60.0 24 6
with this PR 30 0

With the stock file, 6 of 30 busy nodes are recorded as EXPECTED_TERMINATION ("received SIGTERM"). With the patch, all 30 are recorded as preempted.

Duplicate check

AI assistance

I used an AI coding assistant (Claude Code) to draft the change, the tests and this description. I reviewed every changed line and ran the tests and the Kubernetes check above myself.

Checks

  • I've signed off every commit (by using the -s flag, i.e., git commit -s) in this PR.
  • I've run black and ruff (pre-commit versions) on the changed files.
  • Unit tests
  • Release tests
  • This PR is not tested :(

🤖 Generated with Claude Code

@zhangsikai123
zhangsikai123 requested a review from a team as a code owner November 22, 2026 13:12

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a mechanism to ensure that a node's drain deadline has strictly passed in wall-clock time before shutting down the node. It adds a helper function _wait_until_past_drain_deadline in python/ray/_private/node.py that sleeps for a small margin (10ms) past the deadline, preventing race conditions where a node's termination is incorrectly recorded as EXPECTED_TERMINATION instead of preempted. Corresponding unit tests have been added and updated in python/ray/tests/test_draining.py to verify this behavior. There are no review comments to address, and the implementation looks solid.

… passed

When `ray start --block` drains on SIGTERM and the node is still busy at
the end of RAY_GRACEFUL_SHUTDOWN_DRAIN_TIMEOUT_S, it killed the raylet at
about the same millisecond as the drain deadline. The raylet only records
AUTOSCALER_DRAIN_PREEMPTED if the deadline is strictly in the past, so the
node was often recorded as EXPECTED_TERMINATION and task retries were
counted against max_retries.

Wait until wall-clock time is a small margin past the deadline before
killing local processes. The wait is capped so that a backwards clock jump
cannot stall teardown.

Closes ray-project#68304.

Signed-off-by: zhangsikai123 <[email protected]>
@zhangsikai123
zhangsikai123 force-pushed the fix-sigterm-drain-deadline-68304 branch from 4a7f298 to bc6c368 Compare November 22, 2026 13:21
@ray-gardener ray-gardener Bot added core Issues that should be addressed in Ray Core community-contribution Contributed by the community labels Nov 22, 2026

@rueian rueian left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. thanks @zhangsikai123

@rueian rueian added the go add ONLY when ready to merge, run all tests label Nov 25, 2026
@rueian
rueian enabled auto-merge (squash) November 25, 2026 18:21
@rueian
rueian merged commit 65cab5d into ray-project:master Nov 25, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Contributed by the community core Issues that should be addressed in Ray Core go add ONLY when ready to merge, run all tests

Projects

None yet

2 participants