Skip to content

fix(cli): serialize shared virtual environment setup - #3435

Merged
bxyu-nvidia merged 2 commits into
mainfrom
mbien/fix/venv-setup-race
Sep 18, 2026
Merged

bxyu-nvidia merged 2 commits into
mainfrom
mbien/fix/venv-setup-race

Conversation

@Glorf

@Glorf Glorf commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Prevent a server from starting while another server is still installing dependencies into their shared virtual environment. With skip_venv_if_present=true, the presence of bin/python and bin/activate could previously trigger premature reuse and import failures such as missing aiohttp.

Check readiness at execution time under a per-environment lock, and record completion only after installation succeeds. A surviving installer retains the lock if its wrapper is killed; failed or interrupted setup can be retried.

No separate issue: this focused bug fix includes a regression test for the observed startup race.

Validation

Run against the final squashed change (53b072cb):

  • .venv/bin/python -m pytest tests/unit_tests/test_cli_setup_command.py tests/unit_tests/test_cli.py -x --durations=5 — 132 passed.
  • .venv/bin/pre-commit run --all-files — passed.

New runtime coverage is limited to seven cases across four tests: concurrent setup/interruption, failure/retry, reuse without a manifest, and the generated setup/activation command. Existing dependency and version assertions are retained. The full unit suite and coverage were not rerun for this revision.

Rollouts

N/A: this changes component setup, with no changes to agent or verifier behavior. Startup is exercised with real subprocesses and a controlled dependency installer; no model rollout was run for this revision.

Compatibility and benchmark impact

The existing skip_venv_if_present option and default are unchanged. Environments without a completion marker run dependency installation once before reuse, without being deleted. Setup/reuse requires a writable sibling lock file on a filesystem supporting process-shared locks. Benchmark definitions and scoring are unchanged.

Checklist

  • I have read the contributing guidelines.
  • The change is focused.
  • Tests added or updated and pass locally.
  • Documentation: N/A for this focused fix; the upgrade behavior is described above, with no new public configuration or API.
  • Pre-commit checks pass locally.
  • New source files include the required Apache-2.0 SPDX header.
  • All commits have DCO sign-off.

@copy-pr-bot

copy-pr-bot Bot commented Sep 16, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@github-actions

Copy link
Copy Markdown
Contributor

Check environment readiness under a per-venv lock and mark setup complete only after dependency installation succeeds. Preserve existing reuse behavior and cover concurrent setup, interruption, and retry with focused regression tests.

Signed-off-by: Michal Bien <[email protected]>
@Glorf
Glorf force-pushed the mbien/fix/venv-setup-race branch from 9d6b98a to 53b072c Compare September 18, 2026 13:41
@Glorf
Glorf marked this pull request as ready for review September 18, 2026 13:56
@Glorf

Glorf commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

/claude review

@Glorf

Glorf commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 53b072c

@copy-pr-bot

copy-pr-bot Bot commented Sep 18, 2026

Copy link
Copy Markdown

/ok to test 53b072c

@Glorf, there was an error processing your request: E2

See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/

@claude

claude Bot commented Sep 18, 2026

Copy link
Copy Markdown
Contributor

SHIP — no reliability concerns.

Reviewed the venv-setup concurrency and quoting rework end to end.

Concurrency / locking (_venv_setup.py) — correct. The flock(LOCK_EX) is held across the whole install and the descriptor is inherited by the installer via pass_fds, so killing the wrapper cannot let a second setup overlap a surviving installer. The marker is unlinked before install and only touch()ed after both subprocess.run success and the required_files existence check, all still under the lock — fail-closed, no window where the lock releases before the marker is written. Signal-exit translation (128 - returncode when returncode < 0 → 128 + N) is right.

TOCTOU fix — deferring the skip_if_ready readiness check into the locked setup process (rather than at command-generation time) closes the real race where a parallel server created the venv but was still installing. The deferred-manifest branch (ready venv needs no pyproject.toml/requirements.txt) preserves prior behavior and is covered by test_manifest_is_required_only_when_setup_is_needed.

Quoting — shlex.quote/shlex.join on python-version, venv path, activate path, and dir_path fixes shell-splitting for paths/versions containing spaces. All four env.py call sites (432, 783, 918, 1384) compose correctly with the new trailing && source <activate>.

Tests — exercise the interrupt/kill concurrency path with real subprocesses, signal handling, marker invalidation+retry, and the deferred-manifest branch. Real behavior, not pass-through. .gitignore lock entry matches the generated .venv.setup.lock name.

@Glorf

Glorf commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 31d150e

@Glorf

Glorf commented Sep 18, 2026

Copy link
Copy Markdown
Contributor Author

@bxyu-nvidia could you take a look?

@yaoyu-33 yaoyu-33 added area:cli Command-line interfaces and user-facing command behavior bug Something isn't working complexity:medium Single-domain change with interacting parts or a moderate review surface needs-review PR is ready for code review and waiting on a reviewer labels Sep 18, 2026
@bxyu-nvidia
bxyu-nvidia added this pull request to the merge queue Sep 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/35375138185

Merged via the queue into main with commit 399e678 Sep 18, 2026
45 checks passed
@bxyu-nvidia
bxyu-nvidia deleted the mbien/fix/venv-setup-race branch September 18, 2026 17:48

This branch was previously deployed

1 inactive deployment
public — 31d150ea Deployed Sep 18, 2026 by copy-pr-bot[bot] via release / finalize / notify #3157
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:cli Command-line interfaces and user-facing command behavior bug Something isn't working complexity:medium Single-domain change with interacting parts or a moderate review surface needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants