mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 10:22:23 +00:00
rpc: fix client shutdown hang when Close races with Unsubscribe
Fixes #17837
This commit is contained in:
parent
032416e542
commit
c2ae396646
2 changed files with 36 additions and 7 deletions
|
|
@ -117,7 +117,8 @@ type Client struct {
|
||||||
|
|
||||||
// for dispatch
|
// for dispatch
|
||||||
close chan struct{}
|
close chan struct{}
|
||||||
didQuit chan struct{} // closed when client quits
|
closing chan struct{} // closed when client is quitting
|
||||||
|
didClose chan struct{} // closed when client quits
|
||||||
reconnected chan net.Conn // where write/reconnect sends the new connection
|
reconnected chan net.Conn // where write/reconnect sends the new connection
|
||||||
readErr chan error // errors from read
|
readErr chan error // errors from read
|
||||||
readResp chan []*jsonrpcMessage // valid messages from read
|
readResp chan []*jsonrpcMessage // valid messages from read
|
||||||
|
|
@ -191,7 +192,8 @@ func newClient(initctx context.Context, connectFunc func(context.Context) (net.C
|
||||||
isHTTP: isHTTP,
|
isHTTP: isHTTP,
|
||||||
connectFunc: connectFunc,
|
connectFunc: connectFunc,
|
||||||
close: make(chan struct{}),
|
close: make(chan struct{}),
|
||||||
didQuit: make(chan struct{}),
|
closing: make(chan struct{}),
|
||||||
|
didClose: make(chan struct{}),
|
||||||
reconnected: make(chan net.Conn),
|
reconnected: make(chan net.Conn),
|
||||||
readErr: make(chan error),
|
readErr: make(chan error),
|
||||||
readResp: make(chan []*jsonrpcMessage),
|
readResp: make(chan []*jsonrpcMessage),
|
||||||
|
|
@ -228,8 +230,8 @@ func (c *Client) Close() {
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case c.close <- struct{}{}:
|
case c.close <- struct{}{}:
|
||||||
<-c.didQuit
|
<-c.didClose
|
||||||
case <-c.didQuit:
|
case <-c.didClose:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -429,7 +431,9 @@ func (c *Client) send(ctx context.Context, op *requestOp, msg interface{}) error
|
||||||
// This can happen if the client is overloaded or unable to keep up with
|
// This can happen if the client is overloaded or unable to keep up with
|
||||||
// subscription notifications.
|
// subscription notifications.
|
||||||
return ctx.Err()
|
return ctx.Err()
|
||||||
case <-c.didQuit:
|
case <-c.closing:
|
||||||
|
return ErrClientQuit
|
||||||
|
case <-c.didClose:
|
||||||
return ErrClientQuit
|
return ErrClientQuit
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -464,7 +468,7 @@ func (c *Client) reconnect(ctx context.Context) error {
|
||||||
case c.reconnected <- newconn:
|
case c.reconnected <- newconn:
|
||||||
c.writeConn = newconn
|
c.writeConn = newconn
|
||||||
return nil
|
return nil
|
||||||
case <-c.didQuit:
|
case <-c.didClose:
|
||||||
newconn.Close()
|
newconn.Close()
|
||||||
return ErrClientQuit
|
return ErrClientQuit
|
||||||
}
|
}
|
||||||
|
|
@ -482,8 +486,9 @@ func (c *Client) dispatch(conn net.Conn) {
|
||||||
requestOpLock = c.requestOp // nil while the send lock is held
|
requestOpLock = c.requestOp // nil while the send lock is held
|
||||||
reading = true // if true, a read loop is running
|
reading = true // if true, a read loop is running
|
||||||
)
|
)
|
||||||
defer close(c.didQuit)
|
defer close(c.didClose)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
close(c.closing)
|
||||||
c.closeRequestOps(ErrClientQuit)
|
c.closeRequestOps(ErrClientQuit)
|
||||||
conn.Close()
|
conn.Close()
|
||||||
if reading {
|
if reading {
|
||||||
|
|
|
||||||
|
|
@ -323,6 +323,30 @@ func TestClientSubscribeClose(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This test reproduces https://github.com/ethereum/go-ethereum/issues/17837 where the
|
||||||
|
// client hangs during shutdown when Unsubscribe races with Client.Close.
|
||||||
|
func TestClientCloseUnsubscribeRace(t *testing.T) {
|
||||||
|
service := &NotificationTestService{}
|
||||||
|
server := newTestServer("eth", service)
|
||||||
|
defer server.Stop()
|
||||||
|
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
client := DialInProc(server)
|
||||||
|
nc := make(chan int)
|
||||||
|
sub, err := client.EthSubscribe(context.Background(), nc, "someSubscription", 3, 1)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
go client.Close()
|
||||||
|
go sub.Unsubscribe()
|
||||||
|
select {
|
||||||
|
case <-sub.Err():
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("subscription not closed within timeout")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This test checks that Client doesn't lock up when a single subscriber
|
// This test checks that Client doesn't lock up when a single subscriber
|
||||||
// doesn't read subscription events.
|
// doesn't read subscription events.
|
||||||
func TestClientNotificationStorm(t *testing.T) {
|
func TestClientNotificationStorm(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue