Skip to content

Conversation

@laanwj
Copy link
Member

@laanwj laanwj commented Apr 15, 2014

  • In wallet and GUI code LOCK cs_main as well as cs_wallet when necessary
  • In main.cpp SendMessages move the TRY_LOCK(cs_main) up, to encompass the call
    to IsInitialBlockDownload.
  • Make ActivateBestChain, AddToBlockIndex, IsInitialBlockDownload, InitBlockIndex acquire the cs_main lock

Fixes #3997

Copy link
Member

Choose a reason for hiding this comment

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

As IsInitialBlockDownload gets its own LOCK(cs_main), is this necessary? (I really prefer to keep the amount of time cs_main is locked minimal, as it's likely the most common cause for latency in processing)

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not strictly necessary (it would get the lock either way), however, what is the use of a TRY_LOCK if a few lines before we do a non-try LOCK() of the same lock? That's why I moved the TRY_LOCK up, so that if the try fails it won't do IsInitialBlockDownload either...

Copy link
Member

Choose a reason for hiding this comment

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

Good point.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree that keeping the time that cs_main is locked down would be good.It's even worse than the Big Kernel Lock :) Finer-grained locking would be in order.

In the meantime: We could do TRY_LOCK and release the lock again multiple times?

Copy link
Member

Choose a reason for hiding this comment

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

Let's do that in a different PR, if needed.

@laanwj laanwj added this to the 0.9.2 milestone Apr 16, 2014
@laanwj
Copy link
Member Author

laanwj commented Apr 16, 2014

Hmm - although it improves things, this is not yet complete. There are places left in the GUI code where chainActive is used directly without acquiring the appropriate lock. Will add a commit to fix these.

Edit: fixed, should be complete now

laanwj added 2 commits April 17, 2014 16:46
All functions that use ChainActive but do not aquire the cs_main
lock themselves, need to be called with the cs_main lock held.

This commit adds assertions to all externally callable functions
that use chainActive or chainMostWork.

This will flag usages when built with -DDEBUG_LOCKORDER.
- In wallet and GUI code LOCK cs_main as well as cs_wallet when
  necessary
- In main.cpp SendMessages move the TRY_LOCK(cs_main) up, to encompass the call
  to IsInitialBlockDownload.
- Make ActivateBestChain, AddToBlockIndex, IsInitialBlockDownload,
  InitBlockIndex acquire the cs_main lock

Fixes bitcoin#3997
@BitcoinPullTester
Copy link

Automatic sanity-testing: PASSED, see http://jenkins.bluematt.me/pull-tester/55a1db4fa2cf62b9766ef382c1e14b3ecbdf67fe for binaries and test log.
This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/
Contact BlueMatt on freenode if something looks broken.

@cozz
Copy link
Contributor

cozz commented Apr 18, 2014

Is it really necessary for the 4 lock-functions (isLockedCoin,lockCoin,unlockCoin,listLockedCoins) to
acquire a cs_main lock?

@laanwj
Copy link
Member Author

laanwj commented Apr 18, 2014

@cozz It's better to err on the safe side. If it happens that the cs_wallet lock is acquired before the cs_main lock it will result in a deadlock.

I've added cs_main locks everywhere before that the cs_wallet lock is taken except if I could clearly see within the function that the main lock is not necessary (for example, for address book manipulation).

(This does make the issue visible that almost everything in the wallet code depends on the cs_main lock)

@sipa
Copy link
Member

sipa commented Apr 18, 2014

ACK.

Some of the wallet function may survive with smaller locks (in particular, just locking the mempool rather than whole of main), but let's do that later.

@laanwj laanwj merged commit 55a1db4 into bitcoin:master Apr 22, 2014
laanwj added a commit that referenced this pull request Apr 22, 2014
55a1db4 Solve chainActive-related locking issues (Wladimir J. van der Laan)
e07c943 Add AssertLockHeld for cs_main to ChainActive-using functions (Wladimir J. van der Laan)
@laanwj laanwj mentioned this pull request Apr 23, 2014
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

crash in CMerkleTx::GetDepthInMainChainINTERNAL()

5 participants