Review report

bitcoin/bitcoin: PR #36799

1 minor finding · 3 suggestions

Findings

Link

[minor] · BlocksDirLock can release another live manager's lock

A failed duplicate kernel-manager creation erases the shared blocks-directory lock while the original manager remains alive, allowing another local writer with an independent index to acquire it.

Location: src/node/blockstorage.cpp:1198 (head).

Details and suggested fix

An external kernel embedder can keep manager A alive and call btck_chainstate_manager_create again with the same disk-backed options. LockDirectory reuses A's map entry without recording another owner. The second BlockManager constructs this guard before opening BlockTreeDB, so LevelDB's rejection of the duplicate index open unwinds the guard and erases A's FileLock. The API returns null while A remains usable without its directory lock. Another local process with write access and its own index can then acquire .lock and make conflicting writes to the block files. Destroying either of two successfully created managers sharing the directory has the same ownership problem. Use shared ownership tokens, including init's reservation, so only the last owner releases the OS lock. Add a regression test that keeps A alive across failed duplicate creation and checks contention from another process. No in-tree caller creating overlapping managers on one blocks directory was found, and production occurrence remains unconfirmed. The base kernel had no directory lock, so this is a gap in the new protection rather than a regression.


Optional improvements

Link

[suggestion] · The file-lock test misses kernel-owned contention

The new test checks rejection against bitcoind's existing lock, so changing the kernel acquisition to probe-only would leave both assertions passing.

Location: test/functional/feature_filelock.py:65 (head).

Details and suggested fix

The test starts node1 first, so TryLock fails before the helper reaches its successful-lock retention branch. Calling LockDirectory(..., probe_only=true) from BlocksDirLock would therefore produce the same return code and message. Add a reverse-order case: start bitcoin-chainstate on a fresh <root>/regtest with stdin open, wait for its flushed input prompt after successful ChainMan construction, and assert that bitcoind with -blocksdir=<root> and its own datadir fails with the blocks-directory lock error. Separate indexes prevent LevelDB from making the assertion pass accidentally. Close stdin, wait for the tool to exit, and check that bitcoind can start. That last step shows release at process exit; isolating destructor-time release requires a helper that destroys the manager while its process remains alive.


Link

[suggestion] · Directory-lock comments still promise process-long ownership

BlockManager destruction now releases the blocks-directory lock during production execution, but init and dir_locks comments still describe retention until exit or bulk cleanup.

Location: src/node/blockstorage.cpp:1198 (head).

Details and suggested fix

The GUI reindex retry resets node.chainman before constructing its replacement, so this destructor can release init's blocks-directory lock while the process remains alive. AppInitLockDirectories at src/init.cpp:1297 still says the directory locks are held until exit, and the map comment at src/util/fs_helpers.cpp:46-49 omits UnlockDirectory as a release path. Update the comments to distinguish the datadir lock from the blocks-directory lock and describe explicit removal. If the ownership fix retains init's reference until exit, keep the init wording consistent with that final lifetime instead.


Link

[suggestion] · Kernel creation docs omit blocks-directory lock requirements

The public creation docs omit the new .lock write-access and cross-process locking requirements, leaving these null-return conditions unexplained.

Location: src/node/blockstorage.cpp:1185 (head).

Details and suggested fix

BlocksDirLock now throws when the lock file cannot be opened for writing or its OS lock cannot be obtained, including when another process holds it. btck_chainstate_manager_create logs the exception and returns null, but its public comment only says “or null on error.” Add a note documenting lock-file write access, cross-process exclusion, and the lock's lifetime under the final ownership model. Describe exclusion as cross-process rather than in-process, since LockDirectory currently accepts an existing same-process entry.


Coverage details