kill sandbox on MCP gateway startup failure to prevent orphaned resources#1499
kill sandbox on MCP gateway startup failure to prevent orphaned resources#1499AdaAibaby wants to merge 1 commit into
Conversation
|
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
Hey there, thanks for the contribution! |
|
Hi @AdaAibaby, I noticed this PR has been waiting on integration-test coverage since June 29. I'd be happy to take over the remaining work, replace the mocked tests with integration tests, and open a superseding PR while preserving attribution for your original implementation. @mishushakov, would that approach work for you if the original author isn't planning to continue? |
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:
