Skip to content

Commit 89966f2

Browse files
fix(kit/jsonrpc): deliver a reply that arrives as the connection closes
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.
1 parent a43d3be commit 89966f2

2 files changed

Lines changed: 67 additions & 11 deletions

File tree

‎kit/jsonrpc/client.go‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,35 @@ func (c *Client[C]) Call(ctx context.Context, method string, params, result any)
115115
}
116116
select {
117117
case m := <-ch:
118-
if m.Error != nil {
119-
return m.Error
120-
}
121-
if result == nil {
122-
return nil
123-
}
124-
if err := json.Unmarshal(m.Result, result); err != nil {
125-
return fmt.Errorf("decode %s result: %w", method, err)
126-
}
127-
return nil
118+
return callResult(method, m, result)
128119
case <-ctx.Done():
129120
return ctx.Err()
130121
case <-c.done:
131-
return c.Err()
122+
// readLoop hands over every reply before it closes done, so a
123+
// reply that came just before the end is in ch now: a select
124+
// that saw both ready may have picked done.
125+
select {
126+
case m := <-ch:
127+
return callResult(method, m, result)
128+
default:
129+
return c.Err()
130+
}
131+
}
132+
}
133+
134+
// callResult ends a call with its response m: the server's error, or the
135+
// result decoded into result (unless nil).
136+
func callResult[C Code](method string, m Frame[C], result any) error {
137+
if m.Error != nil {
138+
return m.Error
139+
}
140+
if result == nil {
141+
return nil
142+
}
143+
if err := json.Unmarshal(m.Result, result); err != nil {
144+
return fmt.Errorf("decode %s result: %w", method, err)
132145
}
146+
return nil
133147
}
134148

135149
func (c *Client[C]) write(m Frame[C]) error {

‎kit/jsonrpc/jsonrpc_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,3 +437,45 @@ func TestNotifyRefusesAnOversizedNotification(t *testing.T) {
437437
t.Errorf("Notify = %v, want refused", err)
438438
}
439439
}
440+
441+
// writeThenWait is a client's end of a pipe whose Write returns only after
442+
// the client has seen its connection end, so that Call finds both its reply
443+
// and Done ready when it starts to wait.
444+
type writeThenWait struct {
445+
net.Conn
446+
done <-chan struct{}
447+
}
448+
449+
func (w *writeThenWait) Write(p []byte) (int, error) {
450+
n, err := w.Conn.Write(p)
451+
<-w.done
452+
return n, err
453+
}
454+
455+
// A reply that arrives just before the server closes the connection, as
456+
// a shutdown method's does, reaches the caller: Call never picks the
457+
// closed connection over a reply that is already there.
458+
func TestAReplyBeforeTheCloseReachesTheCall(t *testing.T) {
459+
for range 64 {
460+
cn, sn := net.Pipe()
461+
w := &writeThenWait{Conn: cn}
462+
c := jsonrpc.NewClient[code](w)
463+
w.done = c.Done()
464+
go func() {
465+
defer func() { _ = sn.Close() }()
466+
sc := bufio.NewScanner(sn)
467+
if sc.Scan() {
468+
_, _ = sn.Write([]byte(`{"jsonrpc":"2.0","id":1,"result":{"text":"bye"}}` + "\n"))
469+
}
470+
}()
471+
var got struct {
472+
Text string `json:"text"`
473+
}
474+
if err := c.Call(t.Context(), "shutdown", nil, &got); err != nil {
475+
t.Fatalf("a reply before the close: %v", err)
476+
}
477+
if got.Text != "bye" {
478+
t.Fatalf("result = %+v", got)
479+
}
480+
}
481+
}

0 commit comments

Comments
 (0)