Skip to content

Cancel a session's open calls when its client deletes it - #7341

Merged
rdimitrov merged 2 commits into
mainfrom
fix/vmcp-delete-inflight-call
Nov 27, 2026
Merged

rdimitrov merged 2 commits into
mainfrom
fix/vmcp-delete-inflight-call

Conversation

@rdimitrov

Copy link
Copy Markdown
Member

Summary

A client's DELETE on a Legacy vMCP session that has a tools/call in 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.Terminate ran 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:

  • A small per-session list of open requests (openRequests). The Legacy tool, resource, prompt and completion handlers register while they run.
  • When the session's owner sends DELETE, sessionOwnershipMiddleware cancels 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):

DELETE took
Before 35s (30s backend request timeout + 5s backend DELETE)
After 5s
After, full-chain test with a core call that waits for cancellation ~1ms (before: no answer; the test's 5s client timeout fired)

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

  • Bug fix

Test plan

  • Unit tests (task test): go test -race ./pkg/vmcp/server/...
  • Linting (task lint-fix): golangci-lint run ./pkg/vmcp/server/...
  • Manual testing (describe below)

New tests:

  • TestDeleteCancelsOpenToolCall serves the full Server.Handler chain, holds a tools/call open 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.
  • TestOpenRequests covers 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

  • The cancel lives in sessionOwnershipMiddleware because 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.Terminate runs after the close, which is too late.
  • Modern (2026-07-28) requests are not tracked. They are stateless, so a closed request already cancels its own context.
  • A DELETE handled by a different replica than the one running the call does not cancel it. That replica sees the session as terminated on its next request, as before.

🤖 Generated with Claude Code

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]>
@github-actions github-actions Bot added the size/M Medium PR: 300-599 lines changed label Nov 26, 2026
@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.43590% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.56%. Comparing base (9300f3f) to head (c1f79f0).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
pkg/vmcp/server/serve_handlers.go 90.00% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 overlapping DELETEs can't prematurely clear the closing marker.
  • Short-circuit on ctx.Err() != nil right after openRequests.begin so requests cancelled on entry don't run enforceSessionBinding (context.WithoutCancel → possible RestoreSession or spurious hijack WARN + duplicate Terminate).
  • Track the remaining Legacy I/O paths (coreSubscribeHandler, optimizerFindToolHandler, lazyInjectSessionTools) so they can't stall DELETE on slow backends/embeddings.
  • Add test coverage for overlapping endSession calls and foreign-DELETE non-cancellation.
  • Optional docs follow-up: a short bullet under ### Session Ownership in docs/arch/10-virtual-mcp-architecture.md (and docs/arch/13-vmcp-scalability.md:58-71) noting that DELETE cancels tracked Legacy calls pod-locally on the receiving replica.

Comment thread pkg/vmcp/server/open_requests.go Resolved
Comment thread pkg/vmcp/server/serve_handlers.go Resolved
Comment thread pkg/vmcp/server/serve_handlers.go Resolved
Comment thread pkg/vmcp/server/session_ownership.go Resolved
Comment thread pkg/vmcp/server/open_requests_test.go Resolved
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]>
@github-actions github-actions Bot added size/M Medium PR: 300-599 lines changed and removed size/M Medium PR: 300-599 lines changed labels Nov 26, 2026

@jhrozek jhrozek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@rdimitrov
rdimitrov merged commit 9895e29 into main Nov 27, 2026
57 checks passed
@rdimitrov
rdimitrov deleted the fix/vmcp-delete-inflight-call branch November 27, 2026 09:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Medium PR: 300-599 lines changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants