Review report

core: PR #35071

1 major finding · 3 minor findings · 1 suggestion

Earlier concerns neither republished nor resolved: 1 minor finding. See the Run archive for their assessments.

Findings

Link

[major] · Resume checkpoint can be saved without the block index it lets the next run skip

If the shutdown flush fails or is cut short after an interrupt, the next start skips blocks that never reached the block index; base repairs the same state by rescanning.

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

Details and suggested fix

During the scan, AcceptBlock only calls FlushStateToDisk(NONE). That writes nothing while pruning is off for the reindex, so the index entries and file info added in this run stay in m_dirty_blockindex and m_dirty_fileinfo. On interrupt, this line writes P/O straight to LevelDB. The write is unsynced, but it survives a process kill. The dirty entries are written only later, by Shutdown's ForceFlushStateToDisk after the initload thread is joined (init.cpp:329, 361), and that call only logs a warning if it fails. For example, FlushStateToDisk returns before WriteBlockIndexDB when the blocks directory has less than 50 MiB free. A SIGKILL from a service stop timeout, or a power loss during the flush, leaves the same state.

On the next start the reindex flag is still set, so the loop resumes at P+1. Blocks from the skipped files are missing from the index, and later blocks whose parents are missing stay in the orphan map. WriteReindexingComplete then durably clears the reindex state. The node has to download those blocks again unless the operator notices and runs -reindex. In base, the restart rescans from blk00000.dat and AcceptBlock re-adds any missing entry, so this state repairs itself. Write the dirty block index before the checkpoint, and write P/O only if that succeeded. For example, call FlushStateToDisk(FORCE_SYNC) under cs_main and check its result, or put P/O in the same synced batch as WriteBatchSync.


Link

[minor] · Reindex completion is synced to disk before the scanned block index is written

With the new synced erase, a power loss right after the scan leaves the reindex flag cleared while this run's block index entries are still unwritten.

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

Details and suggested fix

WriteReindexingComplete erases R/P/O with fSync=true right after the scan loop. No write-mode flush happens during the scan, so the index entries and file info added in this run are still only in memory. They are written by the first flush that writes during ActivateBestChains. That happens on the periodic 50-70 minute interval, timed from the process's first FlushStateToDisk call, or when the coins cache fills. For a short resumed run this can be close to an hour after the scan ends. If power is lost or the OS crashes before then, the restart sees no reindex flag and does not rescan, so the node has to download the missing blocks again.

Base used an unsynced Erase, which already had this problem for a process crash, so the regression is limited to power loss and OS crashes. The comment in blockstorage.h aims for the wrong invariant: when the index has not been written, a node that still thinks a scan is required is the safe outcome. The motivating problem also does not happen on a clean shutdown in base. The later synced WriteBatchSync makes the earlier unsynced erase durable, because the LevelDB log is sequential. Write the block index before clearing the keys, for example with WriteBlockIndexDB under cs_main, or clear them in the same synced batch.


Link

[minor] · Resuming past blk00000.dat with -reindex-chainstate skips genesis activation

After an interrupted -reindex, a restart with -reindex-chainstate aborts on the first newly accepted block when -checkblockindex is enabled, which is the default on regtest.

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

Details and suggested fix

-reindex-chainstate wipes only the coins database (init.cpp:1349, 1391). The block index, the reindex flag and the checkpoint from the interrupted -reindex therefore survive. Because the coins view is wiped, LoadChainTip is skipped (chainstate.cpp:120-126) and the active chain is empty. In base, the continued scan reaches the already-indexed genesis block in blk00000.dat. LoadExternalBlockFile then calls ActivateBestChain because ActiveHeight() is -1 (validation.cpp:5043). With the checkpoint, the loop starts at last_completed + 1 and never sees genesis.

The first block that is not yet indexed reaches AcceptBlock. Its CheckBlockIndex call (validation.cpp:4298) asserts that the block index has at most one entry while the active chain is empty (5133-5134). Assertions stay enabled in release builds. This needs at least one not-yet-indexed block after the checkpoint, which is the normal case after an interrupt. With consistency checks off (the mainnet default) nothing crashes, but init waits for a tip until the remaining scan and ActivateBestChains finish. Before resuming past file 0, activate the chain when ActiveHeight() is -1 and genesis is already indexed, or fall back to start_file = 0. A restart case using -reindex-chainstate would cover it.


Link

[minor] · Interrupted-reindex test depends on the stop arriving before the last file is scanned

If the importer scans the remaining -fastprune files before the stop RPC is handled, the test fails even though the code is correct.

Location: test/functional/feature_reindex.py:128 (head).

Details and suggested fix

busy_wait_for_debug_log yields before it starts reading the log (test_node.py:613-615). stop_node is therefore sent only after wait_for_rpc_connection returns and the blk00001.dat line has been seen. ImportBlocks does not wait for the test at that boundary and keeps scanning. If the remaining files are finished before the stop request is handled, line 134 fails. No checkpoint is written in that case, so the 'Resuming reindex' expectation in the next run fails too. The test comment already says blk00001 'probably' is not processed. Use a deterministic, test-only interruption at a chosen file boundary instead of relying on RPC startup and stop latency. How often this fails depends on machine speed and load; it was not measured.


Optional improvements

Link

[suggestion] · Resume test never restores a non-empty orphan map

A bug that drops or misrestores DB_REINDEX_ORPHAN_BLOCKS would still pass the new test.

Location: test/functional/feature_reindex.py:106 (head).

Details and suggested fix

reindex_resume_after_interrupt uses node 1, whose blocks were downloaded in order from node 0. The existing out_of_order() swap only touches node 0's blk00000.dat. So the orphan map saved at interruption is almost certainly empty, and the test only checks the resume log lines and the final height. Add a case where a child is stored in a file before the checkpoint and its parent in a later file, for example by swapping two equal-size blocks across a file boundary on node 1 as out_of_order() does. Interrupt after the child's file, resume, and assert 'Processing out of order child' and the expected tip in the resumed run, while keeping the assertion that completed files are not rescanned. A unit test that round-trips WriteReindexProgress/ReadReindexProgress with duplicate parent keys would also cover the serialization.


Design and approach

Measure base continuation (a restart without -reindex after interrupting late in the scan) against this branch on mainnet-sized data and slow storage, and restate the problem as the re-read cost. If that cost is a small share of the original scan, keep the existing behavior rather than adding persisted checkpoint and orphan state, which this review found has crash-ordering problems. The PR's premise that an interrupted reindex 'will start over at blk00000.dat' overstates the baseline. In base, a restart without -reindex loads the persisted index, keeps the reindex flag and skips AcceptBlock for already-indexed blocks. The only repeated cost is re-reading the already-scanned files. A maintainer answered the #30424 line that the PR quotes. A reviewer here raised the same point, and the author agreed they had not realized it and would measure. No measurement has been supplied, while the mechanism adds persisted state with verified crash-ordering defects. The candidate's own 'evidence that would change this assessment' (ordinary reindex already resumes accepted work) is confirmed by source, so needs_motivation fits better than worth_pursuing. Blind alternative 1 (per-file durable cursor) brings back the per-file writes the author removed after review. Blind alternative 2 (byte offset) adds complexity for little gain. Neither was published. 1 2 3 4

Coverage details

Earlier concerns

Earlier reports raised these, with the titles and severities shown there. This review neither republished nor resolved them.