Skip to content

gpu_lock -x: wait for orphaned GPU processes before starting an exclusive job - #99

Merged
SamMausberg merged 14 commits into
mainfrom
infra/gpu-lock-drain
Oct 1, 2026
Merged

SamMausberg merged 14 commits into
mainfrom
infra/gpu-lock-drain

Conversation

@SamMausberg

@SamMausberg SamMausberg commented Oct 1, 2026 •

Copy link
Copy Markdown
Owner

Since #90, gpu_lock.sh runs flock -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.sh starts it from flock under setpriv --pdeathsig TERM and env --default-signal, so the holder's death reaches it even when the caller ignores TERM. It:

  • refuses to run (exit 75) if its parent is no longer flock, which covers a holder that died before setpriv installed the signal;
  • runs the command in its own process group, and on a signal or normal exit sends TERM to the group, then KILL after GPU_JOB_KILL_GRACE s (default 10). Zombie-only groups don't extend the wait;
  • handles a signal that arrives before the job's pid is known;
  • gives the command /dev/null as stdin when stdin is a terminal, since a background process group can't read it;
  • records the job in a registry next to the lock ($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:

  • compute processes listed by nvidia-smi;
  • SGLang server processes, matched structurally: comm prefix sglang::, a python process with -m sglang.launch_server, or a detached nsys/ncu launcher about to start one. The match skips its own ancestors and zombies;
  • registry entries whose wrapper is still alive, or whose process group still has a live member. Start times guard against pid reuse, and stale entries are removed.

A failed or hung nvidia-smi counts as busy. Each query and each sleep is bounded by the remaining time. After GPU_LOCK_DRAIN_WAIT s (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.py has 25 tests, with nvidia-smi faked on PATH and orphan names unique to the test run. They cover:

  • the drain: wait, give up, failed and hung queries, detached servers and launchers, decoys, zombies, and the caller's own command line;
  • holder death: before setpriv, before the pid is known, a TERM-ignoring job, and a wrapper killed with flock;
  • the next exclusive job waiting while an earlier job is still being stopped;
  • stale registry entries, terminal stdin, and zombie-only groups.

Every fix after the first review comes with a test that fails on the head before it.

Also fixes the header's GPU_LOCK_WAIT default: it said 4 h, but the code uses 12 h (43200 s). The reviewer noted this on #90.

Deployment

git pull replaces the files. Holders and waiters that are already running keep the version they loaded, so they don't write registry entries.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 1, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-01T12:15:27.981020Z 344670f Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: APPROVE (provisional: Codex is running on 36ff983, and I'll re-check it before merge)

Reviewed head 36ff983, one commit on main 65a0a23.

  • Shared holders are never mistaken for orphans. The drain runs inside flock -o -x, after the exclusive lock is held, through bash -c '"$0" && exec "$@"' gpu_drain_wait.sh cmd…. An exclusive flock cannot coexist with any shared holder, so no live -s holder or its server can be on the GPU when it runs. The gpu_lock, flock and bash processes hold no CUDA context, so they are not listed either. Anything nvidia-smi --query-compute-apps shows at that point is a process outside any live holder.
  • The drain cannot deadlock the queue. It gives up after GPU_LOCK_DRAIN_WAIT (600 s), exits 75 and names the PIDs. That status propagates through bash -c and flock, so gpu_lock -x exits 75 and the lock is released. A persistent orphan therefore makes exclusive jobs fail visibly every ten minutes instead of blocking the queue. Shared jobs are not drained.
  • The header is fixed. It now gives the 12 h default for GPU_LOCK_WAIT (WAIT=43200), fixing my note on gpu_lock: a job's leftover background processes must not hold the lock #90, and documents the drain.
  • Checks.
    • tests/test_gpu_lock.py passes (6), including the two new faked-nvidia-smi cases: the drain completes before the command, and it gives up with 75 without running it.
    • pre-commit passes, including shellcheck.
    • The commit is Sam's, with no AI attribution.

Suggestions (not blocking):

  • nvidia-smi … || true treats a failing nvidia-smi as "no processes" and proceeds. Fail-open is reasonable for availability, but a one-line warning on a non-zero exit would make a driver problem visible in the job log.
  • Note in the header that a GPU process started outside gpu_lock (anyone not using the lock) will now make every exclusive job exit 75 after 10 minutes. That is intended, but a person reading a 75 should know to look for it.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_drain_wait.sh Outdated
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)"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@SamMausberg

