blockstorage: lock blocksdir in BlockManager #36799

pull pzafonte wants to merge 2 commits into bitcoin:master from pzafonte:blockstorage-lock-blocksdir changing 3 files +63 −2
  1. pzafonte commented at 12:59 AM on November 8, 2026: contributor

    While playing around with pruning for the kernel, I noticed that only bitcoind's init locks the blocks directory (AppInitLockDirectories). The kernel doesn't.

    Pointing a kernel program at a running node's data directory fails, but only because LevelDB locks blocks/index. To avoid a second copy of the chain, the obvious workaround is to use the node's blocks directory with the program's own data directory, or in-memory databases. It starts without error, but with an empty block index BlockManager writes from offset 0 of the node's blk00000.dat, which FlatFileSeq::Open opens in place with "rb+". The node keeps running and serves the overwritten bytes as its own blocks.

    #31674 added the blocks directory lock to bitcoind and noted that "the kernel does not currently do any dir locking, but it should". This PR follows sedited's suggestion to add the lock to the BlockManager constructor, and is based on the lock_blocksdir_raii commit.

    BlockManager holds .lock for its whole lifetime through BlocksDirLock, its first member, so the directory is locked before xor.dat is touched. Creating a chainstate manager on a blocks directory locked by another process, bitcoind or a kernel user, fails.

    In bitcoind, init already holds the lock, so LockDirectory returns success without locking again. The lock is released when the BlockManager is destroyed rather than at exit. That happens at shutdown, and when a GUI user retries a failed chainstate load with a reindex, after which the new BlockManager reacquires it. A blocks directory used through the kernel now has to be writable, as bitcoind already required.

    Added a regression test to feature_filelock.py.

  2. blockstorage: lock blocksdir in BlockManager
    Only bitcoind's init took this lock, so a kernel user with its own
    block index could overwrite a running node's block files.
    8711bbee40
  3. test: check bitcoin-chainstate can't use a running node's blocksdir 4e0f57f2d6
  4. DrahtBot added the label Block storage on Nov 8, 2026
  5. DrahtBot commented at 12:59 AM on November 8, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  6. KY-U commented at 9:35 PM on November 27, 2026: contributor

    Ran the new feature_filelock.py case without the changes from 8711bbee40198e0fc407e2f3ad45f8f651da7975. Verified bitcoin-chainstate failed at the exit code assertion. After restoring the fix and rebuilding, the same test passed.

    One non-blocking documentation suggestion, maybe in a follow up: could the blocks_directory parameter docs in src/kernel/bitcoinkernel.h mention that creating a chainstate manager requires opening the lock file, similar to the documentation of AppInitInterfaces() in src/init.h? That would make the requirement visible to kernel callers where they choose the directory.

    <details> <summary>maybe similar to this</summary>

    diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h
    index a88397763e..cf3e20ae66 100644
    --- a/src/kernel/bitcoinkernel.h
    +++ b/src/kernel/bitcoinkernel.h
    @@ -1271,7 +1271,8 @@ BITCOINKERNEL_API const btck_BlockTreeEntry* btck_block_tree_entry_get_ancestor(
      * [@param](/bitcoin-bitcoin/contributor/param/)[in] data_directory   Path string of the directory containing the chainstate data. If the directory
      *                             does not exist yet, it will be created.
      * [@param](/bitcoin-bitcoin/contributor/param/)[in] blocks_directory Path string of the directory containing the block data. If the directory
    - *                             does not exist yet, it will be created.
    + *                             does not exist yet, it will be created. Creating the chainstate manager
    + *                             fails if another process holds the lock on this directory.
      * [@return](/bitcoin-bitcoin/contributor/return/)                     The allocated chainstate manager options, or null on error (e.g. if a path is invalid).
      */
     BITCOINKERNEL_API btck_ChainstateManagerOptions* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chainstate_manager_options_create(
    

    </details>


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-12-04 01:52 UTC

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