Skip to content

Inconsistent locking behavior in LoadChainTip #15967

Closed

Description

@deadalnix

LoadChainTip requires cs_main to be locked. Under some conditions, it may call ActivateBestChain. However, ActivateBestChain requires cs_main NOT to be taken when called.

Activity

  1. changed the title [-]Inconsistent licking behavior in LoadChainTip[/-] [+]Inconsistent locking behavior in LoadChainTip[/+] on May 6, 2019
  2. practicalswift commented on May 7, 2019

    @practicalswift
    Contributor

    Confirming that there appears to be an inconsistency:

    $ grep -B11 '^        if (!ActivateBestChain(state, chainparams)) {' src/validation.cpp
    bool LoadChainTip(const CChainParams& chainparams)
    {
        AssertLockHeld(cs_main);
    
        if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;
    
        if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
            // In case we just added the genesis block, connect it now, so
            // that we always have a chainActive.Tip() when we return.
            LogPrintf("%s: Connecting genesis block...\n", __func__);
            CValidationState state;
            if (!ActivateBestChain(state, chainparams)) {
    $ grep -A2 -E '^bool ActivateBestChain' src/validation.cpp
    bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
        return g_chainstate.ActivateBestChain(state, chainparams, std::move(pblock));
    }
    $ grep -A5 -E '^bool CChainState::ActivateBestChain\(' src/validation.cpp
    bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
        // Note that while we're often called here from ProcessNewBlock, this is
        // far from a guarantee. Things in the P2P/RPC will often end up calling
        // us in the middle of ProcessNewBlock - do not assume pblock is set
        // sanely for performance or correctness!
        AssertLockNotHeld(cs_main);
    $ git diff
    diff --git a/src/validation.h b/src/validation.h
    index 7ab6adaf3..5f19f32b4 100644
    --- a/src/validation.h
    +++ b/src/validation.h
    @@ -257,7 +257,7 @@ bool GetTransaction(const uint256& hash, CTransactionRef& tx, const Consensus::P
      * May not be called with cs_main held. May not be called in a
      * validationinterface callback.
      */
    -bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>());
    +bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>()) LOCKS_EXCLUDED(cs_main);
     CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
    
     /** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
    
    $ make
    …
     validation.cpp:3959:14: error: cannot call function 'ActivateBestChain' while mutex 'cs_main' is held [-Werror,-Wthread-safety-analysis]
            if (!ActivateBestChain(state, chainparams)) {
                 ^
    1 error generated.
    

    I just submitted #15971 to add compile-time checking for some negative locking requirements in the validation code, but that PR does not address this case.

    Updated: Had totally forgotten that I submitted #15721 -- which adds LOCKS_EXCLUDED(cs_main) to CChainState::ActivateBestChain -- back in April too :-)

  3. added a commit that references this issue on May 17, 2019
    a822a0e
  4. locked as resolved and limited conversation to collaborators on Dec 16, 2021
  5. added a commit that references this issue on Jul 19, 2026
    928c646
  6. added a commit that references this issue on Aug 30, 2026
    0c909d6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions