Bugfix: RPC/Mining: getblocktemplate: Delay updating nTransactionsUpdatedLast and time_start until after the new template is cached #30088
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.
This is needed to avoid parallel calls from incorrectly using the stale cached template or (after an OOM) dereferencing a nullptr pblocktemplate
Considering memory overcommit, the worst case scenario likely never occurs, but that possibility is the clear bugfix here.
Aside from that, I wonder if using the stale cache in this race scenario (which is what could happen if this isn't fixed) is better than having N threads all making a new block (with this fix).
A middle ground (and perhaps cleaner than a bunch of static variables) might be to take a lock and have racing requests all wait on the same block creation, but that is not implemented here.