Repository navigation
fix(sandbox): retry e2b calls a just-created sandbox answers as gone - #5460
hemildesai wants to merge 9 commits into
Conversation
An AgentEnv gateway can answer 404 "sandbox assignment not found" for a running sandbox in its first seconds (a node heartbeat briefly drops the new route), and a new VM can refuse connections for a few seconds during a create wave (envd 502). The provider took both as final: file calls failed, status() said STOPPED, and close() treated the sandbox as already gone, so it kept running until its TTL. For operations.new_sandbox_grace_s (default 15 s) after create, retry these answers for file reads and writes and for close. Command starts retry only the 404, which the gateway sends before the sandbox gets the request; a 502 can come after the command started. status() reports UNKNOWN instead of STOPPED during the grace period. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hemil Desai <[email protected]>
|
/ok to test bd0947d |
…ilience Resolve status() against #3757's control-plane get_info: a not-found answer still reads as UNKNOWN while the sandbox is new. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hemil Desai <[email protected]>
|
/ok to test 3ca9db2 |
|
/review model=claude |
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
SHIP WITH CARE
One RISK: command-start grace retries can exceed the configured exec wait budget.
I reviewed all four changed files and traced the affected command, file-transfer, configuration, and lifecycle paths. This was static inspection; I didn't execute tests or inspect the external SDK implementation. The delegated command review was cancelled, so I inspected that topic directly.
Review generated by codex. This was a full review. Request a full review with /review reset=true.
A command start retried during the new-sandbox grace period could keep dispatching for the whole grace period and then start with the full original timeout. Bound the retries by the exec deadline: sleep no past it, send nothing once it has passed (raise TimeoutError), and give a retried start only the remaining wait and stream-open budget. Background reattaches share the same deadline. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hemil Desai <[email protected]>
|
/ok to test ab4e013 |
|
/review model=claude |
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
SHIP
LGTM, no concerns found. This head fixes the earlier RISK about command-start retries running past the exec wait budget.
What I checked for the prior finding
execnow sets one deadline fromtimeout_sand passes it to both exec modes.- On a retry,
_start_commandsleeps no longer than the time left. It then rebuilds the call with_remaining_budget. Once the budget is used up, it raisesTimeoutErrorinstead of sending another start. - I traced this with the real 0.5 s retry interval and a 15 s grace period:
- With
timeout_s=1, no start goes out at or after the deadline. - A start retried after 1 s of a 10 s budget gets
timeout=9andrequest_timeout=9. - A later background reattach uses the same deadline.
- With
- The other cases the fix had to keep still work:
timeout_s=0and effectiveNonestay untimed. The deadline expression isNonewhen the timeout is falsy.- A 502 command start is still not retried.
- File retries still depend only on the grace period.
- The new fake-clock tests cover both modes (test_e2b_provider.py L1197-L1245).
- The existing reattach tests still check these cases (L658-L716):
- a 55 s reconnect after a stream is lost at 5 s;
- no reattach after the deadline;
- exit code 7 with both output fragments kept.
Follow-up scope
This follow-up only covers changes since 3ca9db2. I compared them with the tree from the previous review:
_run_backgroundreattach refactor: the reconnect budget matches the baseline for untimed,request_timeoutNone/0 and positive cases. The tests that patchmonotonicwith a fixed list of values still get the right number of clock reads._retry_while_new: theinclude_unreachableparameter is gone. Its only remaining callers arewrite_fileandread_file, and both already passedTrue.- Docs: the new paragraph in e2b.mdx matches the code.
- Unchanged since the last review:
create,close,status, the grace bookkeeping ande2b.yaml.
None of these changes adds a defect above the moderate impact ceiling from earlier reviews.
Limitations
- I only read the source. I didn't run the tests, and the E2B SDK isn't mounted, so its timeout handling comes from the provider code and test fakes.
- I couldn't search the whole repository for outside callers of the private
_run_background, whose signature changed. - One delegated reviewer's report didn't reach me, so I traced its topic, the command-start retry path, myself.
Review generated by claude. This was a findings-only review. Request a full review with /review reset=true.
…ilience Resolve the E2B docs page against #3810: keep the new-sandbox grace paragraph before the control-plane status note and the new Pause and Resume section. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hemil Desai <[email protected]>
|
/ok to test b6182bd |
…ilience Resolve the E2B config and docs page against #5607: keep operations.new_sandbox_grace_s and the new attribution block. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Hemil Desai <[email protected]>
|
/ok to test 512dd4c |
…ilience Clean merge; #5589 moved test_e2b_provider.py to tests/unit_tests/sandbox/. Signed-off-by: Hemil Desai <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
/ok to test 8b718d7 |
…ilience Signed-off-by: Hemil Desai <[email protected]> Co-Authored-By: Claude Fable 5.2 <[email protected]>
On an AgentEnv cell, a guest restored from an image that its node has not cached can refuse connections for 5 to 15 s after create returns. The node proxy gives up after 5 s and answers 502, so the first setup command failed in about 4% of sandboxes in large SWE RL runs. A command start is not retried on a 502, because the command may have been delivered. Create now waits, inside operations.new_sandbox_grace_s, until the sandbox answers envd's health check. That request is safe to repeat. The wait ends with the grace period or on any other error, and a create cancelled during the wait kills the sandbox. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Hemil Desai <[email protected]>
|
/ok to test ec5e1cc |
|
/review model=claude |
svcnvidia-nemo-ci
left a comment
There was a problem hiding this comment.
SHIP
LGTM, no concerns found. This head has no unresolved earlier findings, and nothing added since the last bot review reaches the impact level this follow-up reports.
Earlier findings
- The latest bot review at
ab4e0139had no findings. Before that, a review raised a RISK that command-start retries could outlast the exec wait budget. That review confirmed the fix. - At this head,
_start_commandstill has the same deadline-bounded sleep, applies_remaining_budgetto each retry and raisesTimeoutErroronce the time is used up.execstill sets one deadline for both exec modes.
What changed since the last review
- Create now waits for a health check (
ec5e1cc). I traced the new code increateand_wait_until_reachable:- The loop is bounded by the grace period, and each
is_runningcall gets_request_params(). - Health-check errors other than the 404 or 502 end the wait without failing create.
- With
new_sandbox_grace_s: 0, no health check is sent. - Only a
BaseExceptionsuch as a cancellation reaches the cleanup handler. That handler drops the_new_untilentry, makes one best-effortkilland re-raises the original exception.
- The loop is bounded by the grace period, and each
- Callers of create.
AsyncSandbox.startandstart_with_setupinnemo_gym/sandbox/api.pyput no timeout aroundprovider.create. The tightest outer deadline I found is Harbor's ready deadline, which defaults to at least 915 s. The added wait is at most the 15 s grace period plus one in-flight health check, so creates that worked before won't start timing out. - Merges from main. Compared with the base tree, the merges keep the attribution metadata in
create,pause/resume, the extension-params create path and the docs' Pause and Resume section. The only other changes tostatusandcloseare the grace-period additions. - Tests, config and docs. I checked the following:
FakeSandbox.is_runningreports a running sandbox by default.- The new create-wait tests cover the wait, the end of the grace period, other health-check errors, grace 0 and cancellation.
- The updated
monotonicsequences in the reattach tests match the extra clock reads by create and the health wait. new_sandbox_grace_sis at the right indentation underoperationsin e2b.yaml.- The new docs sentences in e2b.mdx match the code.
This follow-up only reports new issues above the moderate impact level set by earlier reviews. None of these changes reaches it.
Limitations
- I only read the source and didn't run the tests.
- The E2B SDK isn't mounted, so I couldn't confirm how
is_runningbehaves on a 502 or a timeout. That behavior comes from the test fakes and the PR description. - I read the main callers of
createbut couldn't search every agent and resources server for tighter timeouts around sandbox start. - Two of the three delegated reviewers didn't send me reports: the one for the create-wait internals and the one for tests, config and docs. I checked both topics myself, as described above.
Review generated by claude. This was a findings-only review. Request a full review with /review reset=true.
Review generated by claude. This was a findings-only review. Request a full review with /review reset=true.
…ilience main (#5410) added its own new-sandbox grace period: `_start_grace` and `_in_grace`, a kill that is repeated, status UNKNOWN, and an opt-in reachability probe (`create.reachable_timeout_s`). This branch keeps only what main does not have, on main's helpers: - a command start the gateway did not route is sent again, inside the command's deadline; - file calls are retried on the unrouted 404 and on a 502; - create waits for a health check inside the grace period when `create.reachable_timeout_s` is unset. A cancelled create is cleaned up by main's handler. A command whose start is never routed before its deadline now reports a timeout (exit 124), as main reports every timeout at the deadline. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Hemil Desai <[email protected]>
Summary
During full-scale SWE RL runs on an AgentEnv cell (about 1,024 E2B sandboxes in flight), two errors came from sandboxes that were running and only seconds old:
sandbox assignment not found: the AgentEnv gateway answers this when a node heartbeat briefly drops a new sandbox's route. The route comes back with the next heartbeat (about 5 s). The request never reaches the sandbox.The provider treated both as final. File calls failed.
status()returnedSTOPPED.close()decided the sandbox was already gone, so the sandbox kept running until its TTL.This PR adds
operations.new_sandbox_grace_s(default 15 s). For that long aftercreate(), the provider:timeout_s: a retry gets only the time left, and none is sent after the deadline. The gateway sends the 404 before routing, so the command did not start. A 502 can come after the command started, so a retry could run it twice. The 502 still fails as before;create()until the sandbox answers envd's health check, so the first command does not meet a sandbox that is still starting. A health check is safe to repeat; a command start on a 502 is not. This costs one extra request for each create. A create cancelled during the wait kills the sandbox;close()while kill reports not found, so the new sandbox is not leaked;UNKNOWN, notSTOPPED, fromstatus()when the control plane answers not found for a new sandbox.After the grace period, behavior is unchanged. Set the value to
0to turn this off.Also updated: the bundled
e2b.yamlconfig and the E2B provider docs page.The gateway-side fix for the 404 race is a separate upstream AgentEnv change. This PR keeps Gym correct against gateways that still have the race.
Issue: none filed. The cause comes from an investigation of AgentEnv gateway, scheduler and node logs.
Update, Nov 10: 502s on new sandboxes are common in large runs
Node logs from an AgentEnv cell for Nov 5-10 (685k creates) show 11,419 of these 502s. All are the node proxy's 5 s connect timeout on a guest that was restored from an image its node had not cached, during a create burst. They hit 3.9% of the sandboxes of the SWE RL runs. None comes earlier than 5.2 s after the sandbox is ready, 93% come within 15 s, and nearly every sandbox has only one. The create wait added here covers that window with a request that is safe to repeat.
Not in this PR
The same report had three more issues. None of them has a Gym client fix:
invalid state Killingon timeout renewal, and a 60 s timeout on an unfiltered sandbox list. Gym's E2B provider neither renews timeouts nor lists sandboxes.exec.request_timeout_s, default from the SDK). A safe automatic retry needs proof that the command did not start, and the SDK cannot give that.Test plan
pytest tests/unit_tests/sandbox/test_e2b_provider.py: 173 passed (after mergingmain). That includes 20 newTestNewSandboxGracecases. Of the first 13, 11 fail onmainand 2 are negative cases that guard behavior that must not change. Of the 7 added for the create wait, 6 fail without the wait and 1 is a negative case.pytest tests/unit_tests/sandbox/test_e2b_*.py tests/unit_tests/agents/test_sandbox_session.py: 309 passed, 3 skipped (after mergingmain).pytest tests/unit_tests/test_public_api.py tests/unit_tests/test_public_api_facades.py: 970 passed.pre-commit run --files <changed files>: passed, including ruff, ruff-format, guardwords and import-linter.create(). They now setnew_sandbox_grace_s: 0, or skip the extramonotonic()call thatcreate()now makes.e2b ... on new sandbox ... retryingINFO lines in large runs.Compatibility: new optional config key with a default; no migration needed.
🤖 Generated with Claude Code