5 suggestions
Optional improvements
[suggestion] · Target changes discard usable reorganisation read-ahead
Higher-work descendants stored during successive read rounds can repeatedly postpone the first disconnect despite unchanged useful reads; this delay ends when target changes pause, and its practical cost is unmeasured.
Location: src/validation.cpp:5141 (head).
Details and suggested fix
FinishNewBlock() publishes candidates under cs_main without taking m_chainstate_mutex, so a stored descendant can change FindMostWorkChain() while ReadForReorg() waits. This comparison then discards both read vectors and starts another round, even when extending the same competing branch leaves the fork, disconnect data and required connecting prefix unchanged. Before the first disconnect, the behind-starting-tip guard does not apply and there is no retry bound. Base selected the target and planned the step in one hold.
Limit consecutive replanning rounds per step. After that limit, still reselect the target under cs_main, but consume completed reads only through TakeDisconnect() and TakeConnect(), using the existing synchronous fallback for missing entries. This preserves target reselection while trading possible extra under-lock reads for bounded preparatory delay.
[suggestion] · Reorganisation tests miss target changes across the lock drop
Both added reorganisation tests keep the candidate target fixed, so no assertion depends on refreshing it after the new cs_main lock drop.
Location: src/test/validation_chainstate_tests.cpp:405 (head).
Details and suggested fix
CheckReorgReadAhead() builds both branches before synchronous activation and introduces no candidate during the read phase. Its depth-18 variant tests misses beyond the 16-entry limit, not replacement of a completed plan. A regression confined to the reorg_reads->target mismatch case can therefore evade the added assertions. Add a deterministic barrier in the off-lock read phase, store a distinct higher-work branch under cs_main without activating it, then resume. Assert that the refreshed plan names the new target from the original tip, that superseded entries are never consumed for different indices, and that the resulting tip and coins match the new branch. Eventual convergence alone would not test reselection before the first disconnect.
[suggestion] · The initial page-cache sweep precedes window-block validation
On Linux, the first successful disk-backed window read after restart can sweep thousands of older block files before submitting validation, delaying ordered publication if it is the lowest block; the latency is unmeasured.
Location: src/validation.cpp:4121 (head).
Details and suggested fix
m_block_files_released starts at zero, so resuming IBD at a high file number makes the winning ReleaseBlockFilesBelow() call visit [0, pos.nFile - 16). It attempts to open each file and calls posix_fadvise() and close() for those it finds. This happens before Submit(task), whereas base submitted validation immediately after reading. If the sweeping block is lowest in flight, height-ordered publication waits for these calls, even though the advice is not required for validation. Queue the advice as a separate RunBlockIO() task using the copied threshold and a BlockManager reference, rather than staged, so submitting this block's validation no longer waits for the sweep.
[suggestion] · Failed undo read-ahead lacks fallback coverage
The added tests never exercise DisconnectTip() retaining a prefetched block while retrying a failed undo read under cs_main.
Location: src/validation.cpp:3775 (head).
Details and suggested fix
ReadForDisconnect() assigns read.block before attempting the undo read. If that read fails, DisconnectTip() keeps the block but calls the synchronous Chainstate::DisconnectBlock() wrapper, whose BLOCK_FILE_WORK_UNDER_CS_MAIN scope permits the fallback undo read. The reorganisation fixtures inject no failure and contain only coinbase transactions; the copied-position checksum test checks rejection without subsequent disconnection. Add a one-shot test hook that fails only the speculative undo read, leaving the fallback working, and reorganise away from a block that spends an output. With DEBUG_LOCKORDER, assert successful activation, the expected tip, restoration of the spent coin and mempool consistency. This distinguishes partial read-ahead from the entirely absent entries covered by the deep-reorganisation test.
[suggestion] · ReorgReads is missing from the memory ledger
The memory ledger omits reorganisation read-ahead, which adds concurrent undo retention to existing block pinning and has only a 16-per-side count bound; no peak was measured or OOM shown.
Location: src/validation.cpp:4559 (head).
Details and suggested fix
ReadForReorg() retains every result until all tasks finish: up to 16 disconnect CBlocks with CBlockUndo, plus 16 connect blocks. The worker count does not bound this retained set. Undo contains spent prevouts and their scripts, so the count limit alone does not describe the byte peak. Base already retained connecting blocks through reorganisation signals; the distinct new retention includes undo objects that base read and consumed one at a time. Add ReorgReads to util/byte_ledger.h's holder summary with its count-only limit and a rationale or expected memory range for choosing 16. Filling that bound requires a deep reorganisation, not a normal tip extension.
Background
Ralph's summary of historical context for the behaviour affected by this change. The historical record reviewed here is incomplete.
Documented decisions
The progress-versus-responsiveness tradeoff predates this series. Pieter Wuille's 2014 lock-splitting change stopped a successful activation step once chainwork exceeded the old tip, explicitly describing that as a better position from which to release the lock. Its outer loop selected the most-work chain again under cs_main.
The read-ahead endpoint—the first connecting block that improves work over the old tip—follows an established activation boundary rather than a fixed number of blocks. Prefetching a branch is preparatory work; it does not make the previously selected target authoritative across a lock drop.