Skip to content

Commit

Permalink
fix no corresponding request if req.ID is modified by onSend (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
Semesse authored Jan 13, 2023
1 parent 065a868 commit e1f9fdf
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,7 @@ func (c *Conn) send(_ context.Context, m *anyMessage, wait bool) (cc *call, err
return nil, ErrClosed
}

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

Expand All @@ -445,8 +444,6 @@ func (c *Conn) send(_ context.Context, m *anyMessage, wait bool) (cc *call, err
m.request.ID.Num = c.seq
}
}
id = m.request.ID
c.pending[id] = cc
c.seq++
}
c.mu.Unlock()
Expand All @@ -467,6 +464,15 @@ func (c *Conn) send(_ context.Context, m *anyMessage, wait bool) (cc *call, err
}
}

// Store requests so we can later associate them with incoming
// responses.
if m.request != nil && wait {
c.mu.Lock()
id = m.request.ID
c.pending[id] = cc
c.mu.Unlock()
}

// From here on, if we fail to send this, then we need to remove
// this from the pending map so we don't block on it or pile up
// pending entries for unsent messages.
Expand Down

0 comments on commit e1f9fdf

Please sign in to comment.