Solve chainActive-related locking issues #4058

pull laanwj wants to merge 2 commits into bitcoin:master from laanwj:2014_04_walletlock_main changing 8 files +267 −252
  1. laanwj commented at 3:43 PM on April 15, 2014: member
    • 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

  2. in src/main.cpp:None in b45e335dbf outdated
    4201 | @@ -4186,6 +4202,10 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
    4202 |              }
    4203 |          }
    4204 |  
    4205 | +        TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
    


    sipa commented at 3:49 PM on April 15, 2014:

    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)


    laanwj commented at 4:08 PM on April 15, 2014:

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


    sipa commented at 4:11 PM on April 15, 2014:

    Good point.


    laanwj commented at 4:18 PM on April 15, 2014:

    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?


    sipa commented at 9:47 PM on April 18, 2014:

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

  3. laanwj added this to the milestone 0.9.2 on Apr 16, 2014
  4. laanwj commented at 11:14 AM on April 16, 2014: member

    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

  5. Add AssertLockHeld for cs_main to ChainActive-using functions
    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.
    e07c943ce8
  6. Solve chainActive-related locking issues
    - 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
    55a1db4fa2
  7. BitcoinPullTester commented at 11:22 AM on April 18, 2014: none

    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.

  8. cozz commented at 3:57 PM on April 18, 2014: contributor

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

  9. laanwj commented at 4:49 PM on April 18, 2014: member

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

  10. sipa commented at 9:48 PM on April 18, 2014: member

    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.

  11. wtogami commented at 11:35 AM on April 20, 2014: contributor

    http://nightly.bitcoin.it/0.9.99.0-20140420-4a102fa/ https://github.com/nightlybitcoin/bitcoin/commits/0.9.99.0-20140420-4a102fa This nightly build and later will contain this PR to more easily expose it to expose it to public testing.

  12. laanwj merged this on Apr 22, 2014
  13. laanwj closed this on Apr 22, 2014

  14. laanwj referenced this in commit 2bbecc84e2 on Apr 22, 2014
  15. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me