Skip to content

fix(sdk): clean up sandbox when MCP gateway startup fails - #1547

Merged
mishushakov merged 1 commit into
e2b-dev:mcp-gateway-startup-cleanupfrom
hxaxd:codex/issue-1498-mcp-gateway-cleanup
Jul 13, 2026
Merged

mishushakov merged 1 commit into
e2b-dev:mcp-gateway-startup-cleanupfrom
hxaxd:codex/issue-1498-mcp-gateway-cleanup

Conversation

@hxaxd

@hxaxd hxaxd commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Problem

Fixes #1498.

Sandbox.create allocates a remote sandbox before starting mcp-gateway. If gateway startup returns a non-zero exit code or throws an exception, creation fails before the sandbox object is returned. As a result, the caller has no sandbox ID to clean up, and the orphaned sandbox continues consuming resources until it times out.

This state transition exists in synchronous Python, asynchronous Python, and JavaScript/TypeScript.

Changes

  • Add a rollback boundary around MCP gateway startup in all three SDK implementations.
  • Attempt to kill the newly allocated sandbox when gateway startup fails.
  • Keep cleanup best-effort so a failed or cancelled kill does not replace the original gateway error.
  • Add real integration coverage for synchronous Python, asynchronous Python, and TypeScript.
  • Add a patch changeset for e2b and @e2b/python-sdk.

Usage Behavior

No API changes are required. Existing code continues to receive the original startup error, but a failed creation will normally no longer leave a sandbox behind.

Validation

Integration tests have been added and verified successfully.

Notes

PR #1499 is a friendly competing implementation for the same issue. The maintainer requested that its mocked tests be replaced with real integration tests, but the PR received no further updates for several days. The maintainer subsequently confirmed that an alternative contribution could be submitted as long as it was implemented independently from scratch, so this PR was prepared only after that confirmation.

Copilot AI review requested due to automatic review settings July 13, 2026 16:30
@cla-bot

cla-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

We require contributors to sign our Contributor License Agreement, and we don't have @hxaxd on file. You can sign our CLA at https://e2b.dev/docs/cla . Once you've signed, post a comment here that says '@cla-bot check'

@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e71a19b

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
e2b Patch
@e2b/python-sdk Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copilot AI 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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hxaxd

hxaxd commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

@cla-bot check

@cla-bot cla-bot Bot added the cla-signed label Jul 13, 2026
@cla-bot

cla-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

The cla-bot has been summoned, and re-checked this pull request!

@mishushakov
mishushakov changed the base branch from main to mcp-gateway-startup-cleanup July 13, 2026 16:37
@mishushakov
mishushakov merged commit 16390cf into e2b-dev:mcp-gateway-startup-cleanup Jul 13, 2026
1 of 2 checks passed
@hxaxd
hxaxd deleted the codex/issue-1498-mcp-gateway-cleanup branch July 13, 2026 17:00
mishushakov added a commit that referenced this pull request Aug 7, 2026
## Problem

Fixes #1498.

`Sandbox.create` allocates a remote sandbox before starting
`mcp-gateway`. If gateway startup fails, creation throws before the
sandbox object is returned. As a result, the caller has no sandbox ID to
clean up, and the orphaned sandbox continues consuming resources until
it times out.

This state transition exists in synchronous Python, asynchronous Python,
and JavaScript/TypeScript.

## Changes

- Add a rollback boundary around MCP gateway startup in all three SDK
implementations: on failure, best-effort kill the newly allocated
sandbox, then re-raise.
- Surface gateway startup failure as `SandboxError` (JS) /
`SandboxException` (Python) with a `Failed to start MCP gateway:
<stderr>` message. Previously the intended message was unreachable dead
code — foreground `commands.run` already throws on non-zero exit — so
callers got a bare `CommandExitError`/`CommandExitException`.
- In async Python, re-raise `asyncio.CancelledError` from the
best-effort `kill()` so caller cancellation (e.g. `asyncio.timeout`) is
honored; only ordinary cleanup failures are suppressed and never mask
the original error.
- Add integration coverage for synchronous Python, asynchronous Python,
and TypeScript. The tests pin the sandbox to the base template (which
has no `mcp-gateway` binary) so gateway startup genuinely fails after
allocation.
- Add a patch changeset for `e2b` and `@e2b/python-sdk`.

## Usage Behavior

No API changes. A failed creation no longer leaves a sandbox behind, and
the error is now descriptive:

```ts
try {
  const sandbox = await Sandbox.create({ mcp: { ... } })
} catch (err) {
  // err is SandboxError: "Failed to start MCP gateway: <stderr>"
  // the allocated sandbox has already been killed — no orphan is left running
}
```

## Validation

All three integration tests verified against real infra: creation
rejects with the documented error and no sandbox remains.

## Notes

Supersedes #1547 by @hxaxd (squash-merged into this branch to preserve
attribution).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: 苏紫辰 <[email protected]>
Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MCP gateway startup failure leaves orphaned sandbox running

3 participants