Skip to content

kill sandbox on MCP gateway startup failure to prevent orphaned resources#1499

Open
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:fix/cleanup-sandbox-on-mcp-gateway-failure
Open

kill sandbox on MCP gateway startup failure to prevent orphaned resources#1499
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:fix/cleanup-sandbox-on-mcp-gateway-failure

Conversation

@AdaAibaby

Copy link
Copy Markdown

fix #1498

Problem

When Sandbox.create() is called with an mcp configuration, the sandbox is created first, then the MCP gateway is started inside it. If the gateway startup fails (non-zero exit code or exception), the error is raised but the sandbox is never cleaned up, leaving an orphaned sandbox running and consuming resources until it times out.
This issue impacts all three SDK implementations:
Python async: AsyncSandbox.create
Python sync: Sandbox.create
JS/TS: Sandbox.create

Fix

Wrap MCP gateway startup logic with try/catch:
On any gateway startup failure, run best-effort sandbox.kill() before re-raising the original error.
Catch errors from kill() separately to guarantee the original failure error is always propagated to the caller.

Code Changes

main.py: Add async cleanup with await sandbox.kill()
main.py: Add sync cleanup with sandbox.kill()
index.ts: Add cleanup logic await sandbox.kill().catch(() => {})
test_mcp_gateway_cleanup.py: 14 unit tests (Python async + sync)
mcpGatewayCleanup.test.ts: 7 unit tests (TS)

Test Coverage

Total 21 new unit tests, all scenarios verified:
image

@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 0e15e09

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Credits must be used to enable repository wide code reviews.

@mishushakov

Copy link
Copy Markdown
Member

Hey there, thanks for the contribution!
Could you please update the tests so they are integration tests instead of mock?

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