-
Notifications
You must be signed in to change notification settings - Fork 380
fix(transaction): prevent concurrent map access in monitor #5309
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
Open
mfw78
wants to merge
2
commits into
ethersphere:master
Choose a base branch
from
mfw78:fix/transaction-monitor-race-condition
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+31
−18
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,46 +190,59 @@ func watchStart(watches []transactionWatch) time.Time { | |
| return start | ||
| } | ||
|
|
||
| // check pending checks the given block (number) for confirmed or cancelled transactions | ||
| // checkPending checks the given block for confirmed or cancelled transactions. | ||
| func (tm *transactionMonitor) checkPending(block uint64) error { | ||
| confirmedNonces := make(map[uint64]*types.Receipt) | ||
| var cancelledNonces []uint64 | ||
| for nonceGroup, watchMap := range tm.watchesByNonce { | ||
| // Snapshot nonces and tx hashes to check (releases lock during slow RPC calls). | ||
| tm.lock.Lock() | ||
| snapshot := make(map[uint64]map[common.Hash]time.Time, len(tm.watchesByNonce)) | ||
| for nonce, watchMap := range tm.watchesByNonce { | ||
| snapshot[nonce] = make(map[common.Hash]time.Time, len(watchMap)) | ||
| for txHash, watches := range watchMap { | ||
| snapshot[nonce][txHash] = watchStart(watches) | ||
| } | ||
| } | ||
| tm.lock.Unlock() | ||
|
|
||
| // Check receipts without holding lock (RPC calls can be slow). | ||
| confirmedNonces := make(map[uint64]*types.Receipt) | ||
| for nonce, txMap := range snapshot { | ||
| for txHash, start := range txMap { | ||
| receipt, err := tm.backend.TransactionReceipt(tm.ctx, txHash) | ||
| if err != nil { | ||
| // wait for a few blocks to be mined before considering a transaction not existing | ||
| transactionWatchNotFoundTimeout := 5 * tm.pollingInterval | ||
| if errors.Is(err, ethereum.NotFound) && watchStart(watches).Before(time.Now().Add(transactionWatchNotFoundTimeout)) { | ||
| // if both err and receipt are nil, there is no receipt | ||
| // the reason why we consider this only potentially cancelled is to catch cases where after a reorg the original transaction wins | ||
| if errors.Is(err, ethereum.NotFound) && start.Before(time.Now().Add(5*tm.pollingInterval)) { | ||
| continue | ||
| } | ||
| return err | ||
| } | ||
| if receipt != nil { | ||
| // if we have a receipt we have a confirmation | ||
| confirmedNonces[nonceGroup] = receipt | ||
| confirmedNonces[nonce] = receipt | ||
| break // found receipt for this nonce, no need to check other tx hashes | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for nonceGroup := range tm.watchesByNonce { | ||
| if _, ok := confirmedNonces[nonceGroup]; ok { | ||
| continue | ||
| var unconfirmedNonces []uint64 | ||
| for nonce := range snapshot { | ||
| if _, ok := confirmedNonces[nonce]; !ok { | ||
| unconfirmedNonces = append(unconfirmedNonces, nonce) | ||
| } | ||
| } | ||
|
|
||
| // Check for cancellations. | ||
| var cancelledNonces []uint64 | ||
| if len(unconfirmedNonces) > 0 { | ||
| oldNonce, err := tm.backend.NonceAt(tm.ctx, tm.sender, new(big.Int).SetUint64(block-tm.cancellationDepth)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not see the reason to call |
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if nonceGroup < oldNonce { | ||
| cancelledNonces = append(cancelledNonces, nonceGroup) | ||
| for _, nonce := range unconfirmedNonces { | ||
| if nonce < oldNonce { | ||
| cancelledNonces = append(cancelledNonces, nonce) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // notify the subscribers and remove watches for confirmed or cancelled transactions | ||
| // Notify subscribers and cleanup. | ||
| tm.lock.Lock() | ||
| defer tm.lock.Unlock() | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could stop checking other txHashes here, for this nonce, because we found the receipt?