Skip to content

jsonrpc2: wrap write error in server-closing error - #1186

Merged
guglielmo-san merged 4 commits into
modelcontextprotocol:mainfrom
Tethys0:fix/jsonrpc2-wrap-write-error
Aug 27, 2026
Merged

jsonrpc2: wrap write error in server-closing error#1186
guglielmo-san merged 4 commits into
modelcontextprotocol:mainfrom
Tethys0:fix/jsonrpc2-wrap-write-error

Conversation

@Tethys0

@Tethys0 Tethys0 commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

The "server is closing" error produced when a connection shuts down because its write side failed formatted the underlying write error with %v, so the cause - typically io.EOF when a stdio host closes its pipe - was not part of the error chain. A caller that wants to distinguish a clean host disconnect from a real failure via errors.Is(err, io.EOF) therefore always got false.

This changes both s.writeErr wrappings to use %w, preserving the write error in the error chain while keeping the same message text ("server is closing: EOF").

A regression test exercises the write-failure branch of shuttingDown and asserts both errors.Is(err, ErrServerClosing) and errors.Is(err, io.EOF).

Fixes #1098

The connection's "server is closing" error formatted the underlying
write error with %v, so the cause - typically io.EOF when a stdio host
closes its pipe - was not in the error chain. Callers using
errors.Is(err, io.EOF) to distinguish a clean host disconnect from a
real failure therefore always got false.

Use %w so the write error is preserved in the error chain.

Fixes modelcontextprotocol#1098
Comment thread internal/jsonrpc2/conn.go Outdated

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.

we should use the same approach for both cases

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied the same %w wrapping to the read-shutdown path in aecb26d, with a focused errors.Is regression test. The pushed branch is ready for re-review.

guglielmo-san
guglielmo-san previously approved these changes Aug 26, 2026
@guglielmo-san
guglielmo-san enabled auto-merge (squash) August 27, 2026 12:46
@guglielmo-san
guglielmo-san merged commit 165121e into modelcontextprotocol:main Aug 27, 2026
9 checks passed
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.

jsonrpc2: server-closing error formats writeErr with %v, so errors.Is(err, io.EOF) is false for consumers

3 participants