Skip to content

Commit f3ac95e

Browse files
committed
Check if cc.conns == nil before reading cc.conns
1 parent 2e4cfe0 commit f3ac95e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

call_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,18 @@ func TestInvokeCancel(t *testing.T) {
276276
cc.Close()
277277
server.stop()
278278
}
279+
280+
// TestInvokeCancelClosedNonFail checks that a canceled non-failfast RPC
281+
// on a closed client will terminate.
282+
func TestInvokeCancelClosedNonFailFast(t *testing.T) {
283+
server, cc := setUp(t, 0, math.MaxUint32)
284+
var reply string
285+
cc.Close()
286+
req := "hello"
287+
ctx, cancel := context.WithCancel(context.Background())
288+
cancel()
289+
if err := Invoke(ctx, "/foo/bar", &req, &reply, cc, FailFast(false)); err == nil {
290+
t.Fatalf("canceled invoke on closed connection should fail")
291+
}
292+
server.stop()
293+
}

clientconn.go

+4
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ func (cc *ClientConn) getTransport(ctx context.Context, opts BalancerGetOptions)
479479
if cc.dopts.balancer == nil {
480480
// If balancer is nil, there should be only one addrConn available.
481481
cc.mu.RLock()
482+
if cc.conns == nil {
483+
cc.mu.RUnlock()
484+
return nil, nil, toRPCErr(ErrClientConnClosing)
485+
}
482486
for _, ac = range cc.conns {
483487
// Break after the first iteration to get the first addrConn.
484488
ok = true

0 commit comments

Comments
 (0)