Skip to content

Comments

fix(client): wait for in-flight requests before aborting on close()#1569

Closed
aptsalt wants to merge 2 commits intomodelcontextprotocol:mainfrom
aptsalt:fix/graceful-transport-close
Closed

fix(client): wait for in-flight requests before aborting on close()#1569
aptsalt wants to merge 2 commits intomodelcontextprotocol:mainfrom
aptsalt:fix/graceful-transport-close

Conversation

@aptsalt
Copy link

@aptsalt aptsalt commented Feb 22, 2026

Summary

  • Track in-flight send() requests in both SSEClientTransport and StreamableHTTPClientTransport
  • close() now waits for pending requests to settle (with a 2s timeout) before calling AbortController.abort()
  • Prevents Undici/OpenTelemetry from marking successful 200 OK responses as UND_ERR_ABORTED

Context

Fixes #1231 — When close() is called while POST requests are still in-flight, the immediate AbortController.abort() causes Undici to report UND_ERR_ABORTED even though the server returned 200 OK. This creates misleading telemetry in OpenTelemetry traces.

Changes

packages/client/src/client/streamableHttp.ts

  • Added _pendingRequests Set to track active send() calls
  • send() registers/unregisters itself from the set using try/finally
  • close() waits up to 2s for pending requests before aborting

packages/client/src/client/sse.ts

  • Same pattern applied to the SSE transport

packages/client/test/client/streamableHttp.test.ts

  • "should wait for in-flight requests before aborting" — verifies abort fires after the fetch completes
  • "should abort after timeout even if requests are still pending" — verifies stuck requests don't block close() beyond 2s

Test plan

  • All 40 StreamableHTTP tests pass
  • All 27 SSE tests pass
  • Build succeeds (pnpm build:all)
  • Manual verification with OpenTelemetry Undici instrumentation

Closes #1231

Both SSEClientTransport and StreamableHTTPClientTransport now track
pending send() requests and wait for them to settle (with a 2s timeout)
before calling AbortController.abort(). This prevents Undici and
OpenTelemetry instrumentation from marking successful 200 OK responses
as UND_ERR_ABORTED when close() is called while requests are in-flight.

Closes #1231
@aptsalt aptsalt requested a review from a team as a code owner February 22, 2026 09:31
@changeset-bot
Copy link

changeset-bot bot commented Feb 22, 2026

🦋 Changeset detected

Latest commit: d215fff

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@modelcontextprotocol/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 22, 2026

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/client@1569

@modelcontextprotocol/server

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/server@1569

@modelcontextprotocol/express

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/express@1569

@modelcontextprotocol/hono

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/hono@1569

@modelcontextprotocol/node

npm i https://pkg.pr.new/modelcontextprotocol/typescript-sdk/@modelcontextprotocol/node@1569

commit: d215fff

@aptsalt aptsalt closed this by deleting the head repository Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Undici instrumentation marks successful MCP requests as aborted due to premature AbortController.abort() in SSE / StreamableHTTP transports

1 participant