Skip to content

gpu_startup_lock.sh: opt-in free-memory gate (GPU_STARTUP_MIN_FREE_GB) - #84

Merged
SamMausberg merged 1 commit into
mainfrom
infra/startup-min-free
Oct 1, 2026
Merged

SamMausberg merged 1 commit into
mainfrom
infra/startup-min-free

Conversation

@SamMausberg

Copy link
Copy Markdown
Owner

Shared GPU jobs that start SGLang servers can leave each other too little memory. SGLang sizes its pools as roughly --mem-fraction-static times the free memory it sees at start-up, minus the weights. At 04:11 a DFlash server at 0.25 found 0.46 GB for its pools while five shared holders were running, and it exited ("Not enough GPU memory for hybrid (mamba/linear-attention) state cache"). Kernel's dflash_check failed the same way.

This PR adds an opt-in free-memory gate to scripts/gpu_startup_lock.sh:

  • GPU_STARTUP_MIN_FREE_GB=n: take the start-up lock and read the free memory of GPU GPU_STARTUP_GPU (default 0) from nvidia-smi. If at least n GiB are free, run the command while holding the lock. Otherwise release the lock, wait GPU_STARTUP_RETRY_WAIT seconds (default 60) and try again, up to GPU_STARTUP_TRIES times (default 30). When the tries run out, exit 75 without running the command. Each retry is logged to stderr. An unreadable nvidia-smi value counts as too little memory, and invalid settings exit 64.
  • With the variable unset, the script still runs exec flock -o ... as before.
  • On the gated path the lock descriptor is closed for the command ({fd}>&-), so a background server never holds it, and the lock is released explicitly once the command returns.

Tests: tests/test_gpu_startup_lock.py (CPU only, with nvidia-smi faked on PATH), 12 cases. They cover: the unset path, including exit-status pass-through and no memory query; a gate that passes; a gate that gives up after N tries without running the command; retries until memory frees; a fractional threshold; an unreadable value; invalid settings; the lock being held during the command and not inherited by a background child, checked through /proc/<pid>/fd (the test fails if {fd}>&- is removed); and a lock-wait timeout exiting 75 on both paths. pre-commit is clean, including shellcheck. The script header gives a dry run (GPU_STARTUP_MIN_FREE_GB=40 GPU_STARTUP_TRIES=1 scripts/gpu_startup_lock.sh nvidia-smi --query-gpu=memory.free --format=csv), which printed 52,156 MiB free and exited 0 here.

Deployment: jobs execute this script while they run. A git checkout or merge replaces the file with a new inode, so a running bash keeps reading the old one. Any manual update should also write a temporary file and mv it into place, never edit in place.

First user: experiments/drafter/serve_run.py on drafter/buffered-verify (778bb23) now takes the start-up lock through this script, with --min-free-gb setting the variable. That change goes in through the buffered-verify PR, not this one. The coordinator will add the variable to BRIEF.md once this merges.

With GPU_STARTUP_MIN_FREE_GB=n the script takes the start-up lock, reads
the GPU's free memory from nvidia-smi and runs the command only if at least
n GiB are free; otherwise it releases the lock, waits GPU_STARTUP_RETRY_WAIT
seconds (default 60) and retries up to GPU_STARTUP_TRIES times (default 30),
then exits 75 without running the command. Unset, the script still execs
flock -o as before. The lock descriptor is closed for the command, so a
background server never holds it. CPU tests fake nvidia-smi on PATH.
@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-01T04:40:51.325851Z dd97139 PR opened
ℹ️ 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

Reviewed head dd97139, one commit on main 36db481. Codex finished on dd97139 with no findings.

(1) The unset path behaves exactly as before. I put a fake flock on PATH that prints its argv and ran the old script (main) and the new one with the same arguments, including one containing a space and an empty one. The argv was identical in three cases: defaults, with GPU_STARTUP_LOCK_WAIT/GPU_LOCK_FILE overridden, and with GPU_STARTUP_MIN_FREE_GB= (empty, which counts as unset). In every case it was -o -w <wait> -E 75 <lock>.startup <cmd...>.

(2) On the gated path, the command does not get the lock.

  • "$@" {fd}>&- closes the descriptor in the child only. On this box's bash 5.1.16, I checked that the child's /proc/self/fd lacks it and the parent still holds it, so the explicit flock -u "$fd" that follows succeeds under set -e.
  • test_lock_is_held_during_the_command_and_not_inherited covers both modes. While the command runs, the lock is held. Afterwards, a background child neither holds the lock nor has the file open.

(3) Exit codes.

  • 75 on a lock timeout (flock ... || exit 75) and when the tries run out.
  • 64 on a non-numeric threshold or wait, or on tries ≤ 0.
  • Otherwise the command's own status.
  • A missing or unreadable nvidia-smi counts as too little memory.

Run here.

  • tests/test_gpu_startup_lock.py: 12 passed.
  • pre-commit, including shellcheck: passes.
  • I ran a dry run against the real nvidia-smi on a scratch lock file, so the live start-up lock was not touched. Here -i 0 with nounits returns 96764\n, the parse works, and a 1 GiB gate ran the command. A 100,000 GiB gate retried twice, logged both tries and exited 75 without running it.

The commit is Sam's, with no AI attribution.

Deployment note, as the author says: jobs execute this script while they run, so update ~/verified-progress with git pull or checkout, which replaces the file. Don't edit it in place.

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