Skip to content

Commit e1f9fdf

Browse files
authored
fix no corresponding request if req.ID is modified by onSend (#60)
1 parent 065a868 commit e1f9fdf

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

jsonrpc2.go

+10-4
Original file line numberDiff line numberDiff line change
@@ -432,8 +432,7 @@ func (c *Conn) send(_ context.Context, m *anyMessage, wait bool) (cc *call, err
432432
return nil, ErrClosed
433433
}
434434

435-
// Store requests so we can later associate them with incoming
436-
// responses.
435+
// Assign a default id if not set
437436
if m.request != nil && wait {
438437
cc = &call{request: m.request, seq: c.seq, done: make(chan error, 1)}
439438

@@ -445,8 +444,6 @@ func (c *Conn) send(_ context.Context, m *anyMessage, wait bool) (cc *call, err
445444
m.request.ID.Num = c.seq
446445
}
447446
}
448-
id = m.request.ID
449-
c.pending[id] = cc
450447
c.seq++
451448
}
452449
c.mu.Unlock()
@@ -467,6 +464,15 @@ func (c *Conn) send(_ context.Context, m *anyMessage, wait bool) (cc *call, err
467464
}
468465
}
469466

467+
// Store requests so we can later associate them with incoming
468+
// responses.
469+
if m.request != nil && wait {
470+
c.mu.Lock()
471+
id = m.request.ID
472+
c.pending[id] = cc
473+
c.mu.Unlock()
474+
}
475+
470476
// From here on, if we fail to send this, then we need to remove
471477
// this from the pending map so we don't block on it or pile up
472478
// pending entries for unsent messages.

0 commit comments

Comments
 (0)