Skip to content

gpu_lock: a job's leftover background processes must not hold the lock - #90

Merged
SamMausberg merged 2 commits into
mainfrom
infra/gpu-lock-close-fd
Oct 1, 2026
Merged

SamMausberg merged 2 commits into
mainfrom
infra/gpu-lock-close-fd

Conversation

@SamMausberg

Copy link
Copy Markdown
Owner

scripts/gpu_lock.sh ran its command with the locked file descriptor inherited, so any background process the job left behind kept the GPU lock.

On 2026-10-01 at 06:17 this deadlocked the queue. A hold queued its successor with setsid nohup gpu_lock.sh ... & from inside itself. The successor inherited fd 3, so it held the exclusive lock while it waited for older tickets, and those tickets were waiting for that same lock. The GPU sat idle with two priority holds blocked until the leftover processes were killed.

The fix is flock -o in both modes. flock now holds the lock for exactly the command's lifetime and closes the fd before running it, the same way gpu_startup_lock.sh already does.

tests/test_gpu_lock.py covers it:

  • a job that leaves a detached sleeper behind releases the lock (-x and -s);
  • the exclusive lock is held while the command runs.

Both leftover-process tests fail on the old script and pass with the fix.

Deployment: merge, then git pull in each checkout. git replaces the file, so running holders keep the old inode. Never edit the script in place.

flock passed the locked fd to the command, so any background process a job
left behind kept the GPU lock. On 2026-10-01 a hold that queued its successor
from inside itself deadlocked the queue: the successor inherited the lock
while waiting for older tickets that were waiting for that lock. flock -o
closes the fd for the command, so the lock lasts exactly as long as the
command. Tests cover both modes and fail without the change.
@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-01T06:35:10.192063Z 1c4f7a8 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.

@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: 1913661a4f

ℹ️ 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
flock -x -w "$WAIT" -E 75 "$LOCK_FILE" "$@"
# -o: the lock is held by flock itself for the command's lifetime and is not inherited, so a
# background process the job leaves behind (or a successor ticket it queues) cannot keep it.
flock -o -x -w "$WAIT" -E 75 "$LOCK_FILE" "$@"

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 Reconcile -o with the background-server contract

When a wrapped command deliberately starts a server in the background and then exits, this releases the lock while that server is still using the GPU, allowing a subsequent exclusive benchmark to run concurrently. This contradicts the same script's documented contract at lines 20–21 that a server left running keeps the GPU locked; flock --help confirms that -o will “close file descriptor before running command,” so descendants cannot retain it. Either revise that supported workflow and its callers or preserve the lock for legitimate background servers while addressing the successor deadlock separately.

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 1c4f7a8. The header now says flock holds the lock for exactly the command's lifetime: children don't inherit it, so a job must stop its servers and background processes before it exits, and killing the flock process releases the lock. A shared-mode test is added.

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: REQUEST CHANGES

Reviewed head 1913661, one commit on main 85004ec. The fix is right; one documentation item blocks.

What I checked.

  • The tests. tests/test_gpu_lock.py passes on the fix (3). Run against main's script, both leftover-process cases fail (-x and -s), and the held-while-running case passes.

  • The -s path, run by hand on a scratch lock file:

    • While gpu_lock.sh -s sleep 4 runs, an exclusive request is blocked, a shared request succeeds, and the queue directory is empty, so the ticket was dropped once the lock was held.
    • The sleep process holds no fd on the lock file; the flock -o -s parent holds the lock.
    • The lock is free after the command exits.
  • The bench's "lock held" check still works. bench/server.py:gpu_lock_held_by_someone tests with a fresh descriptor (flock -n -s … true), so it does not depend on the job inheriting the fd. bench/sweep.py and bench/quality.py will still see the lock under -x.

  • No current holder relies on inheriting the fd. I checked:

    • the running and queued gpu_lock.sh commands;
    • the 85 references in the repository;
    • the hold and job scripts under ~/vp-data/*/holds and */jobs.

    None starts a server in the background and exits expecting the lock to stay held. Nested use goes through VP_LOCKED=1 (experiments/profiling/run_all.sh), which the outer flock's lock still covers.

  • pre-commit (including shellcheck) passes. The commit is Sam's, with no AI attribution.

Blocking

B1. The documented lock contract contradicts the new behaviour. This is also Codex's P2 on gpu_lock.sh:107. The header still says:

The lock is released when the command and every child holding it exit, so a server left running keeps the GPU locked.

With -o, a server left running no longer holds the lock: the next exclusive job can start while it still occupies GPU memory. That is the intended trade, but the header must say so. For example: "The lock is held by flock for exactly the command's lifetime; background processes the command leaves behind do not hold it, so a job must stop any server it started before it exits." Please also reply on the Codex thread, and announce the change to the workstreams in BRIEF.md, since the old text told them a leftover server was safe.

Suggestions

  • Add a -s test that the shared lock is held while the command runs (an exclusive request blocks, a shared one succeeds) and that the ticket is gone. I checked both by hand.
  • With -o, only the flock parent holds the lock. Killing that one process (not its process group) now releases the lock while the command keeps running. Before, the command's own fd kept it. A sentence in the header would cover it.

Deployment, as the PR says: git pull replaces the file, and running holders keep the old inode.

…the shared path

Addresses review of #90 (and Codex's P2): the header no longer says a server
left running keeps the GPU locked; jobs must stop their servers before they
exit. Adds a shared-mode test: the lock is held, a second shared holder gets
in, and the ticket is dropped.
@SamMausberg

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 1c4f7a8336

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

@SamMausberg

Copy link
Copy Markdown
Owner Author

Verdict: APPROVE

Re-reviewed 1c4f7a8.

  • B1 is fixed. The header now says that flock holds the lock itself (-o) for exactly the command's lifetime, that children do not inherit it, that a job must stop its servers and background processes before it exits, and that killing the flock process releases the lock. The Codex thread has a reply, and Codex's manual run on 1c4f7a8 finished with no new comments.
  • The -s suggestion is taken. test_shared_job_drops_its_ticket_and_holds_the_lock checks four things: an exclusive request is blocked during the command, a second shared holder gets in, the queue is empty (the ticket was dropped), and the lock is free afterwards. That matches what I checked by hand on 1913661.
  • Checks. tests/test_gpu_lock.py passes (4). pre-commit, including shellcheck, passes. The commits are Sam's, with no AI attribution.

Suggestion (not blocking): the reflowed line still says "GPU_LOCK_WAIT (seconds, default 4 h)", but the script sets WAIT="${GPU_LOCK_WAIT:-43200}", which is 12 h. The mismatch predates this PR. Fix it in the BRIEF.md follow-up, or whenever the header is next touched.

Deployment: git pull in each checkout, which replaces the file; running holders keep the old inode.

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