Skip to content

fix(sandbox): retry e2b calls a just-created sandbox answers as gone - #5460

Open
hemildesai wants to merge 9 commits into
mainfrom
hemild/e2b-client-resilience
Open

hemildesai wants to merge 9 commits into
mainfrom
hemild/e2b-client-resilience

Conversation

@hemildesai

@hemildesai hemildesai commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • 404 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.
  • envd 502 ("likely due to sandbox timeout" in e2b 2.46; "could not be reached" in later SDKs): a new VM refuses connections for a few seconds during a create wave.

The provider treated both as final. File calls failed. status() returned STOPPED. 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 after create(), the provider:

  • retries file reads and writes on either error;
  • retries a command start on the 404 only, within the command's 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;
  • waits in 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;
  • retries close() while kill reports not found, so the new sandbox is not leaked;
  • reports UNKNOWN, not STOPPED, from status() when the control plane answers not found for a new sandbox.

After the grace period, behavior is unchanged. Set the value to 0 to turn this off.

Also updated: the bundled e2b.yaml config 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:

  • A 500 invalid state Killing on timeout renewal, and a 60 s timeout on an unfiltered sandbox list. Gym's E2B provider neither renews timeouts nor lists sandboxes.
  • Background command starts that never answered (300 s). Router logs show the requests never arrived. Gym already limits the stream-open wait (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 merging main). That includes 20 new TestNewSandboxGrace cases. Of the first 13, 11 fail on main and 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 merging main).
  • 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.
  • Updated 6 existing tests (and the 2 fake-clock tests again for the create wait) that check after-grace behavior right after create(). They now set new_sandbox_grace_s: 0, or skip the extra monotonic() call that create() now makes.
  • Rollout evidence: N/A in this PR. The change affects only error handling during the first 15 s of a sandbox's life. On the happy path the only extra work is one health request for each create. The failures happen in roughly 1 in 4,000 sandboxes, too rarely to see in a smoke rollout. They will show up as e2b ... on new sandbox ... retrying INFO lines in large runs.

Compatibility: new optional config key with a default; no migration needed.

🤖 Generated with Claude Code

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]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 2, 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.

@hemildesai

Copy link
Copy Markdown
Contributor Author

/ok to test bd0947d

@github-actions

github-actions Bot commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

…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]>
@hemildesai

Copy link
Copy Markdown
Contributor Author

/ok to test 3ca9db2

@hemildesai

Copy link
Copy Markdown
Contributor Author

/review model=claude

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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]>
@hemildesai

Copy link
Copy Markdown
Contributor Author

/ok to test ab4e013

@hemildesai

Copy link
Copy Markdown
Contributor Author

/review model=claude

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • exec now sets one deadline from timeout_s and passes it to both exec modes.
  • On a retry, _start_command sleeps no longer than the time left. It then rebuilds the call with _remaining_budget. Once the budget is used up, it raises TimeoutError instead 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=9 and request_timeout=9.
    • A later background reattach uses the same deadline.
  • The other cases the fix had to keep still work:
    • timeout_s=0 and effective None stay untimed. The deadline expression is None when 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_background reattach refactor: the reconnect budget matches the baseline for untimed, request_timeout None/0 and positive cases. The tests that patch monotonic with a fixed list of values still get the right number of clock reads.
  • _retry_while_new: the include_unreachable parameter is gone. Its only remaining callers are write_file and read_file, and both already passed True.
  • Docs: the new paragraph in e2b.mdx matches the code.
  • Unchanged since the last review: create, close, status, the grace bookkeeping and e2b.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]>
@hemildesai

Copy link
Copy Markdown
Contributor Author

/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]>
@hemildesai

Copy link
Copy Markdown
Contributor Author

/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]>
@hemildesai

Copy link
Copy Markdown
Contributor Author

/ok to test 8b718d7

@svcnvidia-nemo-ci svcnvidia-nemo-ci added bug Something isn't working area:sandbox Sandbox providers, isolation, and sandbox runtime contracts needs-review PR is ready for code review and waiting on a reviewer labels Nov 7, 2026
hemildesai and others added 2 commits November 10, 2026 13:13
…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]>
@hemildesai

Copy link
Copy Markdown
Contributor Author

/ok to test ec5e1cc

@hemildesai

Copy link
Copy Markdown
Contributor Author

/review model=claude

@svcnvidia-nemo-ci svcnvidia-nemo-ci left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ab4e0139 had 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_command still has the same deadline-bounded sleep, applies _remaining_budget to each retry and raises TimeoutError once the time is used up. exec still sets one deadline for both exec modes.

What changed since the last review

  1. Create now waits for a health check (ec5e1cc). I traced the new code in create and _wait_until_reachable:
    • The loop is bounded by the grace period, and each is_running call 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 BaseException such as a cancellation reaches the cleanup handler. That handler drops the _new_until entry, makes one best-effort kill and re-raises the original exception.
  2. Callers of create. AsyncSandbox.start and start_with_setup in nemo_gym/sandbox/api.py put no timeout around provider.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.
  3. 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 to status and close are the grace-period additions.
  4. Tests, config and docs. I checked the following:
    • FakeSandbox.is_running reports 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 monotonic sequences in the reattach tests match the extra clock reads by create and the health wait.
    • new_sandbox_grace_s is at the right indentation under operations in 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_running behaves on a 502 or a timeout. That behavior comes from the test fakes and the PR description.
  • I read the main callers of create but 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]>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the risk:high Autobot PR risk: high label Nov 22, 2026

This branch was previously deployed

1 inactive (outdated) deployment
public — ec5e1cc9 Deployed Nov 10, 2026 by copy-pr-bot[bot] via release / finalize / notify #11590
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:sandbox Sandbox providers, isolation, and sandbox runtime contracts bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer risk:high Autobot PR risk: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants