rpc: fix client shutdown hang when Close races with Unsubscribe (#17894)

Fixes #17837
This commit is contained in:
Felix Lange
2018-10-15 10:56:04 +02:00
committed by GitHub
parent 6566a0a3b8
commit 2e98631c5e
3 changed files with 102 additions and 47 deletions

View File

@ -25,7 +25,6 @@ import (
"fmt"
"net"
"net/url"
"os"
"reflect"
"strconv"
"strings"
@ -118,7 +117,8 @@ type Client struct {
// for dispatch
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
readErr chan error // errors from read
readResp chan []*jsonrpcMessage // valid messages from read
@ -181,45 +181,6 @@ func DialContext(ctx context.Context, rawurl string) (*Client, error) {
}
}
type StdIOConn struct{}
func (io StdIOConn) Read(b []byte) (n int, err error) {
return os.Stdin.Read(b)
}
func (io StdIOConn) Write(b []byte) (n int, err error) {
return os.Stdout.Write(b)
}
func (io StdIOConn) Close() error {
return nil
}
func (io StdIOConn) LocalAddr() net.Addr {
return &net.UnixAddr{Name: "stdio", Net: "stdio"}
}
func (io StdIOConn) RemoteAddr() net.Addr {
return &net.UnixAddr{Name: "stdio", Net: "stdio"}
}
func (io StdIOConn) SetDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "stdio", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (io StdIOConn) SetReadDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "stdio", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func (io StdIOConn) SetWriteDeadline(t time.Time) error {
return &net.OpError{Op: "set", Net: "stdio", Source: nil, Addr: nil, Err: errors.New("deadline not supported")}
}
func DialStdIO(ctx context.Context) (*Client, error) {
return newClient(ctx, func(_ context.Context) (net.Conn, error) {
return StdIOConn{}, nil
})
}
func newClient(initctx context.Context, connectFunc func(context.Context) (net.Conn, error)) (*Client, error) {
conn, err := connectFunc(initctx)
if err != nil {
@ -231,7 +192,8 @@ func newClient(initctx context.Context, connectFunc func(context.Context) (net.C
isHTTP: isHTTP,
connectFunc: connectFunc,
close: make(chan struct{}),
didQuit: make(chan struct{}),
closing: make(chan struct{}),
didClose: make(chan struct{}),
reconnected: make(chan net.Conn),
readErr: make(chan error),
readResp: make(chan []*jsonrpcMessage),
@ -268,8 +230,8 @@ func (c *Client) Close() {
}
select {
case c.close <- struct{}{}:
<-c.didQuit
case <-c.didQuit:
<-c.didClose
case <-c.didClose:
}
}
@ -469,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
// subscription notifications.
return ctx.Err()
case <-c.didQuit:
case <-c.closing:
return ErrClientQuit
case <-c.didClose:
return ErrClientQuit
}
}
@ -504,7 +468,7 @@ func (c *Client) reconnect(ctx context.Context) error {
case c.reconnected <- newconn:
c.writeConn = newconn
return nil
case <-c.didQuit:
case <-c.didClose:
newconn.Close()
return ErrClientQuit
}
@ -522,8 +486,9 @@ func (c *Client) dispatch(conn net.Conn) {
requestOpLock = c.requestOp // nil while the send lock is held
reading = true // if true, a read loop is running
)
defer close(c.didQuit)
defer close(c.didClose)
defer func() {
close(c.closing)
c.closeRequestOps(ErrClientQuit)
conn.Close()
if reading {