diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 8469312..ae225be 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -51,6 +51,16 @@ func (c *Client) Close() error { if c.conn != nil { err := c.conn.Close() c.conn = nil + + // Notify all pending requests so they don't block forever. + // The receive goroutine treats a closed connection as a normal exit + // and won't call handleError, so we must clean up here. + requests := c.requests + c.requests = make(map[uint64]chan *serialize.Reader) + for _, ch := range requests { + ch <- nil + } + return err } return nil @@ -207,7 +217,8 @@ func (c *Client) sendMessage(ctx context.Context, serviceID uint64, methodID uin return 0, nil, err } - ch := make(chan *serialize.Reader) + // With a buffered channel of size 1, a late send succeeds without blocking + ch := make(chan *serialize.Reader, 1) c.requests[requestID] = ch // Send message