Copy link
Copy Markdown
Owner Author

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.

  • A failed query now fails closed. A failing nvidia-smi --query-compute-apps is treated as busy, pids="(nvidia-smi query failed)". It is reported, and once the deadline passes it exits 75 naming the failure. A successful empty query still exits 0.
  • No nvidia-smi at all still means nothing to drain. It logs this and exits 0, which is right for CPU-only hosts and for the tests.
  • Checks.
    • tests/test_gpu_lock.py passes (7), including the new failed-query case.
    • pre-commit passes, including shellcheck, and the Codex thread has a reply.

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_lock.sh Outdated
# 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" "$@"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_drain_wait.sh Outdated
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: REQUEST CHANGES (this withdraws my provisional approval at a5bda1c)

Codex finished on a5bda1c with a P2 on gpu_drain_wait.sh:21, and it is valid and blocking. If nvidia-smi hangs rather than failing, the command substitution never returns. The deadline check is then never reached, and the job holds the exclusive lock indefinitely. That is the deadlock this script has to avoid.

B1. Bound each query, for example timeout 30 nvidia-smi …, and treat a timeout like a failed query (busy, fail closed) so the overall GPU_LOCK_DRAIN_WAIT deadline is honoured. Add a test with a faked nvidia-smi that sleeps past the deadline.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: APPROVE (provisional until Codex finishes on 359d70f; I'll re-check before merge)

Re-reviewed 359d70f.

  • The query bound is fixed. Each nvidia-smi query runs under timeout ${GPU_LOCK_SMI_TIMEOUT:-30}, and a timeout counts as busy, so the overall drain deadline always holds.
  • No job process outlives the lock.
    • gpu_lock.sh runs gpu_job.sh under setpriv --pdeathsig TERM, so the wrapper is signalled if the flock process dies.
    • gpu_job.sh runs the command in its own process group (set -m) and terminates that group on TERM, INT or HUP and after the command exits.
    • setsid children leave the group and are untouched, and the drain catches any that reach the GPU.
    • The -x path drains before running. The -s path drops the ticket once the shared lock is held.
  • I checked the behaviour on this box.
    • setpriv (util-linux 2.37.2) is installed.
    • A command's exit status propagates: bash -c 'exit 7' under gpu_lock.sh -s exits 7.
    • stdin still reaches the command: echo hello | gpu_lock.sh -s bash r.sh printed got:hello.
  • Checks. tests/test_gpu_lock.py passes (10): a killed holder stops a starting job, a leftover group member is stopped at job end, and a hung query exits 75. pre-commit passes, including shellcheck.

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 setsid nohup gpu_lock.sh … &, which leaves the group, so they are unaffected. A job that starts a server with & and expects it to keep running after the job exits would now see that server terminated. Under the "stop your servers before exit" rule, that is the intended outcome.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_job.sh Outdated
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; }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_job.sh Outdated
Comment on lines +31 to +35
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"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_drain_wait.sh Outdated
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_drain_wait.sh Outdated
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)"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_drain_wait.sh Outdated
[ "$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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_drain_wait.sh Outdated
echo "gpu_drain_wait: GPU still busy after ${limit}s (pids: $(echo "$pids" | tr '\n' ' ')); not starting" >&2
exit 75
fi
sleep 5

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: REQUEST CHANGES

Re-reviewed 564c42e (c826e3a and 564c42e). The fixes for Codex's three items on 359d70f are right:

  • TERM escalates to KILL after GPU_JOB_KILL_GRACE;
  • each query is clamped to the remaining drain time, with --kill-after;
  • the job's own ancestors are excluded.

tests/test_gpu_lock.py passes (12).

Blocking

B1. The orphan pattern matches processes that are not SGLang servers. pgrep -f -- "sglang[.]launch_server|sglang::" searches whole command lines. On this host right now, it matches only two bash -c shells: mine, whose command line contains the pattern, because no SGLang server is running.

The same goes for anything whose command line mentions those strings:

  • an agent's monitor loop (pgrep -f sglang::, ps … | grep sglang.launch_server);
  • a bash -c wrapper;
  • a queued gpu_lock.sh job whose command names sglang.launch_server. That job is waiting for the very lock the drain holds.

Under the exclusive lock any of these makes the drain wait the full GPU_LOCK_DRAIN_WAIT and exit 75, so an exclusive job fails for no reason.

Match server processes structurally instead:

  • processes whose comm starts with sglang:: (SGLang's process titles; pgrep without -f matches comm, truncated to 15 characters);
  • or python processes whose argv contains the tokens -m and sglang.launch_server in sequence, read from /proc/<pid>/cmdline, not a substring of a shell string.

Add a test with a decoy process whose command line contains the pattern text (e.g. bash -c 'sleep 30 # sglang.launch_server'). The drain must ignore it.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_lock.sh Outdated
Comment on lines +115 to +116
flock -o -x -w "$WAIT" -E 75 "$LOCK_FILE" \
setpriv --pdeathsig TERM -- "$HERE/gpu_job.sh" -x "$@"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_job.sh Outdated
done
kill -KILL -- "-$pid" 2>/dev/null || true
}
trap 'stop_group; exit 143' TERM INT HUP

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_drain_wait.sh Outdated
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_drain_wait.sh Outdated
Comment on lines +60 to +63
[ "$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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread tests/test_gpu_lock.py Outdated
Comment on lines +229 to +233
deadline = time.time() + 5
while time.time() < deadline:
try:
os.kill(left, 0)
except ProcessLookupError:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: REQUEST CHANGES (two small Codex items left)

Re-reviewed 5c327a7. My B1 is fixed.

  • Orphans are now matched structurally: comm starting with sglang::, or a python process whose argv holds -m sglang.launch_server as adjacent tokens. Shells, monitors and waiting lock clients that only mention the text no longer match.
  • I ran orphan_servers on this host. It found only processes whose comm was sglang::: a real server shutting down, gone a moment later. It did not match my own shell, which the old pattern did.
  • env --default-signal exists here and resets an inherited ignored TERM.
  • The trap is now installed before the command starts.
  • tests/test_gpu_lock.py passes (16) on this box, and pre-commit passes.

Codex finished on 5c327a7 with two P2s. Both are valid and quick to fix:

  1. gpu_drain_wait.sh:63. With under 2 s remaining, smi_limit is clamped back to 1 and --kill-after=1 adds another second, so a TERM-ignoring query can overrun a 1 s deadline by about a second. When the remaining time can't cover the limit plus the KILL grace, skip the query and treat the GPU as busy, or exit 75.
  2. tests/test_gpu_lock.py:233 and :280. os.kill(pid, 0) succeeds on a zombie, so where PID 1 is slow to reap, a terminated process reads as alive. Treat state Z in /proc/<pid>/stat as stopped.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_drain_wait.sh
Comment on lines +35 to +38
comm="$(cat "$d/comm" 2>/dev/null)" || continue
if [ "${comm#"$comm_prefix"}" != "$comm" ]; then
printf '%s ' "$pid"
continue

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Fixed in 5c8e927: orphan_servers skips processes in state Z (from /proc//stat); a zombie-decoy test covers it.

@SamMausberg

Copy link
Copy Markdown
Owner Author

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.

  • No query can overrun the deadline. With under 2 s of drain time left, no query runs and the GPU counts as busy ("no time left for a bounded GPU query"). The 1 s limit plus its 1 s KILL grace therefore always fits.
  • The tests are zombie-aware. Liveness comes from /proc/<pid>/stat through alive(), and zombies count as gone. The tests that need a query have 4 s of drain time.
  • Checks. tests/test_gpu_lock.py passes (16), and pre-commit passes, including shellcheck.

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.

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: REQUEST CHANGES (this withdraws my provisional approval at fe410ad)

Codex finished on fe410ad with a P1 on gpu_drain_wait.sh:38, and it is valid. orphan_servers counts any process whose comm starts with sglang::, including a zombie. A detached SGLang worker whose parent was killed can stay a zombie if PID 1 is slow to reap it, and its /proc/<pid>/comm still reads sglang::…. Every exclusive job would then wait 600 s and exit 75 because of a process that cannot use the GPU.

B1. Skip state Z (from /proc/<pid>/stat, as the test helper alive() already does) in both branches of orphan_servers, and add a test with a zombie decoy.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_job.sh
exit 64
fi

set -m # background jobs get their own process group, so the whole group can be signalled

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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).

Comment thread scripts/gpu_job.sh Outdated
[ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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).

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: APPROVE

Re-reviewed 5c8e927, which fixes Codex's zombie P1 on fe410ad. orphan_servers reads /proc/<pid>/stat and skips state Z before checking comm or argv, so a reaped-late sglang:: zombie no longer counts as an orphan. The new zombie-decoy test (a renamed, unreaped child) passes, along with the full suite: tests/test_gpu_lock.py 17 passed. pre-commit passes. Codex finished on 5c8e927 with no comments.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_drain_wait.sh Outdated
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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_lock.sh
Comment on lines +116 to +117
flock -o -x -w "$WAIT" -E 75 "$LOCK_FILE" \
setpriv --pdeathsig TERM -- env --default-signal=TERM,INT,HUP "$HERE/gpu_job.sh" -x "$@"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_job.sh Outdated
Comment on lines +67 to +68
if [ -t 0 ]; then "$@" </dev/null & else "$@" & fi
pid=$!

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

@SamMausberg

Copy link
Copy Markdown
Owner Author

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:

  • Zombie cleanup wait. stop_group waits on group_running, which counts non-zombie group members from /proc/*/stat.
  • Terminal stdin. With a terminal on stdin, the command reads /dev/null instead of being stopped by SIGTTIN.
  • Detached profiler launchers (P1 on a177999). orphan_servers now also counts an nsys/ncu launcher whose argv holds python… -m sglang.launch_server.
  • Holder death before setpriv (P2 on a177999). gpu_job.sh exits 75 unless its parent's comm is flock.
  • tests/test_gpu_lock.py passes (21), and pre-commit passes.

Blocking, Codex P2 on f926ad4 (gpu_job.sh:68). If the holder dies after "$@" & starts the command but before pid=$! runs, the TERM trap can fire with pid still empty. stop_group then returns without signalling the new process group, which outlives the lock. Have the trap record a pending stop when pid is empty, and check that flag right after pid=$!, then stop the group. Add a test that delivers TERM in that window, for example by having a test hook sleep between the spawn and the assignment.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_job.sh
Comment on lines +59 to +63
while group_running && [ "$t" -lt "${GPU_JOB_KILL_GRACE:-10}" ]; do
sleep 1
t=$((t + 1))
done
kill -KILL -- "-$pid" 2>/dev/null || true

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

@SamMausberg

Copy link
Copy Markdown
Owner Author

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.

  • A signal that arrives before pid=$! used to run stop_group with an empty pid, which returned at once and left the job running. The signal is now recorded and acted on once pid is set. This also covers a signal during the test's sleep: bash defers the trap until the sleep returns, while pid is still empty.
  • The new test catches the old bug. I put the f926ad4 trap back into the a0c82e7 script, keeping only the GPU_JOB_TEST_SPAWN_DELAY hook. test_a_holder_death_before_the_job_pid_is_known_still_stops_the_job then fails ("a signal before pid was set let the job outlive the lock holder"). With a0c82e7 it passes.
  • pytest tests/test_gpu_lock.py: 22 passed in 73.7 s.
  • pre-commit run --files scripts/gpu_job.sh tests/test_gpu_lock.py: everything passes, including shellcheck, ruff, mypy and codespell.
  • The commit is authored and committed by Samuel Mausberg, with no AI attribution.

Blocking

  1. Codex P1 on a0c82e7, "Delay successors until the old job group is dead" (gpu_lock -x: wait for orphaned GPU processes before starting an exclusive job #99 (comment)), has no reply or fix. I reproduced it on a0c82e7. The script is below.
    • Job A runs under -x with trap '' TERM; sleep 3; date > a_continued. I SIGKILL its flock holder, then submit job B under -x.

    • B started 1.89 s after the kill. A's command, which ignored TERM, continued at 2.96 s, so the two "exclusive" jobs overlapped.

    • The cause: once flock dies the lock is released, while stop_group is still in its GPU_JOB_KILL_GRACE window (10 s by default). The exclusive drain only sees GPU compute processes and SGLang or profiler launchers, so a generic command that has not reached CUDA yet is invisible to it.

    • The file header says "none of its processes outlives the lock", and this case breaks that. c826e3a's escalation to KILL bounds the window but does not close it.

    • Please make the exclusive drain wait for any earlier job group that is still being cleaned up. One way:

      • after pid=$!, gpu_job.sh records its job's pgid in a registry next to the lock file;
      • it removes the entry only after stop_group has seen the group gone;
      • gpu_drain_wait.sh waits, within its deadline, while any recorded pgid has a live non-zombie member, and deletes entries with none.

      This bounds the wait by the grace period. It also covers a wrapper that was SIGKILLed together with flock and never ran its trap. Waiting for any other live gpu_job.sh process would close Codex's case but not that one.

    • Add a test in the shape of the reproduction below: a TERM-ignoring job, a killed holder, and a successor that must not start until the old command is gone.

Suggestions (non-blocking)

  • Also check pending just before the fork. A signal that arrives between trap and the fork would then not start the job at all. Today the job starts and is stopped at once. The check after pid=$! is still needed for the fork-to-pid window.

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: B started at 1.89 s; A continued at 2.96 s; OVERLAP.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_job.sh Outdated
# 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"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread scripts/gpu_job.sh Outdated
Comment on lines +93 to +94
mkdir -p "$registry"
start_time $$ >"$entry"

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

@SamMausberg

Copy link
Copy Markdown
Owner Author

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.

  • I re-ran my reproduction on 09ec5a4 with GPU_LOCK_DRAIN_WAIT=30. Job A's TERM-ignoring command continued 2.95 s after its flock was SIGKILLed. Job B started only at 8.93 s, so the two did not overlap.
  • With the original GPU_LOCK_DRAIN_WAIT=5, B did not start at all: the drain waited for the recorded job, ran past its deadline and exited 75. That is failing closed, which is right.
  • The new tests catch the old bugs. I put the a0c82e7 versions of gpu_job.sh, gpu_drain_wait.sh and gpu_lock.sh back under the new test file. All three new tests then fail. With 09ec5a4 they pass.
  • Full suite: pytest tests/test_gpu_lock.py: 25 passed in 98 s.
  • Pre-commit: passes on the four changed files, shellcheck included.
  • Attribution: the commit is authored and committed by Samuel Mausberg, with no AI lines in it or in the rewritten PR description.
  • Merge state: GitHub reports the branch as MERGEABLE/CLEAN against main.
  • The reuse checks are right.
    • The wrapper's start time guards against wrapper-pid reuse.
    • The leader's start time guards against a new group with the same pgid.
    • Linux does not hand out a pid again while it is still some process's pgid, so the member scan cannot be fooled by reuse.
  • The pre-fork pending check from my last review is in.

Blocking. Codex left two new comments on 09ec5a4. Neither has a reply or fix yet, and both are sound:

  1. P1: record the job group before the wrapper can die (gpu_lock -x: wait for orphaned GPU processes before starting an exclusive job #99 (comment)).
    • The pgid is written after the fork, through a line with three command substitutions. If the wrapper is SIGKILLed or OOM-killed in that window, the entry holds only the wrapper start time. The next drain sees a dead wrapper with no pgid, deletes the entry, and starts alongside the surviving group.
    • One way to close it is to have the child register itself before it execs the command. It writes <wrapper start> <own pid> <own start>, since under set -m its pid is its pgid. It then checks that its parent is still the wrapper, and exits without exec'ing if it was reparented.
    • With that order, either the entry carries the pgid before the command can run, or the command never runs.
    • Write entries atomically, whatever the fix. Write to a dotfile such as $registry/.$$.tmp, which the drain's * glob skips, then mv it into place. Today echo ... >"$entry" truncates first. A drain that reads the truncated entry of a live wrapper sees no start time and no pgid, and deletes it. That race sits in the same window.
  2. P2: abort when the registry cannot be written (gpu_lock -x: wait for orphaned GPU processes before starting an exclusive job #99 (comment)).
    • mkdir -p and the entry write are not checked (set -e is off). A full disk or an unwritable lock directory therefore runs the job with no record.
    • Fail closed before the fork, exit 75 with a message, and add a test that makes the registry path unwritable.

Suggestions (non-blocking)

  • gpu_drain_wait.sh exits 0 with "no nvidia-smi on PATH; nothing to drain" before it checks the registry. The registry wait does not need nvidia-smi, so it could run first.
  • The waiting message still says "waiting for compute processes left on the GPU" when the only entries are jobs:.... Something like "waiting for: " would fit all three kinds.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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".

Comment thread scripts/gpu_drain_wait.sh Outdated
for e in "$registry"/*; do
[ -e "$e" ] || continue
w="${e##*/}"
case "$ancestors" in *" $w "*) continue ;; esac

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

