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


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-11-13 23:51 UTC

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