Skip to content

Commit 380fc7d

Browse files
committed
Fix data race in client Close
1 parent 474a323 commit 380fc7d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

‎client.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,6 @@ func (c *ClientRaw) Close() error {
227227
if c == nil {
228228
return nil
229229
}
230-
defer close(c.Messages)
231230

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

242241
err := c.conn.Close()
243242

243+
close(c.Messages)
244+
244245
return err
245246
}
246247

@@ -316,16 +317,17 @@ func (c *ClientRaw) input() {
316317
break
317318
}
318319

320+
c.mu.Lock()
319321
id := message.Header.ID
320322
if id == 0 {
321323
// A message with ID 0 is a standalone message (e.g. log message)
322324
// and not part of the request-response flow.
323325
c.Messages <- message
326+
c.mu.Unlock()
324327
continue
325328
}
326329

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

339341
delete(c.pending, id)
340-
c.mu.Unlock()
341342
if call == nil {
342343
err = fmt.Errorf("call with ID %d not found", id)
344+
c.mu.Unlock()
343345
break
344346
}
345347
call.Messages <- message
348+
c.mu.Unlock()
346349
call.done()
347350
}
348351

0 commit comments

Comments
 (0)