Preserve hibernated MCP streams during cold restore - #1538
Open
jarell-cheong wants to merge 1 commit into
Open
Conversation
PartyServer can restore a Durable Object's WebSockets before onStart rebuilds Executor's in-memory MCP runtime. The existing cleanup path closed those restored response streams even though there was no prior runtime to replace, which could leave the startup block waiting on its own stream teardown until Cloudflare reset the object. Only close active streams when onStart is replacing live in-memory runtime state. Keep the existing cleanup behavior for warm restarts and failed starts, and cover both lifecycle paths with regression tests.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cloudflare MCP sessions can fail during a Durable Object cold restore with HTTP 500 and error 1101:
What happens
PartyServer restores hibernated WebSockets before its
onStarthook rebuilds Executor's in-memory MCP runtime.startRuntimeFromOnStartalways calledcloseRuntime, andcloseRuntimealways closed every active connection.In a cold isolate, there is no previous runtime to replace. This closes the restored response stream that triggered startup while PartyServer is still inside its startup concurrency block.
In repeated fresh-session tests against a Cloudflare Workers deployment, the unpatched build failed 2 of 5
skillscalls after approximately 30 seconds. Durable Object logs showed the request waiting until Cloudflare reset the object for theblockConcurrencyWhiletimeout.Fix
Detect whether the isolate has live in-memory runtime state before cleanup. A cold restore still releases stale in-memory resources, but it preserves hibernated response streams when there is no prior runtime. Warm restarts and failed-start cleanup continue to close streams.
Regression tests cover both paths:
Verification
bun run test src/mcp/agent-session-durable-object.test.tsinpackages/hosts/cloudflare: 11/11 tests passedbun run testinpackages/hosts/cloudflare: 63/63 tests passedbun run typecheckinpackages/hosts/cloudflarebun run typecheckinapps/host-cloudflarebun run typecheckinapps/cloudbunx vitest run --project cloudflare scenarios/browser-approval.test.tsine2e: 2/2 tests passedbun run format:checkbun run lintbun run typecheck: 44/44 packages passed