Skip to content

Commit

Permalink
Fix data race in client Close
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 1, 2024
1 parent 474a323 commit 380fc7d
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (c *ClientRaw) Close() error {
if c == nil {
return nil
}
defer close(c.Messages)

c.sendMu.Lock()
defer c.sendMu.Unlock()
Expand All @@ -241,6 +240,8 @@ func (c *ClientRaw) Close() error {

err := c.conn.Close()

close(c.Messages)

return err
}

Expand Down Expand Up @@ -316,16 +317,17 @@ func (c *ClientRaw) input() {
break
}

c.mu.Lock()
id := message.Header.ID
if id == 0 {
// A message with ID 0 is a standalone message (e.g. log message)
// and not part of the request-response flow.
c.Messages <- message
c.mu.Unlock()
continue
}

// Attach it to the correct pending call.
c.mu.Lock()
call, found := c.pending[id]
if !found {
panic(fmt.Sprintf("call with ID %d not found", id))
Expand All @@ -337,12 +339,13 @@ func (c *ClientRaw) input() {
}

delete(c.pending, id)
c.mu.Unlock()
if call == nil {
err = fmt.Errorf("call with ID %d not found", id)
c.mu.Unlock()
break
}
call.Messages <- message
c.mu.Unlock()
call.done()
}

Expand Down

0 comments on commit 380fc7d

Please sign in to comment.