Repository navigation
gpu_lock -x: wait for orphaned GPU processes before starting an exclusive job - #99
Conversation
With flock -o (#90), killing a job's flock parent releases the lock while its children may still run on the GPU. At 07:35 on 2026-10-01 a stopped job's processes were still on the GPU when bench's exclusive holds started, and their sweeps refused to run. An exclusive job now waits (gpu_drain_wait.sh, up to GPU_LOCK_DRAIN_WAIT s, default 600) until no compute process is left, and exits 75 naming the processes if the GPU stays busy. The header now gives the 12 h default of GPU_LOCK_WAIT that the code uses.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Verdict: APPROVE (provisional: Codex is running on 36ff983, and I'll re-check it before merge) Reviewed head 36ff983, one commit on main 65a0a23.
Suggestions (not blocking):
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 36ff983a3e
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| deadline=$((SECONDS + limit)) | ||
| reported="" | ||
| while true; do | ||
| pids="$(nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null | tr -d ' ' | grep -v '^$' || true)" |
There was a problem hiding this comment.
Fail closed when the GPU process query fails
When nvidia-smi fails without producing stdout—for example because of a transient NVML/driver error—the trailing || true turns that failure into an empty pids value, so lines 17–18 report the GPU as drained and start the supposedly exclusive benchmark even if orphaned processes remain. Preserve and check the query status, and abort rather than treating an unsuccessful query as an idle GPU.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a5bda1c: a failed query now counts as busy until the deadline, then exits 75; a missing nvidia-smi still means nothing to drain. A test covers the failed query.
Addresses Codex's P1 on #99: a failed nvidia-smi query (e.g. a transient NVML error with no output) was read as an empty GPU. It now counts as busy until the deadline, then exits 75. A missing nvidia-smi (CPU-only host) still means nothing to drain.
|
@codex review |
|
Verdict: APPROVE (provisional until Codex runs on a5bda1c; I'll re-check before merge) Re-reviewed a5bda1c, which fixes Codex's P1 on 36ff983 and also covers my first suggestion.
My earlier points still hold: a shared holder can't be mistaken for an orphan under the exclusive flock, and the drain cannot deadlock the queue because it times out with exit 75. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a5bda1c142
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # lock means every holder has exited, so anything still running is an orphan of a killed job. | ||
| # shellcheck disable=SC2016 # $0 and $@ belong to the inner shell | ||
| flock -o -x -w "$WAIT" -E 75 "$LOCK_FILE" \ | ||
| bash -c '"$0" && exec "$@"' "$(dirname "${BASH_SOURCE[0]}")/gpu_drain_wait.sh" "$@" |
There was a problem hiding this comment.
Keep pre-CUDA children from outliving the exclusive lock
When the flock parent is killed while its child is still doing CPU-side startup, -o lets that child continue without the lock; the next exclusive job can acquire the lock, observe an empty compute-process list, and start before the orphan initializes CUDA. The orphan can then register on the GPU and overlap the supposedly exclusive run, so this one-time pre-command query does not close the exact killed-parent race the change is intended to address.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 359d70f: jobs run through scripts/gpu_job.sh under setpriv --pdeathsig TERM in their own process group, terminated when the flock dies or the command exits; each nvidia-smi query is bounded by GPU_LOCK_SMI_TIMEOUT (default 30 s) and a timeout counts as busy. Tests cover a killed lock holder, a leftover group member and a hung query.
| fi | ||
| while true; do | ||
| # A failed query is not an empty GPU: treat it as busy (fail closed) until the deadline. | ||
| if raw="$(nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null)"; then |
There was a problem hiding this comment.
Bound each nvidia-smi query by the drain deadline
When nvidia-smi hangs rather than returning an error, this command substitution never completes, so the later deadline check cannot run and the exclusive lock remains held indefinitely. The installed flock --help documents -w as the lock-wait timeout, so the outer option does not time the launched helper; wrap each query with the remaining GPU_LOCK_DRAIN_WAIT duration so the advertised upper bound is enforced.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 359d70f: jobs run through scripts/gpu_job.sh under setpriv --pdeathsig TERM in their own process group, terminated when the flock dies or the command exits; each nvidia-smi query is bounded by GPU_LOCK_SMI_TIMEOUT (default 30 s) and a timeout counts as busy. Tests cover a killed lock holder, a leftover group member and a hung query.
|
Verdict: REQUEST CHANGES (this withdraws my provisional approval at a5bda1c) Codex finished on a5bda1c with a P2 on B1. Bound each query, for example |
Addresses Codex's P1 and P2 on a5bda1c (#99). With flock -o, a child still starting up when its flock is killed could outlive the lock and reach the GPU after the next exclusive job's drain check. Jobs now run through gpu_job.sh under setpriv --pdeathsig TERM: the command gets its own process group, which is terminated when the flock dies and when the command exits. Each nvidia-smi query in the drain is bounded (GPU_LOCK_SMI_TIMEOUT, default 30 s) and a timeout counts as busy, so a hung query cannot hold the lock forever. Tests cover a killed lock holder, a leftover group member, and a hung query.
|
@codex review |
|
Verdict: APPROVE (provisional until Codex finishes on 359d70f; I'll re-check before merge) Re-reviewed 359d70f.
The behaviour change is intended and documented. Anything left in a job's own process group at exit is now terminated. I checked the current hold dispatchers: they queue successors with |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 359d70fddd
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| set -m # background jobs get their own process group, so the whole group can be signalled | ||
| "$@" & | ||
| pid=$! | ||
| stop_group() { kill -TERM -- "-$pid" 2>/dev/null || true; } |
There was a problem hiding this comment.
Track detached GPU descendants outside the job group
Fresh evidence beyond the earlier resolution is that production lock callers actually detach GPU servers: bench/server.py:516-521, experiments/drafter/serve_run.py:166-167, and others use start_new_session=True. Those servers are outside -$pid, so if flock is killed while the runner is starting, this signal kills only the runner's group; a detached server that has not reached CUDA yet survives, the next exclusive drain can observe an empty GPU, and both jobs can subsequently overlap. The wrapper needs supervision that also contains detached descendants rather than relying solely on the initial process group.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in c826e3a: TERM escalates to KILL after GPU_JOB_KILL_GRACE (default 10 s); the exclusive drain also waits for any SGLang server process (detached or pre-CUDA), which under the exclusive lock can only be an orphan; each nvidia-smi query is clamped to the remaining drain time. Tests cover a detached server and a TERM-ignoring job.
| trap 'stop_group; wait "$pid" 2>/dev/null; exit 143' TERM INT HUP | ||
| wait "$pid" | ||
| rc=$? | ||
| stop_group # leftovers in the job's group do not outlive the job | ||
| exit "$rc" |
There was a problem hiding this comment.
Ensure TERM-resistant processes stop before the next job
When the command or one of its descendants catches or ignores SIGTERM, stop_group does not ensure it has exited: after a holder death the trap can wait indefinitely for the leader even though flock has already released the lock, while after normal command completion the wrapper sends TERM to remaining descendants and exits immediately without waiting. In either case a surviving process can overlap the next supposedly exclusive job; this is reproducible with a command that runs trap '' TERM; exec sleep 30. Escalate and verify group termination rather than treating a best-effort TERM as cleanup.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in c826e3a: TERM escalates to KILL after GPU_JOB_KILL_GRACE (default 10 s); the exclusive drain also waits for any SGLang server process (detached or pre-CUDA), which under the exclusive lock can only be an orphan; each nvidia-smi query is clamped to the remaining drain time. Tests cover a detached server and a TERM-ignoring job.
| while true; do | ||
| # A failed or hung query (GPU_LOCK_SMI_TIMEOUT, default 30 s) is not an empty GPU: treat it as busy (fail | ||
| # closed) until the deadline. | ||
| if raw="$(timeout "${GPU_LOCK_SMI_TIMEOUT:-30}" nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null)"; then |
There was a problem hiding this comment.
Clamp each query to the remaining drain deadline
Fresh evidence after the earlier timeout fix is that this still passes an independent 30-second query timeout even when less time remains in GPU_LOCK_DRAIN_WAIT; the local timeout --help confirms that its duration bounds the command, not the surrounding drain loop. For example, with GPU_LOCK_DRAIN_WAIT=1 and a fake nvidia-smi that sleeps 3 seconds before failing, this script exits after 3 seconds rather than the advertised 1, and the unconditional 5-second sleep can add another overrun. Use the remaining drain duration for both the query and sleep so the configured deadline is actually enforced.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in c826e3a: TERM escalates to KILL after GPU_JOB_KILL_GRACE (default 10 s); the exclusive drain also waits for any SGLang server process (detached or pre-CUDA), which under the exclusive lock can only be an orphan; each nvidia-smi query is clamped to the remaining drain time. Tests cover a detached server and a TERM-ignoring job.
… clamp queries Addresses Codex's P1/P1/P2 on 359d70f (#99). gpu_job.sh now escalates TERM to KILL after GPU_JOB_KILL_GRACE s (default 10) on holder death and at job end. The exclusive drain also waits for any SGLang server process on the host (GPU_LOCK_ORPHAN_PATTERN), since under the exclusive lock any such process is an orphan, including one started with start_new_session that has not reached CUDA. Each nvidia-smi query is clamped to the remaining drain time. Tests cover a detached server and a TERM-ignoring job.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c826e3ac37
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| pids="$(printf '%s\n' "$raw" | tr -d ' ' | grep -v '^$' || true)" | ||
| # Under the exclusive lock any SGLang server is an orphan, even one that detached from | ||
| # its job's process group and has not reached CUDA yet. | ||
| servers="$(pgrep -f -- "${GPU_LOCK_ORPHAN_PATTERN:-sglang[.]launch_server|sglang::}" 2>/dev/null | tr '\n' ' ' || true)" |
There was a problem hiding this comment.
Exclude ancestor shells from orphan matching
When the lock is invoked through a wrapper such as bash -c 'scripts/gpu_lock.sh -x python -m sglang.launch_server ...', this full-command-line search matches the ancestor shell itself, so the drain waits for its own caller and eventually exits 75 even though no server exists. The installed pgrep --help documents -f as matching full command lines and -A as excluding ancestors; exclude ancestors or identify actual server processes more precisely.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 564c42e: the orphan search skips the drain's own ancestor chain; timeout --kill-after=5 bounds a query that ignores TERM; the retry sleep is capped at the remaining drain time. Tests cover a caller whose command line matches the pattern and a 1 s bound.
| [ "$remaining" -ge 1 ] || remaining=1 | ||
| smi_limit="${GPU_LOCK_SMI_TIMEOUT:-30}" | ||
| [ "$smi_limit" -le "$remaining" ] || smi_limit="$remaining" | ||
| if raw="$(timeout "$smi_limit" nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null)"; then |
There was a problem hiding this comment.
Escalate timed-out GPU queries to KILL
If a hung nvidia-smi blocks or ignores SIGTERM, this command substitution still never returns because plain timeout sends only TERM and continues waiting. The installed timeout --help explicitly says TERM may be caught or blocked and that --kill-after sends KILL; use an escalation deadline so GPU_LOCK_SMI_TIMEOUT actually bounds each query.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 564c42e: the orphan search skips the drain's own ancestor chain; timeout --kill-after=5 bounds a query that ignores TERM; the retry sleep is capped at the remaining drain time. Tests cover a caller whose command line matches the pattern and a 1 s bound.
| echo "gpu_drain_wait: GPU still busy after ${limit}s (pids: $(echo "$pids" | tr '\n' ' ')); not starting" >&2 | ||
| exit 75 | ||
| fi | ||
| sleep 5 |
There was a problem hiding this comment.
Cap the retry sleep at the remaining drain time
Fresh evidence after the claimed c826e3a resolution is that this sleep remains an unconditional five seconds: with GPU_LOCK_DRAIN_WAIT=1 and a responsive fake nvidia-smi that reports one PID, the script exits after about five seconds rather than one. Sleep for at most the remaining deadline so the configured drain bound is enforced.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 564c42e: the orphan search skips the drain's own ancestor chain; timeout --kill-after=5 bounds a query that ignores TERM; the retry sleep is capped at the remaining drain time. Tests cover a caller whose command line matches the pattern and a 1 s bound.
… bounds Addresses Codex's three P2s on c826e3a (#99). The orphan search skips the drain's own ancestor chain, whose command lines may name an SGLang server (gpu_lock.sh -x python -m sglang.launch_server ...); timeout --kill-after bounds a query that ignores TERM; the retry sleep is capped at the remaining drain time. Tests cover a caller whose command line matches the pattern and a 1 s drain bound.
|
@codex review |
|
Verdict: REQUEST CHANGES Re-reviewed 564c42e (c826e3a and 564c42e). The fixes for Codex's three items on 359d70f are right:
BlockingB1. The orphan pattern matches processes that are not SGLang servers. The same goes for anything whose command line mentions those strings:
Under the exclusive lock any of these makes the drain wait the full Match server processes structurally instead:
Add a test with a decoy process whose command line contains the pattern text (e.g. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 564c42e748
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| flock -o -x -w "$WAIT" -E 75 "$LOCK_FILE" \ | ||
| setpriv --pdeathsig TERM -- "$HERE/gpu_job.sh" -x "$@" |
There was a problem hiding this comment.
Reset inherited SIGTERM before setting the parent-death signal
When gpu_lock.sh is launched by a parent that ignores SIGTERM (for example, trap '' TERM; exec scripts/gpu_lock.sh ...), that disposition is inherited through flock and setpriv, and Bash cannot install the cleanup trap for a signal ignored on shell entry. Fresh evidence beyond the earlier TERM-resistance resolution is that killing the flock holder in this context leaves both gpu_job.sh and its command running after the lock is released, allowing the next exclusive job to overlap; reset SIGTERM to its default disposition before executing the wrapper.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5c327a7: env --default-signal=TERM,INT,HUP resets a caller's ignored TERM before gpu_job.sh; the cleanup trap is installed before the command starts; the query's KILL grace (1 s) is inside the drain deadline. Tests cover an ignored TERM in the caller.
| done | ||
| kill -KILL -- "-$pid" 2>/dev/null || true | ||
| } | ||
| trap 'stop_group; exit 143' TERM INT HUP |
There was a problem hiding this comment.
Install cleanup traps before starting the command
If the flock holder dies after "$@" & starts the command but before this trap is installed, the parent-death SIGTERM takes its default action and terminates only gpu_job.sh; the newly created command group survives without the lock. Fresh evidence beyond the prior killed-holder resolution is this remaining launch-to-trap window, so the signals need to be handled or blocked before the child can start.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5c327a7: env --default-signal=TERM,INT,HUP resets a caller's ignored TERM before gpu_job.sh; the cleanup trap is installed before the command starts; the query's KILL grace (1 s) is inside the drain deadline. Tests cover an ignored TERM in the caller.
| smi_limit="${GPU_LOCK_SMI_TIMEOUT:-30}" | ||
| [ "$smi_limit" -le "$remaining" ] || smi_limit="$remaining" | ||
| # --kill-after: a query that ignores TERM is killed, so the limit really bounds it. | ||
| if raw="$(timeout --kill-after=5 "$smi_limit" nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null)"; then |
There was a problem hiding this comment.
Include the KILL grace period in the drain deadline
When nvidia-smi ignores SIGTERM and less than five seconds remain, --kill-after=5 can extend the query five seconds beyond GPU_LOCK_DRAIN_WAIT; the installed timeout --help confirms that this duration begins after the initial signal. Fresh evidence beyond the earlier timeout fixes is that GPU_LOCK_DRAIN_WAIT=1 GPU_LOCK_SMI_TIMEOUT=1 with a TERM-ignoring fake query exits after about six seconds, not one, so the escalation grace must also be clamped to the remaining drain time.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5c327a7: env --default-signal=TERM,INT,HUP resets a caller's ignored TERM before gpu_job.sh; the cleanup trap is installed before the command starts; the query's KILL grace (1 s) is inside the drain deadline. Tests cover an ignored TERM in the caller.
…start Addresses the reviewer's change request and Codex's P1/P2/P2 on 564c42e (#99). Orphan SGLang servers are matched structurally (comm prefix sglang::, or a python process whose argv holds '-m sglang.launch_server'), so shells, monitors and waiting lock clients that only mention the name do not block the drain. env --default-signal resets a caller's ignored TERM before gpu_job.sh, so the parent-death TERM is not lost. gpu_job.sh installs its cleanup trap before starting the command. The query timeout includes its KILL grace within the drain deadline. Decoy and ignored-TERM tests added.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5c327a7860
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| [ "$smi_limit" -le "$((remaining - 1))" ] || smi_limit="$((remaining - 1))" | ||
| [ "$smi_limit" -ge 1 ] || smi_limit=1 | ||
| # --kill-after: a query that ignores TERM is killed 1 s later, so the limit really bounds it. | ||
| if raw="$(timeout --kill-after=1 "$smi_limit" nvidia-smi --query-compute-apps=pid --format=csv,noheader 2>/dev/null)"; then |
There was a problem hiding this comment.
Keep the KILL grace within one-second deadlines
Fresh evidence in the current revision is that after reserving one second for the KILL grace, the next line clamps smi_limit back to one second. The installed timeout --help states that --kill-after sends KILL that long after the initial signal, so with GPU_LOCK_DRAIN_WAIT=1 and a fake nvidia-smi that ignores TERM, the query takes about two seconds and violates the advertised drain bound. Use a hard-KILL timeout or otherwise include both phases within the remaining second.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in fe410ad: with under 2 s left the drain counts the GPU as busy instead of starting a query that could overrun the bound; the tests check liveness via /proc//stat and treat zombies as gone.
| deadline = time.time() + 5 | ||
| while time.time() < deadline: | ||
| try: | ||
| os.kill(left, 0) | ||
| except ProcessLookupError: |
There was a problem hiding this comment.
Treat zombie children as stopped in cleanup tests
On environments whose PID 1 does not promptly reap adopted children, a process killed by gpu_job.sh remains a zombie and os.kill(pid, 0) continues to succeed, so this test reports that an already-terminated process outlived the job. Running pytest -q tests/test_gpu_lock.py in the provided environment fails here and at the equivalent check on lines 280–287; inspect the process state and treat Z as terminated instead of requiring immediate PID disappearance.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in fe410ad: with under 2 s left the drain counts the GPU as busy instead of starting a query that could overrun the bound; the tests check liveness via /proc//stat and treat zombies as gone.
|
Verdict: REQUEST CHANGES (two small Codex items left) Re-reviewed 5c327a7. My B1 is fixed.
Codex finished on 5c327a7 with two P2s. Both are valid and quick to fix:
|
Addresses Codex's two P2s on 5c327a7 (#99). With under 2 s of drain time left there is no room for a query plus its 1 s KILL grace, so the GPU is counted as busy instead of overrunning the bound. The tests judge liveness from /proc/<pid>/stat and count a zombie as gone, so slow reaping cannot flake them; tests that need a GPU query get enough drain time for one.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fe410ad296
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| comm="$(cat "$d/comm" 2>/dev/null)" || continue | ||
| if [ "${comm#"$comm_prefix"}" != "$comm" ]; then | ||
| printf '%s ' "$pid" | ||
| continue |
There was a problem hiding this comment.
Skip zombie SGLang processes during drain
When a detached SGLang worker exits after its parent is killed and PID 1 does not reap adopted children, /proc/<pid>/comm remains available with the sglang:: prefix even though the process is a zombie and can no longer use the GPU. This branch consequently reports that PID forever, causing every exclusive job to wait the default 600 seconds and exit 75. Check /proc/<pid>/stat and ignore Z entries, as the new test helper already does for terminated children.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5c8e927: orphan_servers skips processes in state Z (from /proc//stat); a zombie-decoy test covers it.
|
Verdict: APPROVE (provisional until Codex runs on fe410ad; I'll re-check before merge) Re-reviewed fe410ad, which fixes Codex's two P2s on 5c327a7.
My earlier checks still hold: the structural orphan match, TERM escalating to KILL, the pdeathsig teardown, the inherited-signal reset, and exit-status and stdin propagation. |
|
Verdict: REQUEST CHANGES (this withdraws my provisional approval at fe410ad) Codex finished on fe410ad with a P1 on B1. Skip state |
Addresses Codex's P1 on fe410ad (#99). A dead SGLang worker that PID 1 has not yet reaped keeps its sglang:: comm but cannot use the GPU; counting it made every exclusive job wait out the drain and exit 75. orphan_servers now skips processes in state Z (from /proc/<pid>/stat). A zombie-decoy test is added.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5c8e927425
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| exit 64 | ||
| fi | ||
|
|
||
| set -m # background jobs get their own process group, so the whole group can be signalled |
There was a problem hiding this comment.
Keep terminal-reading commands in the foreground
When gpu_lock.sh is invoked from a terminal and the wrapped command reads from that terminal, set -m combined with launching "$@" & places the command in a background process group while gpu_job.sh retains the foreground group. The kernel therefore stops the command with SIGTTIN; for example, an interactive read under gpu_lock.sh -x exits through this wrapper with status 149 instead of accepting input. This regresses the prior foreground execution, so the wrapper should transfer terminal foreground ownership or supervise the command without backgrounding it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a177999. The command still runs in its own process group so the whole group can be stopped, but when stdin is a terminal it now reads /dev/null: a read sees end of input instead of stopping the job with SIGTTIN. The header says the job cannot read from a terminal. test_a_job_started_from_a_terminal_reads_end_of_input_instead_of_stopping runs the lock under a pty (exit 149 before the fix, 0 after).
| [ -n "$pid" ] || return 0 | ||
| kill -TERM -- "-$pid" 2>/dev/null || return 0 | ||
| local t=0 | ||
| while kill -0 -- "-$pid" 2>/dev/null && [ "$t" -lt "${GPU_JOB_KILL_GRACE:-10}" ]; do |
There was a problem hiding this comment.
Stop waiting when the job group contains only zombies
On hosts whose PID 1 does not promptly reap adopted children, a same-group descendant that exits after the TERM remains a zombie and kill -0 -- -$pid continues succeeding. Consequently, a command that leaves a background child, such as bash -c 'sleep 60 &', holds the lock for the full default 10-second grace even though cleanup has already terminated every live process; the repository's new alive() helper explicitly handles this environment, but the production loop does not. Inspect member states and stop waiting once the group is zombie-only.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a177999. stop_group now waits on group_running, which reads /proc//stat and counts only group members that are not zombies, so cleanup stops as soon as every member has exited. test_cleanup_does_not_wait_for_a_group_of_zombies leaves an unreaped child in the group (about 8 s before the fix, under 2 s after).
|
Verdict: APPROVE Re-reviewed 5c8e927, which fixes Codex's zombie P1 on fe410ad. Everything from my earlier reviews of this PR still holds: the structural orphan match, the deadline-bounded queries with no query in the last 2 s, TERM escalating to KILL, pdeathsig teardown with signals reset, and exit-status and stdin propagation. |
…dev/null Cleanup waited out the whole kill grace while the job's group held only exited, unreaped members, since kill -0 succeeds on a zombie. It now reads /proc and stops once no member of the group is still running. The command runs in a background process group, so a read from a controlling terminal stopped it with SIGTTIN (exit 149). When stdin is a terminal, the command now reads /dev/null and sees end of input.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a177999e7d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if [ "$prev" = "-m" ] && [ "$tok" = "$module" ]; then found=1; break; fi | ||
| prev="$tok" | ||
| done < "$d/cmdline" 2>/dev/null | ||
| case "$argv0" in python*) [ "$found" = 1 ] && printf '%s ' "$pid" ;; esac |
There was a problem hiding this comment.
Track detached profiler launchers before CUDA starts
Fresh evidence beyond the earlier detached-server resolution is that the production exclusive workflow in experiments/profiling/run_profiles.py:301-303 launches nsys ... python -m sglang.launch_server with start_new_session=True. Before nsys spawns the Python server, this python* restriction does not recognize the detached launcher; if the lock holder is killed in that interval, the next exclusive drain can observe neither a compute PID nor a matching orphan, start another job, and then overlap when the surviving nsys launches the server. Track this detached launcher or otherwise contain all detached descendants, not only an already-started Python server.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in f926ad4. The drain now also counts a profiler launcher named in GPU_LOCK_ORPHAN_LAUNCHERS (default "nsys ncu") whose argv holds a python interpreter followed by "-m ", so a detached nsys ... python -m sglang.launch_server counts before it spawns the server. Waiting lock clients still do not match, because their argv0 is bash or flock. test_exclusive_waits_for_a_detached_profiler_launcher covers it, and the decoy test adds an nsys-named process whose "-m " does not follow a python token.
| flock -o -x -w "$WAIT" -E 75 "$LOCK_FILE" \ | ||
| setpriv --pdeathsig TERM -- env --default-signal=TERM,INT,HUP "$HERE/gpu_job.sh" -x "$@" |
There was a problem hiding this comment.
Close the parent-death signal setup race
Fresh evidence beyond the earlier launch-to-trap fix is that the holder can die after flock forks this child but before setpriv installs the signal. The installed setpriv --help describes --pdeathsig as setting the parent-death signal, so it cannot retroactively notify a child whose parent already exited; that orphan proceeds through gpu_job.sh and starts the command after the lock has been released. Pass the expected holder PID and verify the parent after installing the signal, aborting when it has already changed.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in f926ad4. gpu_job.sh runs after setpriv has installed the parent-death signal, so it now checks that its parent is still the flock that holds the lock (/proc/$PPID/comm is flock) and exits 75 otherwise. A holder that died before setpriv's prctl has already reparented the job by the time this check runs; one that dies after it sends TERM. test_the_job_does_not_run_once_the_holder_is_gone runs gpu_job.sh under a parent that is not flock and checks the command never runs.
…r is gone A profiler launcher (nsys, ncu) started with start_new_session has not spawned its SGLang server yet, so neither the GPU query nor the server match saw it. The drain now also counts a launcher named in GPU_LOCK_ORPHAN_LAUNCHERS whose argv holds "python... -m <module>". setpriv installs the parent-death signal only after flock has forked the job, so a holder that died in between sent no signal. gpu_job.sh now checks that its parent is still flock and exits 75 otherwise.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f926ad4767
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if [ -t 0 ]; then "$@" </dev/null & else "$@" & fi | ||
| pid=$! |
There was a problem hiding this comment.
Capture the child PID before signal cleanup runs
If the flock holder dies after the asynchronous command is created but before the following pid=$! assignment, Bash can dispatch the pending TERM trap between these commands; stop_group then sees an empty pid, exits without signaling the new process group, and the command survives after the lock is released. Fresh evidence beyond the earlier trap-order fix is that installing the trap before launch still leaves this launch-to-PID-capture window; block/defer these signals during both operations or let the trap recover the current $! when pid is empty.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in a0c82e7. The trap handler now records a pending stop when pid is still empty, and the wrapper stops the group and exits 143 right after pid=$! if one is pending. test_a_holder_death_before_the_job_pid_is_known_still_stops_the_job kills the flock holder while GPU_JOB_TEST_SPAWN_DELAY holds the wrapper between the fork and pid=$!; the job outlives the holder before the fix and stops after it.
|
Verdict: REQUEST CHANGES (one Codex P2 left) Re-reviewed f926ad4 (a177999 and f926ad4). I checked every Codex thread on these heads with pagination; my earlier miss on 5c8e927 came from an unpaginated query. Fixed:
Blocking, Codex P2 on f926ad4 ( |
A holder death between the fork and pid=$! ran the trap with pid empty, so stop_group returned without signalling the new process group and the job outlived the lock. The trap now records the signal when pid is not set yet, and the wrapper stops the group as soon as pid is known. GPU_JOB_TEST_SPAWN_DELAY widens that window for the new test.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a0c82e7bff
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| while group_running && [ "$t" -lt "${GPU_JOB_KILL_GRACE:-10}" ]; do | ||
| sleep 1 | ||
| t=$((t + 1)) | ||
| done | ||
| kill -KILL -- "-$pid" 2>/dev/null || true |
There was a problem hiding this comment.
Delay successors until the old job group is dead
When the flock holder is killed and the wrapped group ignores TERM, this loop leaves that group alive for GPU_JOB_KILL_GRACE after flock has already released the lock. A second -x job can therefore acquire the lock immediately, and because gpu_drain_wait.sh only recognizes current compute processes and specific SGLang launchers, a generic pre-CUDA command is invisible and can initialize CUDA while the successor is running. Fresh evidence beyond the c826e3a resolution is that, on the current tree, killing the holder of trap '' TERM; sleep 2; <continue> allowed a successor to start roughly two seconds before the old command continued; escalation alone does not preserve exclusivity unless successors also wait for this cleanup to finish.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 09ec5a4. gpu_job.sh records each job in a registry next to the lock ($GPU_LOCK_FILE.jobs/: wrapper start time, job pgid and the pgid leader's start time) before the fork, and removes the entry only after stop_group has stopped the group. The exclusive drain now also waits while a recorded wrapper is alive or a recorded group has a non-zombie member, and removes entries whose wrapper and group are both gone (start times guard against pid reuse). This covers a wrapper still in its kill grace after flock died, and a wrapper killed together with flock. New tests: test_next_exclusive_job_waits_while_the_old_job_is_still_being_stopped (TERM-ignoring job, flock SIGKILLed), test_next_exclusive_job_waits_for_a_job_whose_wrapper_was_killed, and test_a_stale_registry_entry_does_not_block_and_is_removed; all three fail on a0c82e7. A signal recorded before the fork now also stops the job before it starts.
|
Verdict: REQUEST CHANGES Re-review on the exact head a0c82e7 ("gpu_job: act on a signal that arrives before the job's pid is known"). The fix in a0c82e7 is correct.
Blocking
Suggestions (non-blocking)
Reproduction (run from the repo root on a0c82e7; it uses the test fixtures): import os, subprocess, sys, tempfile, time
from pathlib import Path
sys.path.insert(0, str(Path.cwd() / 'tests'))
from test_gpu_lock import SCRIPT, fake_smi
t = Path(tempfile.mkdtemp())
lock = t / 'gpu.lock'; lock.touch()
env = dict(fake_smi(t), GPU_LOCK_FILE=str(lock), GPU_LOCK_DRAIN_WAIT='5', GPU_LOCK_WAIT='60')
a_job = f"trap '' TERM; echo $$ > {t}/a.pid; sleep 3; date +%s.%N > {t}/a_continued"
a = subprocess.Popen(['bash', str(SCRIPT), '-x', 'bash', '-c', a_job], env=env)
while not (t / 'a.pid').exists(): time.sleep(0.05)
holder = subprocess.run(['pgrep', '-P', str(a.pid), '-x', 'flock'], capture_output=True, text=True).stdout.split()
os.kill(int(holder[0]), 9); killed = time.time()
subprocess.run(['bash', str(SCRIPT), '-x', 'bash', '-c', f'date +%s.%N > {t}/b_started'], env=env, timeout=60)
time.sleep(5)
bs = float((t / 'b_started').read_text()); ac = float((t / 'a_continued').read_text())
print(f'B started at {bs - killed:.2f} s; A continued at {ac - killed:.2f} s;', 'OVERLAP' if bs < ac else 'no overlap')
a.kill()Output on a0c82e7: |
When a holder died, flock released the lock at once, but the wrapper could still be inside its kill grace with a job that ignores TERM; the drain could not see a job that had not reached CUDA, so the next exclusive job overlapped it. gpu_job.sh now records each job in a registry next to the lock (wrapper pid and start time, job pgid and leader start time) and removes the entry only after stopping the group. The exclusive drain waits while a recorded wrapper or a non-zombie member of a recorded group is still running, which also covers a wrapper killed along with flock, and removes entries whose wrapper and group are gone. A signal recorded before the fork now stops the job from starting.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 09ec5a4a44
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| # Tests widen the window between the fork and pid=$! to deliver a signal inside it. | ||
| if [ -n "${GPU_JOB_TEST_SPAWN_DELAY:-}" ]; then sleep "$GPU_JOB_TEST_SPAWN_DELAY"; fi | ||
| pid=$! | ||
| echo "$(start_time $$) $pid $(start_time "$pid")" >"$entry" |
There was a problem hiding this comment.
Record the job group before the wrapper can die
Fresh evidence beyond the prior registry resolution is that the pre-launch entry contains only the wrapper start time, while the job PGID is not recorded until this line after the child has started. If the wrapper receives SIGKILL or is OOM-killed in that interval, flock releases the lock but the child group survives; the next drain sees a dead wrapper with no recorded PGID, deletes the entry, and starts an exclusive job alongside that child. Close the launch-to-registration window so the surviving group remains discoverable even if the wrapper dies immediately.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 60d1059. The job's first process (the subshell whose pid becomes the job's pgid) now writes the full entry itself, " ", before it execs the command, and it does not start the command if its parent is no longer the wrapper. The wrapper no longer rewrites the entry after the fork. Entries are written to a dotfile in the registry and renamed into place, so a drain never reads half of one, and the drain skips dotfiles. test_a_job_whose_wrapper_dies_before_it_is_recorded_does_not_start kills the wrapper inside that window (GPU_JOB_TEST_RECORD_DELAY), checks that the command never runs, and checks that the next exclusive job is not blocked.
| mkdir -p "$registry" | ||
| start_time $$ >"$entry" |
There was a problem hiding this comment.
Abort when the job registry cannot be written
When the registry directory or entry cannot be created—for example, when GPU_LOCK_FILE is an existing usable lock in a directory without create permission, or the filesystem is full—these commands fail but the script has disabled set -e and does not check their statuses. It therefore launches the command and can return success without any registry record; if the holder or wrapper subsequently dies, the next exclusive drain cannot discover the surviving process group. Fail closed before launching unless the entry was created successfully.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 60d1059. If mkdir of the registry or the first entry write fails, gpu_job.sh exits 75 before the fork, and the command does not run. If the job's own write fails, it exits 75 without starting the command. test_a_job_that_cannot_be_recorded_does_not_run puts a regular file where the registry directory goes and checks for exit 75 with the command not run.
|
Verdict: REQUEST CHANGES Re-review on the exact head 09ec5a4 ("gpu_lock: the next exclusive job waits for a job still being stopped"). The registry closes the overlap I reproduced on a0c82e7.
Blocking. Codex left two new comments on 09ec5a4. Neither has a reply or fix yet, and both are sound:
Suggestions (non-blocking)
|
The wrapper wrote the job's pgid only after the fork, so a wrapper killed in that window left an entry the drain deleted, and the group went unseen. The job's first process now records its own pid as the pgid before the command starts, and does not start the command if its wrapper is already gone. Entries are written to a dotfile and renamed into place, so a drain never reads a half-written one. A failure to create the registry or write the entry now stops the job with exit 75 before the fork. Without nvidia-smi the drain now still waits for SGLang servers and recorded jobs instead of exiting at once, and its messages name what it waits for rather than only GPU processes.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 60d105992f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for e in "$registry"/*; do | ||
| [ -e "$e" ] || continue | ||
| w="${e##*/}" | ||
| case "$ancestors" in *" $w "*) continue ;; esac |
There was a problem hiding this comment.
Check start times before skipping ancestor-named entries
When a killed wrapper's PID is reused by this drain or one of its ancestors while the recorded job group is still alive, this PID-only check skips the old entry before examining its recorded wrapper start time or PGID. If that surviving group is still pre-CUDA, the drain can otherwise appear empty and start a supposedly exclusive job concurrently; only skip an ancestor entry when its recorded start time matches the current process, or avoid skipping registry entries entirely.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 344670f. leftover_jobs no longer skips entries named by one of the drain's ancestors. The skip was never needed, because the drain's own wrapper writes its entry only after the drain finishes. Every entry is now checked by wrapper start time and then by its recorded group. test_an_entry_named_by_an_ancestor_pid_still_counts names an entry after the test's own pid (an ancestor of the drain) with a live old group; it fails on 60d1059 and passes now.
| os.kill(int(wrapper[0]), 9) | ||
| os.kill(holder, 9) | ||
| out = tmp_path / 'b.out' | ||
| check = f'if kill -0 {job} 2>/dev/null; then echo overlap; else echo clean; fi > {out}' |
There was a problem hiding this comment.
Treat the orphaned job's zombie as stopped
On hosts where PID 1 does not promptly reap adopted children, the killed wrapper's sleep becomes a zombie after finishing, and kill -0 still succeeds because the PID exists. Fresh evidence in the newly added wrapper-kill case is that pytest -q tests/test_gpu_lock.py fails here with overlap even though the drain correctly ignored the non-running zombie; use the existing zombie-aware alive() logic or inspect /proc/<pid>/stat in this check.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 344670f. The overlap checks in the new tests now read the process state from /proc//stat and treat a zombie as gone, like alive(), instead of using kill -0.
|
Verdict: APPROVE (provisional: Codex's review of 60d1059 was still running when I posted this. I will re-check its threads on this head before merge.) Re-review on the exact head 60d1059 ("gpu_lock: the job records its own group; unrecordable jobs do not run"). Codex P1 on 09ec5a4 (record the group before the wrapper can die) is fixed. I checked this end to end.
Codex P2 (abort when the registry cannot be written) is fixed. If My earlier suggestions are in. The drain now checks servers and recorded jobs even without nvidia-smi; that test fails on 09ec5a4. The messages are reworded. Other checks
A note on one of the new tests. Blocking: none. Suggestions: none. |
|
Verdict: REQUEST CHANGES This updates my provisional approval of 60d1059 (#99 (comment)), which I withdraw. Codex finished its review of 60d1059 at 12:00:52 and left two P2s. Neither has a reply or fix yet, and both are sound and cheap to fix. Blocking
Everything else in my 60d1059 review still holds: the P1 reproduction across versions, the atomic writes, failing closed on an unrecordable job, and the drain working without nvidia-smi. |
…ks in tests leftover_jobs skipped entries named by one of the drain's ancestors before checking start time or pgid, so a reused pid could hide a live old group. The skip was not needed (the drain's own wrapper writes its entry only after the drain), so every entry is now checked. The overlap checks in the new tests used kill -0, which counts a zombie as running; they now read the process state from /proc.
|
@codex review |
|
Verdict: APPROVE (provisional: Codex's review of 344670f was still running when I posted this. I will re-check its threads on this head before merge.) Re-review on the exact head 344670f ("gpu_drain_wait: check every registry entry; zombie-aware overlap checks in tests").
Blocking: none. Suggestions (non-blocking)
|
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Since #90,
gpu_lock.shrunsflock -o, so the lock lasts exactly as long as the job's command. The side effect: killing a job's flock parent releases the lock while its child processes may still be running on the GPU.At 07:35 on 2026-10-01 that happened. A job was stopped partway through, and two of bench's exclusive holds started while its processes were still on the GPU. Bench's sweeps refused to run, so no data was wrong, but an exclusive hold wasn't exclusive.
What changes
scripts/gpu_job.sh(new) wraps every job.gpu_lock.shstarts it from flock undersetpriv --pdeathsig TERMandenv --default-signal, so the holder's death reaches it even when the caller ignores TERM. It:GPU_JOB_KILL_GRACEs (default 10). Zombie-only groups don't extend the wait;/dev/nullas stdin when stdin is a terminal, since a background process group can't read it;$GPU_LOCK_FILE.jobs/<wrapper pid>), and removes the entry only after the group is stopped.scripts/gpu_drain_wait.sh(new) runs before an exclusive job's command. Holding the exclusive lock means no holder is running, so anything it finds is a leftover. It waits until all of these are gone:nvidia-smi;sglang::, a python process with-m sglang.launch_server, or a detachednsys/nculauncher about to start one. The match skips its own ancestors and zombies;A failed or hung
nvidia-smicounts as busy. Each query and each sleep is bounded by the remaining time. AfterGPU_LOCK_DRAIN_WAITs (default 600), the drain exits 75 and names what it saw, so the job fails visibly instead of measuring on a busy GPU.Shared mode keeps coexisting with other shared holders. Its jobs also run under
gpu_job.sh, so they are stopped with their holder and recorded in the registry.Tests
tests/test_gpu_lock.pyhas 25 tests, withnvidia-smifaked on PATH and orphan names unique to the test run. They cover:Every fix after the first review comes with a test that fails on the head before it.
Also fixes the header's
GPU_LOCK_WAITdefault: it said 4 h, but the code uses 12 h (43200 s). The reviewer noted this on #90.Deployment
git pullreplaces the files. Holders and waiters that are already running keep the version they loaded, so they don't write registry entries.