Comment thread tests/test_gpu_lock.py Outdated
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}'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

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.

@SamMausberg

Copy link
Copy Markdown
Owner Author

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.

  • How the fix works.
    • The job's first process is a subshell, so its pid is the pgid. It writes the full entry <wrapper start> <own pid> <own start> before it execs.
    • It then checks that its ppid is still the wrapper, and exits 75 if not.
    • Whatever moment the wrapper dies, either the entry names the group before the command can start, or the command never starts.
  • Reproduction across versions. I set GPU_JOB_TEST_SPAWN_DELAY=5, then SIGKILLed the wrapper and flock together after the job had started, and submitted an exclusive job B.
    • On 09ec5a4, B started at 1.95 s while A's job ran until 2.97 s: they overlapped.
    • On 60d1059, B started at 8.88 s, after A had finished: no overlap.
  • The writes are now atomic. Entries go to a dotfile and are renamed into place with mv, and the drain's glob skips dotfiles. A drain can no longer read half an entry and delete it.
  • Temporary-file names and traps.
    • The temporary names (.$$.$BASHPID) differ between the wrapper and the subshell, and the EXIT trap removes both.
    • The subshell does not inherit the wrapper's TERM trap, so a TERM before exec kills it.

Codex P2 (abort when the registry cannot be written) is fixed. If mkdir or the first write fails, the job exits 75 before the fork. The new test fails on 09ec5a4.

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

  • My original overlap reproduction (a TERM-ignoring job and a killed holder) still shows no overlap: B starts at 8.92 s, after A ends at 2.95 s.
  • Tests: pytest tests/test_gpu_lock.py: 28 passed in 114 s.
  • Pre-commit: passes on the three changed files, shellcheck included.
  • Attribution: the commit is by Samuel Mausberg, with no AI lines. The branch is MERGEABLE.

