jsonrpc: serialize writes to client connection#9
Merged
Conversation
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.
Summary
This PR serializes writes from
jsonrpc.Clientto its underlying connection by adding a write mutex around theSetWriteDeadline+Writepair.It also adds a regression test that launches concurrent
MethodBlockingcalls through a wrappednet.Connwhich reports an error if writes overlap.Root cause
jsonrpc.Client.Methodcan be called from multiple goroutines, but it previously wrote request bytes directly to the sharednet.Connwithout write-side synchronization. Plain TCP usually masks this because writes are internally serialized, but othernet.Connimplementations can expose the overlap.One affected transport is
golang.org/x/crypto/ssh'schanConn: itsWritedelegates toChannel.WriteExtended, whose source currently notes that concurrentWriteExtendedcalls can be flagged by the race detector because a packet buffer is reused. ItsSetWriteDeadlineimplementation also returnsssh: tcpChan: deadline not supported, so the deadline call does not protect the write path.In downstream SSH-tunneled Electrum usage, concurrent
MethodBlockingcalls can corrupt the SSH packet stream, drop the connection, and cause all in-flight requests to fail together withcontext canceled.Evidence
A downstream reproducer run with
go run -racereported races through this call path:golang.org/x/crypto/ssh.(*channel).WriteExtendedgolang.org/x/crypto/ssh.(*channel).Writegolang.org/x/crypto/ssh.(*chanConn).Writegithub.com/BitBoxSwiss/block-client-go/jsonrpc.(*Client).MethodThe regression test in this PR verifies the client no longer performs overlapping writes while preserving concurrent request/response handling.
Impact
For TCP-only users, this should be behaviorally equivalent to the existing implementation, with one additional mutex acquire per request write. For SSH-tunneled or other connection implementations that are sensitive to concurrent writes, this avoids corrupting the transport and tearing down unrelated in-flight requests.
Tests
go test ./jsonrpcgo test ./...go test -race ./...