Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpcclient: make sure batch requests are GCed #2105

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 38 additions & 20 deletions rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,21 @@ func (c *Client) removeRequest(id uint64) *jsonRequest {
c.requestLock.Lock()
defer c.requestLock.Unlock()

element := c.requestMap[id]
if element != nil {
delete(c.requestMap, id)
request := c.requestList.Remove(element).(*jsonRequest)
return request
element, ok := c.requestMap[id]
if !ok {
return nil
}

return nil
delete(c.requestMap, id)

var request *jsonRequest
if c.batch {
request = c.batchList.Remove(element).(*jsonRequest)
yyforyongyu marked this conversation as resolved.
Show resolved Hide resolved
} else {
request = c.requestList.Remove(element).(*jsonRequest)
}

return request
}

// removeAllRequests removes all the jsonRequests which contain the response
Expand Down Expand Up @@ -1733,28 +1740,38 @@ func (c *Client) Send() error {
return nil
}

// clear batchlist in case of an error
defer func() {
batchResp, err := c.sendAsync().Receive()
if err != nil {
// Clear batchlist in case of an error.
//
// TODO(yy): need to double check to make sure there's no
// concurrent access to this batch list, otherwise we may miss
// some batched requests.
Comment on lines +1747 to +1749
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah - and I guess we also miss all the ones that were in the list that didnt necessarily cause the failure...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, need more love in this lib, more tests, etc

c.batchList = list.New()
}()

result, err := c.sendAsync().Receive()

if err != nil {
return err
}

for iter := c.batchList.Front(); iter != nil; iter = iter.Next() {
var requestError error
request := iter.Value.(*jsonRequest)
individualResult := result[request.id]
fullResult, err := json.Marshal(individualResult.Result)
// Iterate each response and send it to the corresponding request.
for id, resp := range batchResp {
// Perform a GC on batchList and requestMap before moving
yyforyongyu marked this conversation as resolved.
Show resolved Hide resolved
// forward.
request := c.removeRequest(id)
yyforyongyu marked this conversation as resolved.
Show resolved Hide resolved

// If there's an error, we log it and continue to the next
// request.
fullResult, err := json.Marshal(resp.Result)
if err != nil {
return err
log.Errorf("Unable to marshal result: %v for req=%v",
err, request.id)

Comment on lines +1765 to +1767
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the rationale for changing the error handling here? do we expect to get an unmarshal error here frequently?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a list of requests batched together, if one or more requests failed, it should not affect sending responses to other requests. And nope, I think we should almost never get an unmarshal error.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, an unmarshall request here would mean that the full node sent over garbled json.

continue
}

if individualResult.Error != nil {
requestError = individualResult.Error
// If there's a response error, we send it back the request.
var requestError error
if resp.Error != nil {
requestError = resp.Error
}

result := Response{
Expand All @@ -1763,5 +1780,6 @@ func (c *Client) Send() error {
}
request.responseChan <- &result
}

return nil
}
14 changes: 14 additions & 0 deletions wire/msgblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ type MsgBlock struct {
Transactions []*MsgTx
}

// Copy creates a deep copy of MsgBlock.
func (msg *MsgBlock) Copy() *MsgBlock {
block := &MsgBlock{
Header: msg.Header,
Transactions: make([]*MsgTx, len(msg.Transactions)),
}

for i, tx := range msg.Transactions {
block.Transactions[i] = tx.Copy()
}

return block
}

// AddTransaction adds a transaction to the message.
func (msg *MsgBlock) AddTransaction(tx *MsgTx) error {
msg.Transactions = append(msg.Transactions, tx)
Expand Down
Loading