-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Hold cs_main while calling UpdatedBlockTip() signal #12988
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
Conversation
@ryanofsky / other qt experts - the gui signals still get called out of cs_main - do you see any potential issues there? |
Would have to look into it more, but if the problem is NotifyBlockTip events being sent to the GUI in the wrong order, we should determine whether one of @TheBlueMatt's PRs fixes this. Assuming one does, it's probably sufficient for this PR to just update the |
#11856 is the one that I think you are referring to which eliminates that particular call to NotifyBlockTip and replaces it with the ValidationInterface signals |
I think this PR is fine as-is, the GUI I'd be surprised if it were broken, though the mining stuff may be (but #11856 pulls it all out to using validationinterface, which should be a less-likely-to-break-things fix as it doesn't introduce new lockorders). It'd also be nice to see the no-change-callbacks go away ala a9db3da since we're touching this code anyway. |
I think it would be good for this PR to update |
@ryanofsky - i'll update docs |
I expect the GUI doesn't care - it transfers the signals to a different thread anyway. |
Looks good to me. I don't think a9db3da needs to be pulled in - it's an orthogonal change for whether to fire notifications at all. It's useful, but no need to widen the scope of this PR. |
As for the uiInterface callback, I think its best to move it into the cs_main as well - as @laanwj points out in the GUI its immediately pushed to a queue for main thread execution so that should be fine, and the RPCNotifyBlockChange call should be cs_main-safe as it only notify_all()s and the cs_blockchange mutex is never taken with cs_main in another thread. Obviously this really sucks and risks future bugs and I'd like to see #11856 kill the uiInterface callback there entirely, but for a backport/fix, it looks pretty safe to me with current code. |
src/validation.cpp
Outdated
|
||
// Notify external listeners about the new tip. | ||
// Enqueue while holding cs_main to ensure that UpdatedBlockTip is called in the order in which blocks are connected | ||
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload); |
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.
Should add
AssertLockHeld(cs_main);
to CMainSignals::UpdatedBlockTip
? And a comment.
src/validation.cpp
Outdated
} | ||
// When we reach this point, we switched to a new tip (stored in pindexNewTip). | ||
|
||
// Notifications/callbacks that can run without cs_main | ||
|
||
// Notify external listeners about the new tip. | ||
GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload); | ||
|
||
// Always notify the UI if a new block tip was connected | ||
if (pindexFork != pindexNewTip) { | ||
uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip); |
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.
This means that the UI can present a tip that is not the tip?
ACK b6f486c85c85e024f03941cbbeadb348ddceaaf8. Commits can be squashed. |
b6f486c
to
713c066
Compare
Squashed |
ACK 713c066
|
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.
utACK 713c066cdc1cc4750dfa1d41149621356ff2ad04
src/validationinterface.cpp
Outdated
// Dependencies exist that require UpdatedBlockTip events to be delivered in the order in which | ||
// the chain actually updates. In order to ensure this, these signals should be enqueued | ||
// in the same critical section where the chain is updated | ||
AssertLockHeld(cs_main); |
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.
Bleh, if you're gonna assert this here you should probably assert it in pretty much every signal, as they all have ordering requirements. It also just seems kinda strange to do so cause its not really a requirement of the validationinterface, its a requirement that they be ordered, not done in one specific cs (eg the mempool ones could probably be ordered by mempool.cs instead).
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 was also not loving this AssertLockHeld() for the same reason. I'd prefer we drop it and just leave a comment explaining the ordering requirement (maybe with a comment mentioning the cs_main lock as one way to achieve it).
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.
Agreed - updated
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'm not a fan of spreading AssertLockHeld
either but in this case the current fix is to lock and having the assert here makes that explicitly required. With just the comment a future refactor can break that requirement and reintroduce the bug.
But it was just a suggestion.
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.
Note that the AssertLockHeld
wouldn't have prevented the caller from releasing cs_main after updating the tip, and reacquiring cs_main before the validation interface callbacks -- which would still result in violating the ordering requirements.
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.
@sdaftuar that's true and the same could be said to every AssertLockHeld
😛
Ensures that callbacks are invoked in the order in which the chain is updated Resolves bitcoin#12978
713c066
to
d86edd3
Compare
utACK d86edd3. |
utACK d86edd3 |
Ensures that callbacks are invoked in the order in which the chain is updated Resolves bitcoin#12978 GitHub-Pull: bitcoin#12988 Rebased-From: d86edd3
acdf433 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen) 5ff571e [wallet] [tests] Test disallowed multiwallet params (John Newbery) 4c14e7b [wallet] Fix zapwallettxes/multiwallet interaction. (John Newbery) 4087dd0 RPC Docs: gettxout*: clarify bestblock and unspent counts (David A. Harding) b8aacd6 [qa] Handle disconnect_node race (Suhas Daftuar) Pull request description: Backports: - #13201 [qa] Handle disconnect_node race - #13184 RPC Docs: gettxout*: clarify bestblock and unspent counts - #13030 [bugfix] [wallet] Fix zapwallettxes/multiwallet interaction. - #12988 Hold cs_main while calling UpdatedBlockTip() signal to the 0.16 branch. Tree-SHA512: 8f65002bbafaf9c436f89051b2d79bf6a668fbd07bd317c64af238ed4a7c8efe776864b739a7f2869f1e3daa16f2f4366a85f41b188f9c454879d2c7b309be50
Ensures that callbacks are invoked in the order in which the chain is updated Resolves bitcoin#12978 GitHub-Pull: bitcoin#12988 Rebased-From: d86edd3
d86edd3 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen) Pull request description: Resolves bitcoin#12978 Tree-SHA512: 2aed09434cd8dbf541ea75462070b73ee87ff31409bede210f6999ffee4a37e32202a289efd37609485d4cbdfe134fe4660a10bfb41e8a8acdba7cd0b61b8780
d86edd3 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen) Pull request description: Resolves bitcoin#12978 Tree-SHA512: 2aed09434cd8dbf541ea75462070b73ee87ff31409bede210f6999ffee4a37e32202a289efd37609485d4cbdfe134fe4660a10bfb41e8a8acdba7cd0b61b8780
d86edd3 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen) Pull request description: Resolves bitcoin#12978 Tree-SHA512: 2aed09434cd8dbf541ea75462070b73ee87ff31409bede210f6999ffee4a37e32202a289efd37609485d4cbdfe134fe4660a10bfb41e8a8acdba7cd0b61b8780 fix indentation Signed-off-by: pasta <pasta@dashboost.org>
…tChain + optimization 50dbec5 Add unit tests for signals generated by ProcessNewBlock() (furszy) f68251d Validation: rename one of the two instances using "bad-prevblk" to its correct description of "prevblk-not-found" (furszy) a51a755 Fix concurrency-related bugs in ActivateBestChain (Jesse Cohen) 198f435 Do not unlock cs_main in ABC unless we've actually made progress. (Matt Corallo) 8d15cf5 Optimize ActivateBestChain for long chains (Pieter Wuille) 8640be1 Fix fast-shutdown crash if genesis block was not loaded (Matt Corallo) ef24337 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen) f9d2ab3 Update ValidationInterface() documentation to explicitly specify threading and memory model (Jesse Cohen) cb9bb25 Update documentation for SingleThreadedSchedulerClient() to specify the memory model (Jesse Cohen) 4ea2048 Add Unit Test for SingleThreadedSchedulerClient (Jesse Cohen) Pull request description: Made some back ports and adaptations to validate further the work introduced in #2203 and #2209. Now that the scheduler thread is processing most of the chain events, the callbacks execution order must be verified to dispatch the events serially, ensuring a consistent memory state in each event processing invocation. There are some concurrency issues solved, as well a an optimization for larger chains for the ABC workflow. Each commit details them well. List: bitcoin#7917 (only fb8fad1) bitcoin#12988 bitcoin#13023 bitcoin#13247 bitcoin#13835 This is built on top of #2284 (and the reason that made me notice that problem). So, 2284 comes first, then this one. ACKs for top commit: Fuzzbawls: ACK 50dbec5 random-zebra: ACK 50dbec5 and merging... Tree-SHA512: 573144cc96d2caa49ed2f0887dc8c53b5aca0efd54b6a25626063ccb780da426f56b3c6b7491648e7abefc1960c82b1354a4ff2c51425bfe99adaa4394562608
Resolves #12978