Repository navigation
feat(sandbox): stop, start and export owned sandboxes safely under load - #6415
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
A client that disconnects cancels its request, and every later await in it. An unshielded stop in the request's cleanup then raises before the provider closes the sandbox, which runs on until its ttl_s. nemo_gym.sandbox.stop_sandbox(), in the sandbox package's utils, shields the stop from that cancellation and bounds it in time, so a hung provider cannot hold the request either. A timed-out stop raises TimeoutError, which owners treat like any other failed stop. It also accepts a Compose group, which harbor owns. LentSandboxes, the swebench sandbox session base and deepswe_external1 each had a copy of this stop; they now use the shared one. Signed-off-by: Ananth Subramaniam <[email protected]>
Every sandbox has one owner, the server that starts it, but each server
family owns its sandboxes through its own design. Gym already has a class
that tracks a server's sandboxes by key, stops them, lends them with operate
scope and snapshots them for checkpoints: SandboxSessionCheckpointer, which
no server uses yet.
It becomes the public nemo_gym.server_runtime.OwnedSandboxes, in its own
module (nemo_gym/server_runtime/owned_sandboxes.py) since it is no longer
checkpoint-only, and every SimpleServer holds one as self.sandboxes:
- created on first use from the server's sandbox_provider and
checkpoint.state_dir, so a server without sandboxes builds nothing
- self.sandboxes.on("<field>") gives the instance for a second provider
field, with its own server name for the snapshot index
- run_webserver wraps the app's lifespan so the shutdown stops what every
instance still owns, after the server's own shutdown code
- access() lends through the provider block the instance was built from
- the instance owns the provider it creates and closes it once every
sandbox is stopped; it reads the config the server was started with
- the shutdown sweep runs after the server's own shutdown, is bounded
(150 s in all), refuses to build new instances once it began, and is
documented next to server_shutdown_timeout_seconds
The glossary gains "sandbox owner" and "lend / borrow", and its existing
"Owner" (the worker that holds a session) points away from them.
The class is renamed directly without an alias: it merged on 2026-11-16 and
is in no release.
Signed-off-by: Ananth Subramaniam <[email protected]>
OwnedSandboxes becomes the one path for every sandbox start and stop, so it takes on the jobs that servers copied into their own session code: - One stop: stop() is shielded from the caller's cancellation and bounded by STOP_TIMEOUT_S from the moment it is called, its waits for the key's lock and for a stop slot included, so a client that disconnects cannot leave the sandbox running and nothing can hold the stop forever. A stop that fails or times out keeps the key, as before. - Tracking before start: create() tracks the sandbox before it starts. A start that fails or is cancelled after the provider returned the sandbox is stopped, shielded. If that stop fails too, the key stays tracked for the next stop or aclose(), an export leaves the half-started sandbox out, and create() raises the start's error with the stop's as its context. AsyncSandbox.start now keeps the start's error the same way when its own rollback stop fails. A start cancelled before the provider returned the sandbox leaves nothing to stop; ttl_s reclaims it. - Their own start and stop limits: parallelism (16 by default) bounded creates, stops, snapshots and deletes alike, which would let a server seeding 512 episodes start only 16 sandboxes at a time. Starts and forks now take max_concurrent_starts and stops take max_concurrent_stops, both unbounded by default and set per provider with the block's owner_concurrency, so a stop never waits behind starts. parallelism bounds only snapshot submits and deletes. - Exclusion per key: a create, a stop, an export and a restore of one key hold that key's lock. An export snapshots a sandbox only once its start finished, a stop waits for the export or restore of its key and then stops what is there, and other keys go on. A restore takes its keys' locks in sorted order. A key's lock exists only while someone holds or waits for it. Signed-off-by: Ananth Subramaniam <[email protected]>
edb1e8f to
777b9a0
Compare
Resolve against main: - Drop this branch's earlier copy of the shielded stop (stop_sandbox() in nemo_gym.sandbox.utils and its test): #6410 merged a different version that puts the shield and limit in AsyncSandbox.stop(timeout_s=...). The sandbox package, LentSandboxes, the swebench session base and deepswe_external1 take main's code; OwnedSandboxes stops through AsyncSandbox.stop. - #5266 exports the checkpointer under its old name: its server, conformance helper and test move to OwnedSandboxes in owned_sandboxes, and the public API serves SandboxCheckpointConfig, SandboxCheckpointError and checkpoint_state_dir from there. No release shipped the old name. - The glossary keeps main's sandbox lease and capabilities rows beside the owner and lend/borrow rows. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
Brings in origin/main through the parent. Keeps this branch's stop: shielded and bounded as a whole, the wait for a stop slot included. It passes STOP_TIMEOUT_S to AsyncSandbox.stop, whose own shield (#6410) would otherwise hold a stop for its 120 s default past a shorter configured limit. The superseded-sandbox stop does the same. STOP_TIMEOUT_S now comes from nemo_gym.sandbox.api. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
OwnedSandboxes.for_server created the provider before it read checkpoint.state_dir, so a refused config (checkpointing on without a state_dir) left a provider nobody closes, and every later self.sandboxes built another. The provider block and state_dir are now resolved first. An owner on the local provider, which runs commands as host processes, logs a warning. The self.sandboxes docstring and the glossary say what is true today: no server owns its sandboxes through it yet, it is used from the event loop, and a borrower is an agent or a Resources Server grading in the sandbox. The test imports names from their defining modules. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
create() let a cancellation cut the provider's start short and relied on ttl_s to reclaim what the provider had made. Not every provider applies ttl_s: on Enroot a create cancelled half a second in left its container on the node with nothing tracking it (seen in a lifecycle smoke). The start now runs to its end, shielded and bounded by STOP_TIMEOUT_S, so the create's rollback has a handle to stop; only a start still running after that is cut short. A main test of a stop that waits for its turn held the snapshot semaphore, which stops no longer take since they got their own limit; it now holds the stop limit. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
- stop() took STOP_TIMEOUT_S for its key's lock, again for a stop slot, and AsyncSandbox.stop, which shields itself from enclosing deadlines since #6410, took a third: up to three limits in all. One deadline now covers the whole stop, and the provider's stop gets only the time that is left. - create() refuses a spec without ttl_s: the shutdown sweep is bounded and a crash runs none, so ttl_s is what ends a sandbox its owner never stops. - access() lends only a running sandbox. A key is tracked from before its start, so a key still starting, or one whose start and stop both failed, raised a RuntimeError from the sandbox, or lent a broken one. - An instance built with from_config, as swebench's checkpoint server builds it, keeps parallelism as the bound of its starts, forks and stops. They bounded them before they got limits of their own, and such a server has no owner_concurrency to set. swebench's close test holds the stop limit, which stops take now, instead of the snapshot semaphore. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
… after close - A stop waiting for a stop slot no longer counts that wait against STOP_TIMEOUT_S. Every stop ahead holds its slot for at most that long, and cutting queued stops short under load (1000 sandboxes, 16 slots) left most of them running until their ttl_s. The wait for the key's lock and the provider's stop are each bounded by STOP_TIMEOUT_S, without the extra nested limits the previous commit removed. - A create queued for a start slot when aclose() began now raises instead of starting a sandbox after the shutdown sweep has begun. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
|
/ok to test ddaaf9c |
|
Review of 777b9a0. The stack-wide points (restack onto 1. Medium: Scenario: a session close arrives while its key is being exported, and the provider then hangs. The close handler is held for 240 s where the docstring and the docs say 120 s. The shutdown sweep is not affected, since Suggested fix: take one deadline in 2. Medium-low: a key is visible before its sandbox has started, and a half-started sandbox can be lent. Confirmed with a probe. Scenario: a tool call that does Suggested fix: one "ready" flag on the entry in place of 3. Low: 4. Low, for the restack. The new tests assert real behavior and cover cancellation and ordering well. What I ran on 777b9a0
|
Signed-off-by: yaoyu-33 <[email protected]>
Re-review at e61be51Tracking issue: #6703 I reread the commits pushed since my last comment and reran the probes and the live check on this head. Addressed
New findings
Still open, by design
Live check on this head: 16 of 16 pass against OpenSandbox (stop under cancellation, a create cancelled or failed mid-start, |
…and others borrow it The owner description named a Resources Server as a lender of the attempt's sandbox on equal terms with the Environment Server. The Environment Server owns the sandbox an attempt runs in and lends it to the agent and to the Resources Server; a Resources Server owns a sandbox only for a grader that runs code in an attempt without one. Today's Resources lenders and agent-owned harness sandboxes are described as what moves onto it. An export also waits for borrowers' commands, not only the owner's. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: yaoyu-33 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]> # Conflicts: # fern/versions/latest/pages/about/concepts/key-terminology.mdx
|
/ok to test eca95f5 |
Resolve an explicitly configured sandbox_provider while loading the server config so an absent named block stops launch before a rollout is dispatched. Preserve defaults used by optional backends and keep provider construction lazy. Report only the type when a reference points at a scalar setting. Signed-off-by: yaoyu-33 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
|
/ok to test cd513a6 |
|
Addressed the three findings in the re-review in cd513a6.
On the workstation, the five affected sandbox, checkpoint and SWE-bench test modules finished in 6.62 seconds with 333 passed and 3 skipped. Full pre-commit and all 30 documentation-link tests passed. Fern reported zero errors and skipped its authenticated missing-redirects check locally. A real Docker smoke cancelled a blocked upload, started a second sandbox through the single start slot, executed a command there and verified that cleanup removed both containers. |
Allow standalone server schemas to be inspected before the operator composes a provider block. Check the reference before launching a worker or returning from a dry run. Exercise both the schema-only load and actual startup with the same configuration. Signed-off-by: yaoyu-33 <[email protected]>
Signed-off-by: yaoyu-33 <[email protected]>
|
/ok to test 3a0fda7 |
Third review round at 3a0fda7Tracking issue: #6703 I read the commits pushed since my last comment and reran the probes and the live check on this head. All three findings from my last comment are fixed, and I have no new finding here. Addressed
Still open, by design
State charts. I rewrote the TLA+ model of one key for this head and checked it again with TLC (15 states). Five rules hold in every reachable state, including a new one: a cancelled request never ends with a sandbox ready to lend. The one exception is the documented Live check on this head: 16 of 16 pass against OpenSandbox, and no sandbox was left. |
hemildesai
left a comment
There was a problem hiding this comment.
Approving at 3a0fda7. All three findings from my last comment are fixed and confirmed by probe, the live OpenSandbox check passes (16 of 16), and the TLA+ model of one key, rewritten for this head, holds every rule but the documented ttl_s case. Details are in my comments above and in #6703.
Still open by design, not blocking: stop() has two limits in sequence.
The failing CI checks are the ones marked non-blocking: the mini-SWE sandboxed conformance probe fails wherever it currently runs, including on main and on other open PRs, and the Environment Server compliance audit reports findings on agents this PR does not change, as it does on other open PRs.
Signed-off-by: svcnemo-autobot <[email protected]>
|
/ok to test 07c88fe |
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/45645932035 |
Keep owned sandboxes tracked through starts, cancellation, cleanup failures, and checkpoint operations under load.
create,stop,export,restore, anddiscardserialize on each key.discardwaits for an in-flight create before detaching its sandbox, avoiding a late provider reply that escapes cleanup.keys;get()and membership expose only successfully started sandboxes, after task files finish uploading. SWE-bench's cleanup and export preflight use the tracking keys where readiness is irrelevant.owner_concurrency; snapshot submits/deletes retainparallelism. A stop does not queue behind starts.The stop lock wait and provider stop each have their own timeout; waiting for a stop-concurrency slot is unbounded. Independent limits can admit more aggregate backend work than the previous shared limit. A provider create that outlasts cancellation settlement still relies on provider TTL/lease behavior; providers without TTL enforcement retain that limitation. These contracts are documented and tracked in #6703.
Stack
Review bottom to top: #6414 → #6415 → #6416 → #6417. Each PR targets the preceding branch; #6414 targets
main. The stack includesmainthroughd80581feb. Follow-up adoption and remaining lifecycle limits are tracked in #6703.Validation
On the workstation, at
1b3a30607:pre-commit run --all-files: passed.pytest tests/unit_tests/server_runtime/test_owned_sandboxes.py tests/unit_tests/server_runtime/test_checkpoint_sandbox.py tests/unit_tests/sandbox/test_sandbox.py resources_servers/swebench/tests/test_sandbox_session_checkpoints.py -n 4 --tb=short --import-mode=importlib: 318 passed, 3 skipped in 126.15s (0:02:06).pytest resources_servers/deepswe_external1/tests/test_sessions.py -n 4 --tb=short --import-mode=importlib: 21 passed.pytest resources_servers/<server>/tests -n 4 --tb=short --import-mode=importlibruns foragentic_mme,browsecomp_advanced_harness, andlitmus_agent: passed (Agentic MME: 176 passed; BrowseComp: 441 passed; Litmus: 112 passed).Preparation commits include DCO sign-off. Model rollout: N/A for ownership bookkeeping and error preservation; no new model-facing harness or verifier policy is introduced. The real Docker smoke in #6417 exercises cancellation settlement, discard/create ordering, and shutdown cleanup. Cloud-provider settlement tails are covered by controlled tests, without a new live cloud run.