diff --git a/client.go b/client.go index b1bc7a3fc..7520ccf41 100644 --- a/client.go +++ b/client.go @@ -535,10 +535,14 @@ func (c *Client) dispatch(ctx context.Context, req *Request, resp *Response) err return err } - s, err := c.createStream(0, p) - if err != nil { - return err - } + var s *stream + ch := make(chan error) + go func() { + s, err = c.createStream(0, p) + if err != nil { + ch <- err + } + }() defer c.deleteStream(s) var msg *streamMessage @@ -547,6 +551,8 @@ func (c *Client) dispatch(ctx context.Context, req *Request, resp *Response) err return ctx.Err() case <-c.ctx.Done(): return ErrClosed + case err := <-ch: + return err case <-s.recvClose: // If recv has a pending message, process that first select {