Skip to content

Conversation

skeees
Copy link
Contributor

@skeees skeees commented Apr 15, 2018

Resolves #12978

@skeees
Copy link
Contributor Author

skeees commented Apr 15, 2018

@ryanofsky / other qt experts - the gui signals still get called out of cs_main - do you see any potential issues there?

@ryanofsky
Copy link
Contributor

@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 NotifyBlockTip documentation to say that notifications can arrive at the same time from multiple threads and there are no ordering guarantees (or whatever the current situation is).

@skeees
Copy link
Contributor Author

skeees commented Apr 15, 2018

#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

@TheBlueMatt
Copy link
Contributor

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.

@ryanofsky
Copy link
Contributor

I think this PR is fine as-is

I think it would be good for this PR to update NotifyBlockTip documentation if notifications can come out of order, regardless of whether there are currently any gui bugs.

@skeees
Copy link
Contributor Author

skeees commented Apr 15, 2018

@ryanofsky - i'll update docs
@TheBlueMatt - will evaluate whether feasible to cherry-pick that commit into this pr

@laanwj
Copy link
Member

laanwj commented Apr 16, 2018

@ryanofsky / other qt experts - the gui signals still get called out of cs_main - do you see any potential issues there?

I expect the GUI doesn't care - it transfers the signals to a different thread anyway.

@jnewbery
Copy link
Contributor

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.

@TheBlueMatt
Copy link
Contributor

TheBlueMatt commented Apr 16, 2018

Heh, @jnewbery pointed out that a9db3da doesn't actually fix it, I was looking at the wrong callback, sorry for the noise.

@TheBlueMatt
Copy link
Contributor

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.


// 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);
Copy link
Contributor

@promag promag Apr 16, 2018

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.

}
// 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);
Copy link
Contributor

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?

@skeees
Copy link
Contributor Author

skeees commented Apr 16, 2018

Consensus seems to be to update the gui callbacks in cs_main as well (until #11856 which moves these gui callbacks to the ValidationInterface) - latest commit does that and addresses @promag's other review comment

@jnewbery
Copy link
Contributor

ACK b6f486c85c85e024f03941cbbeadb348ddceaaf8. Commits can be squashed.

@skeees skeees force-pushed the updatedblocktip-race branch from b6f486c to 713c066 Compare April 16, 2018 18:02
@skeees
Copy link
Contributor Author

skeees commented Apr 16, 2018

Squashed

@jnewbery
Copy link
Contributor

ACK 713c066

→ git diff b6f486c 713c066

Copy link
Contributor

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

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

utACK 713c066cdc1cc4750dfa1d41149621356ff2ad04

// 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);
Copy link
Contributor

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).

Copy link
Member

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).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed - updated

Copy link
Contributor

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.

Copy link
Member

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.

Copy link
Contributor

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
@skeees skeees force-pushed the updatedblocktip-race branch from 713c066 to d86edd3 Compare April 16, 2018 22:04
@promag
Copy link
Contributor

promag commented Apr 17, 2018

utACK d86edd3.

@sdaftuar
Copy link
Member

utACK d86edd3

@laanwj laanwj merged commit d86edd3 into bitcoin:master Apr 17, 2018
laanwj added a commit that referenced this pull request Apr 17, 2018
d86edd3 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen)

Pull request description:

  Resolves #12978

Tree-SHA512: 2aed09434cd8dbf541ea75462070b73ee87ff31409bede210f6999ffee4a37e32202a289efd37609485d4cbdfe134fe4660a10bfb41e8a8acdba7cd0b61b8780
@skeees skeees deleted the updatedblocktip-race branch April 21, 2018 13:41
@laanwj laanwj added this to the 0.16.1 milestone May 3, 2018
fanquake pushed a commit to fanquake/bitcoin that referenced this pull request May 18, 2018
Ensures that callbacks are invoked in the order in which the chain is updated
Resolves bitcoin#12978

GitHub-Pull: bitcoin#12988
Rebased-From: d86edd3
laanwj added a commit that referenced this pull request May 24, 2018
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
HashUnlimited pushed a commit to chaincoin/chaincoin that referenced this pull request Jun 29, 2018
Ensures that callbacks are invoked in the order in which the chain is updated
Resolves bitcoin#12978

GitHub-Pull: bitcoin#12988
Rebased-From: d86edd3
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Nov 10, 2020
d86edd3 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen)

Pull request description:

  Resolves bitcoin#12978

Tree-SHA512: 2aed09434cd8dbf541ea75462070b73ee87ff31409bede210f6999ffee4a37e32202a289efd37609485d4cbdfe134fe4660a10bfb41e8a8acdba7cd0b61b8780
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Nov 12, 2020
d86edd3 Hold cs_main while calling UpdatedBlockTip() and ui.NotifyBlockTip (Jesse Cohen)

Pull request description:

  Resolves bitcoin#12978

Tree-SHA512: 2aed09434cd8dbf541ea75462070b73ee87ff31409bede210f6999ffee4a37e32202a289efd37609485d4cbdfe134fe4660a10bfb41e8a8acdba7cd0b61b8780
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Nov 17, 2020
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>
random-zebra added a commit to PIVX-Project/PIVX that referenced this pull request Apr 4, 2021
…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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

p2p_compactblocks.py failing occasionally on master
8 participants