Skip to content

fix: terminate orphaned microVM when hydrate fails during provision - #871

Open
tanmaybansal22 wants to merge 1 commit into
yc-software:mainfrom
tanmaybansal22:fix/aws-sandbox-orphaned-microvm-on-hydrate-failure
Open

fix: terminate orphaned microVM when hydrate fails during provision#871
tanmaybansal22 wants to merge 1 commit into
yc-software:mainfrom
tanmaybansal22:fix/aws-sandbox-orphaned-microvm-on-hydrate-failure

Conversation

@tanmaybansal22

@tanmaybansal22 tanmaybansal22 commented Aug 31, 2026

Copy link
Copy Markdown

Summary

ensureBody() in src/sandbox/aws-sandbox.ts registers a freshly launched microVM in endpointById, scopeByMicrovm, and the client's tokenById before store.put() runs. If hydrateHome() throws (e.g. a transient 502 on the agent-home.tar write to the microVM), store.put() never executes.

That VM is then unreachable by every cleanup path:

  • teardown() never runs — no SandboxHandle was ever returned.
  • reapDeepIdle() can't find it — it only iterates store, which never got the entry.

The VM keeps running on AWS, permanently orphaned, and its in-process map entries leak for the life of the process. Worse, since store has nothing for that scope, the next provision attempt for the same scope calls launchBody() again — launching and leaking a fresh VM on every retry instead of reusing or cleaning up the failed one.

Also added the missing client.evict() call in the rotation branch and in reapDeepIdle(), which terminate the microVM via api.terminate() but never evicted its cached auth token, leaking a tokenById entry on every routine rotation/reap.

Test plan

  • Added a regression test in test/aws-sandbox.test.ts: simulates a transient 502 on the agent-home.tar write during a relaunch, and asserts the newly launched microVM is terminated (not orphaned) and a subsequent provision succeeds cleanly.
  • Confirmed the new test fails on main (19 pass / 1 fail, failing on the terminate-not-called assertion) and passes after this fix (20/20).
  • node --experimental-test-module-mocks --test test/aws-sandbox.test.ts test/aws-microvm-api.test.ts — 20/20 pass.
  • npx tsc --noEmit — clean.

View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

ensureBody() registered a freshly launched microVM in endpointById/
scopeByMicrovm/tokenById before store.put() succeeded. If hydrateHome
threw (e.g. a transient 502 on the agent-home.tar write), store.put()
never ran, so the VM was unreachable by both teardown() (no
SandboxHandle was ever returned) and reapDeepIdle() (only iterates
store). The VM kept running on AWS and its map entries leaked forever;
each retry launched and leaked another one.

Also add the missing client.evict() call in the rotation and
reapDeepIdle terminate paths, which had the same token-map leak.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant