Repository navigation
Cancel a session's open calls when its client deletes it - #7341
Conversation
The transport answers DELETE by closing the go-sdk session, which waits for in-flight requests without cancelling them. A Legacy tools/call waiting on a backend held the DELETE for the backend request timeout plus the backend client's own 5s DELETE, 24 to 34 seconds in practice, and past the write timeout the client saw a dropped connection. Track each session's open Legacy requests and cancel them when the session's owner sends DELETE, before the transport closes the session. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #7341 +/- ##
==========================================
+ Coverage 82.40% 82.56% +0.16%
==========================================
Files 937 941 +4
Lines 95821 96711 +890
==========================================
+ Hits 78962 79853 +891
+ Misses 16854 16853 -1
Partials 5 5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jhrozek
left a comment
There was a problem hiding this comment.
Clean, well-scoped fix — ownership gating before cancellation, pointer-keyed openRequests tracking, and panic-safe cleanup all look solid.
Left a few inline suggestions for edge cases worth tightening up:
- Refcount
openRequests.ended(map[string]int) so overlappingDELETEs can't prematurely clear the closing marker. - Short-circuit on
ctx.Err() != nilright afteropenRequests.beginso requests cancelled on entry don't runenforceSessionBinding(context.WithoutCancel→ possibleRestoreSessionor spurious hijackWARN+ duplicateTerminate). - Track the remaining Legacy I/O paths (
coreSubscribeHandler,optimizerFindToolHandler,lazyInjectSessionTools) so they can't stallDELETEon slow backends/embeddings. - Add test coverage for overlapping
endSessioncalls and foreign-DELETEnon-cancellation. - Optional docs follow-up: a short bullet under
### Session Ownershipindocs/arch/10-virtual-mcp-architecture.md(anddocs/arch/13-vmcp-scalability.md:58-71) noting thatDELETEcancels tracked Legacy calls pod-locally on the receiving replica.
Count the DELETEs in progress on each session, so the first one to finish cannot let new requests run while another is still closing the session. Each cleanup runs once. Return at once from a handler whose session is already ending, before the binding check would load the session again. Track the subscribe handler, find_tool and the lazy tool injection hook too, since each can reach the backends while the session is open. Add tests for overlapping DELETEs, a non-owner DELETE that must not cancel the owner's call, and the early return. Note in the architecture doc that DELETE cancels only the receiving replica's requests. Co-Authored-By: Claude Opus 5.5 <[email protected]>
jhrozek
left a comment
There was a problem hiding this comment.
Looks good. The refcounted ended marker and the early ctx.Err() return cover what I raised, and the extra Legacy paths are tracked now. Agreed the 400-DELETE edge belongs in mcpcompat. Optional: a line in 13-vmcp-scalability.md on the pod-local scope, same as the one added to doc 10.
Summary
A client's DELETE on a Legacy vMCP session that has a
tools/callin flight took 24 to 34 seconds. Past the server's write timeout, the client saw a dropped connection instead of a status.Why: the transport answers DELETE by closing the go-sdk session, and that close waits for in-flight requests to finish. It does not cancel them. So the DELETE waited for the open call, which waited on the backend for the full backend request timeout (30s by default). The call then closed its backend client, whose own DELETE to the still-busy backend waited for go-sdk's 5s limit.
Manager.Terminateran only after all of that.The client asked to end the session, and nobody can use those results afterwards, so the vMCP now cancels the session's open requests before the close:
openRequests). The Legacy tool, resource, prompt and completion handlers register while they run.sessionOwnershipMiddlewarecancels them, with the cause "session terminated by the client", before passing the DELETE on. A request that starts on that session during the DELETE is cancelled at once. A DELETE from anyone else is still rejected with 404 before anything is cancelled.Before and after, with a real streamable-HTTP backend whose tool runs for 60s (in-process; vMCP with default timeouts):
The remaining 5s is the vMCP's DELETE to the backend. The backend is still running the tool because the vMCP does not yet pass the cancellation on to it (the same 5s showed up with a backend tool that stops on cancel). Forwarding the cancellation is separate work; once it lands, this DELETE should come back in milliseconds.
Type of change
Test plan
task test):go test -race ./pkg/vmcp/server/...task lint-fix):golangci-lint run ./pkg/vmcp/server/...New tests:
TestDeleteCancelsOpenToolCallserves the fullServer.Handlerchain, holds atools/callopen in the core, and sends DELETE. With the fix, the DELETE returns 200 in about a millisecond and the call is cancelled with the session-terminated cause. Without it, the DELETE never returns and the test fails when its 5s client timeout fires.TestOpenRequestscovers the tracker: only the deleted session's requests are cancelled, a request starting during the DELETE is cancelled, finished requests are forgotten, and sessionless requests are not tracked.Manual: a throwaway in-process test with a real streamable-HTTP backend (60s tool) measured the DELETE before (35s) and after (5s) the fix. It is not committed.
Does this introduce a user-facing change?
Yes. Before: a DELETE on a Legacy session with a call in flight waited up to the backend request timeout plus 5s, and could end in a dropped connection. After: the vMCP cancels the session's open calls and answers the DELETE without waiting for them. The cancelled call's own POST ends without the backend's answer, because the session is ending.
Special notes for reviewers
sessionOwnershipMiddlewarebecause that is the last vMCP code that runs before the transport closes the session, and it has already checked that the caller owns the session.Manager.Terminateruns after the close, which is too late.🤖 Generated with Claude Code