internal/jsonrpc2: wrap writeErr with %w so errors.Is works for consumers#1105
Open
mybytecode wants to merge 1 commit into
Open
internal/jsonrpc2: wrap writeErr with %w so errors.Is works for consumers#1105mybytecode wants to merge 1 commit into
mybytecode wants to merge 1 commit into
Conversation
…mers Fixes modelcontextprotocol#1098. When a connection shuts down due to a write failure, the error was formatted with %v for s.writeErr, which meant the underlying error (typically io.EOF) was not in the error chain. Consumers could not use errors.Is(err, io.EOF) to distinguish a clean host disconnect from a real failure. Change %v to %w so that both ErrServerClosing and the write error are properly wrapped and discoverable via errors.Is/errors.As.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1098.
Summary
%v→%winconn.go:674so thats.writeErris properly wrapped in the error chainerrors.Is(err, io.EOF)to detect clean host disconnects vs. real failuresTestServerClosingErrorWrapsWriteErrDetails
When a connection shuts down due to a write failure, the error was built as:
The
%vverb formatss.writeErras text only — it is not in the error chain. This meanserrors.Is(err, io.EOF)returnsfalse, and consumers cannot programmatically classify shutdown errors.The fix changes
%vto%w, which is supported since Go 1.20 (this SDK targets Go 1.25). With multiple%wverbs,fmt.Errorfreturns an error implementingUnwrap() []error, making bothErrServerClosingand the underlying write error discoverable viaerrors.Is/errors.As.Validation
go test ./internal/jsonrpc2/ -v— all passgo test ./...— full suite passesgo vet ./...— clean