Repository navigation
fix(kit/jsonrpc): deliver a reply that arrives as the connection closes - #40
Merged
Merged
Conversation
Client.Call waited on its reply channel and on Done in one select. When the server replied and then closed the connection (as a shutdown method does), both could be ready, and select picked one at random: half the time the call failed with "connection closed" although its reply had come. readLoop hands over every reply before it closes Done, so Call now takes a reply that is already there before it reports the closed connection. The new test makes Write return only after Done, so both are ready when Call waits; it failed every run before the fix.
bketelsen
approved these changes
Oct 28, 2026
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.
What
kit/jsonrpc:Client.Callcan lose a reply that arrives just before the server closes the connection.Callwaits on its reply channel and onDonein oneselect. A server that replies and then closes (frostnotes'rpc.shutdowndoes) can make both ready at once, andselectpicks one at random: the call then fails withconnection closedalthough its reply came. This made frostnotes'rpc.shutdowntests flaky under-race.Fix
readLoophands every reply to its call's buffered channel before it closesDone. So whenCallseesDone, it now first takes a reply that is already in its channel, and reports the closed connection only when none is there. The decode of a reply moves to a small helper,callResult. The kit's API does not change.Test
TestAReplyBeforeTheCloseReachesTheCallwraps the client's end of a pipe so thatWritereturns only afterDoneis closed: the reply andDoneare both ready whenCallstarts to wait. It runs 64 calls.jsonrpc: connection closed), 3 of 3.go test -race -count=200 -run TestAReplyBeforeTheCloseReachesTheCall ./jsonrpc/passes, andgo test -race -count=20 ./jsonrpc/passes.make checkpasses.After the merge
kit/v0.3.1(the tag is his).kit/v0.3.1.