Skip to content

mcp: let a canceled ioConn write leave the queue - #1499

Open
Tangerg wants to merge 1 commit into
modelcontextprotocol:mainfrom
Tangerg:fix/ioconn-write-cancellation
Open

Tangerg wants to merge 1 commit into
modelcontextprotocol:mainfrom
Tangerg:fix/ioconn-write-cancellation

Conversation

@Tangerg

@Tangerg Tangerg commented Nov 5, 2026

Copy link
Copy Markdown
Contributor

ioConn.Write serialized writers with a mutex and checked ctx only before waiting for it, so a write canceled while another write was in progress still waited and was then sent anyway, and a write blocked on a peer that stopped reading could not be interrupted.

Writers now queue on a one-slot channel, so a canceled waiter returns ctx.Err() without sending. Canceling the write in progress closes the connection: bytes already handed to the stream cannot be withdrawn, and a partial frame would corrupt every later message.

Fixes #1498

AI assistance: prepared with Claude; I reviewed the change and can explain it.

ioConn.Write serialized writers with a mutex and checked ctx only before
waiting for it, so a write whose context was canceled while another write
was in progress still waited for that write and was then sent anyway. A
write blocked on a peer that stopped reading could not be interrupted at
all.

Queue writers on a one-slot channel so a canceled waiter returns without
sending. Canceling the active write closes the connection: bytes already
handed to the stream cannot be withdrawn, and a partial frame would
corrupt every later message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 5, 2026
@github-actions github-actions Bot closed this Nov 5, 2026
@github-actions github-actions Bot reopened this Nov 5, 2026
@github-actions github-actions Bot removed the missing-issue-link Auto-closed: PR needs a linked issue assigned to its author (see CONTRIBUTING.md) label Nov 5, 2026
@guglielmo-san

Copy link
Copy Markdown
Contributor

@Tangerg let's scope this PR only to the blocking case. Exclude for now the case of the cancellation of on going call

This branch has not been deployed

No deployments
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.

mcp: a canceled write on IOTransport/StdioTransport cannot leave the write queue

2 participants