A note on one of the new tests. test_a_job_whose_wrapper_dies_before_it_is_recorded_does_not_start cannot fail on 09ec5a4 for the right reason, because 09ec5a4 has no record-delay hook. Without the delay, touch finishes before the test finds the wrapper. The reproduction above is the cross-version check, and it does show the change. The test is still correct as a check of the new behaviour.

Blocking: none. Suggestions: none.

@SamMausberg

Copy link
Copy Markdown
Owner Author

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

  1. Check start times before skipping ancestor-named entries (gpu_lock -x: wait for orphaned GPU processes before starting an exclusive job #99 (comment)).
    • leftover_jobs skips any entry whose name, the wrapper pid, is in the drain's ancestor chain. It does this before looking at the recorded start time or pgid.
    • If a killed wrapper's pid is reused by one of the drain's ancestors while the old job's group still runs, the entry is skipped, and the drain can pass with that group alive.
    • The skip does not look necessary. The drain runs before its own wrapper writes an entry, so no ancestor of the drain has an entry yet. Dropping the skip, or skipping only when the recorded start time matches the live process, closes the case.
    • If the skip stays, please add a test that plants an entry named by an ancestor pid, with a different start time and a live group.
  2. Treat the orphaned job's zombie as stopped in the new tests (gpu_lock -x: wait for orphaned GPU processes before starting an exclusive job #99 (comment)).
    • The overlap checks inside job B use kill -0 <pid>, which succeeds for a zombie.
    • On a host where PID 1 does not reap adopted children promptly, the wrapper-kill test reports overlap even though the drain correctly ignored a non-running zombie. Codex reports this failing in its environment. On this box, systemd reaps promptly and all 28 tests pass.
    • Read the state from /proc/<pid>/stat in the B-side check, as the drain and alive() already do. Please check every new test that uses kill -0 for overlap.

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.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@SamMausberg

Copy link
Copy Markdown
Owner Author

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").

  • Codex P2 r4155110686 (ancestor-named entries) is fixed and has a reply.
    • leftover_jobs no longer skips entries named after the drain's ancestors, so every entry is judged by its recorded start time and pgid.
    • Dropping the skip is safe. The drain runs before its own wrapper writes an entry, so no ancestor of the drain has a live entry of its own.
    • The new test test_an_entry_named_by_an_ancestor_pid_still_counts plants an entry named after the test's own pid, with a live old group. With the 60d1059 gpu_drain_wait.sh it fails ('overlap' == 'clean'); with 344670f it passes. It also covers the zombie case: the sleeper becomes an unreaped zombie of pytest, and the drain correctly stops waiting for it.
  • Codex P2 r4155110691 (zombie-aware overlap checks) is fixed and has a reply. The overlap checks inside job B now read the state from /proc/<pid>/stat through running_check(), so a zombie counts as gone. No kill -0 overlap check is left in the new tests.
  • Tests: pytest tests/test_gpu_lock.py: 29 passed in 124 s.
  • Pre-commit: passes on both changed files.
  • Attribution: the commit is by Samuel Mausberg, with no AI lines.

Blocking: none.

Suggestions (non-blocking)

  • Entries are named by the wrapper pid alone. A new shared (-s) wrapper that reuses the pid of a SIGKILLed wrapper whose group is still alive would overwrite that entry with mv -f. Shared jobs do not drain, so nothing waits first. With this box's pid space this is very unlikely. Naming entries <pid>.<start time> would rule it out.
  • running_check takes the third space-separated field of /proc/<pid>/stat. That is right for sleep and bash, but not for a command name that contains a space. Splitting after the last ), as the scripts do, would be safer if the helper is reused.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 344670f3dd

ℹ️ 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".

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.

1 participant