validation: delete pruned files after a failed flush #36735

Open
pzafonte wants to merge 4 commits from prune-unlink-after-retry into master AGit
pzafonte commented 2026-11-02 22:09:42 +00:00 (Migrated from github.com)

If a flush that prunes block files fails before the block index write, the files are already marked pruned in memory, but setFilesToPrune is a local in FlushStateToDisk(). The next successful flush writes the pruned file info and never deletes the blk/rev files. Only ScanAndUnlinkAlreadyPrunedFiles() deletes them, at the next startup.

This is number 7 of ryanofsky's post-merge review of #35714. The block file flush failure from #35714 and the blocks directory CheckDiskSpace() failure both return before that write.

I worked on this because it lets me test my kernel pruning refactor (#36738, for #35627 and kernel-node/kernel-node#111) against the issue he describes. The refactor moves the flush, block index write and file deletion that a pruning flush runs from FlushStateToDisk() into WriteBlockStorage(), and no unit test ran that sequence in prune mode. I rebased the refactor onto this branch, and the new tests pass on each of its commits.

The first three commits add tests that pass on master. The unit test fixtures apply -prune and -fastprune from extra_args, chainstate_flush_failure_boundary checks that a flush after a failed one writes what was skipped, and prune_flush_failure shows that a failed prune flush still leaves its files on disk after a retry.

The last commit keeps the selected files in BlockManager and deletes them after the next block index write. If that write fails they are dropped with the dirty entries, which means a file is only deleted once it is marked pruned on disk. Pending files don't force a write. The next flush that writes deletes them.

The disk-space failure and automatic pruning take the same path but have no test. Neither does the FindNextBlockPos() line that drops a file from the pending set when a block is written to it, which can happen after an assumeutxo snapshot validates.

If a flush that prunes block files fails before the block index write, the files are already marked pruned in memory, but `setFilesToPrune` is a local in `FlushStateToDisk()`. The next successful flush writes the pruned file info and never deletes the blk/rev files. Only `ScanAndUnlinkAlreadyPrunedFiles()` deletes them, at the next startup. This is number 7 of ryanofsky's [post-merge review of #35714](https://github.com/bitcoin/bitcoin/pull/35714#pullrequestreview-5697890316). The block file flush failure from #35714 and the blocks directory `CheckDiskSpace()` failure both return before that write. I worked on this because it lets me test my kernel pruning refactor (#36738, for #35627 and kernel-node/kernel-node#111) against the issue he describes. The refactor moves the flush, block index write and file deletion that a pruning flush runs from `FlushStateToDisk()` into `WriteBlockStorage()`, and no unit test ran that sequence in prune mode. I rebased the refactor onto this branch, and the new tests pass on each of its commits. The first three commits add tests that pass on master. The unit test fixtures apply `-prune` and `-fastprune` from `extra_args`, `chainstate_flush_failure_boundary` checks that a flush after a failed one writes what was skipped, and `prune_flush_failure` shows that a failed prune flush still leaves its files on disk after a retry. The last commit keeps the selected files in `BlockManager` and deletes them after the next block index write. If that write fails they are dropped with the dirty entries, which means a file is only deleted once it is marked pruned on disk. Pending files don't force a write. The next flush that writes deletes them. The disk-space failure and automatic pruning take the same path but have no test. Neither does the `FindNextBlockPos()` line that drops a file from the pending set when a block is written to it, which can happen after an assumeutxo snapshot validates.
DrahtBot commented 2026-11-02 22:09:51 +00:00 (Migrated from github.com)

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK nervana21

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #37028 (validation: don't report unstored blocks as new by l0rinc)
  • #36854 (kernel: let callers create the block manager by pzafonte)
  • #36739 (refactor: pass BlockManager to ChainstateManager by pzafonte)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36735 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36735.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36735#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [nervana21](https://github.com/bitcoin/bitcoin/pull/36735#issuecomment-7256804421) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#37028](https://github.com/bitcoin/bitcoin/pull/37028) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37028.svg"></sub> (validation: don't report unstored blocks as new by l0rinc) * [#36854](https://github.com/bitcoin/bitcoin/pull/36854) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36854.svg"></sub> (kernel: let callers create the block manager by pzafonte) * [#36739](https://github.com/bitcoin/bitcoin/pull/36739) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36739.svg"></sub> (refactor: pass BlockManager to ChainstateManager by pzafonte) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Pending-unlink cancellation lacks regression coverage

The new retry test never writes into a pending file, so it would not catch removal of the cancellation that protects reused block files.

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

🔵 Suggestion · Pending-unlink failure handling is split across classes

The ordering is correct today, but dropping pending deletions with a failed dirty batch relies on coordinated implementations in Chainstate and BlockManager.

Location: src/validation.cpp:2865 (head).

🔵 Suggestion · The retry test does not cover deferred deletion scheduling

The immediate forced retry leaves untested the rule that pending deletions must not trigger a write on their own.

Location: src/test/chainstate_write_tests.cpp:212 (head).

🔵 Suggestion · The pending-unlink scheduling rationale is undocumented

The exchange comment explains index-write failure handling, but not why pending files intentionally wait for an independently triggered write.

Location: src/node/blockstorage.h:316 (head).

Review coverage was incomplete.


Reviewed: d224fb87210b · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:965af1044d9e41839565702e4d9bd0a751eda81f6e8b6402747fa296ef84ebea:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:ddea9196493d3b8fd841a36a6c53efd85dac7a05 --> <!-- ralph-head:d224fb87210badc347db34a0273100086445bb3c --> <!-- ralph-case:e1d755dc5f09d3f8d1b82707f6fbb43d9f471e65649f1187f91d97140383ccc2 --> **🔵 Suggestion · Pending-unlink cancellation lacks regression coverage** The new retry test never writes into a pending file, so it would not catch removal of the cancellation that protects reused block files. Location: [`src/node/blockstorage.cpp:899`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d224fb87210badc347db34a0273100086445bb3c/src/node/blockstorage.cpp#L899>) (head). **🔵 Suggestion · Pending-unlink failure handling is split across classes** The ordering is correct today, but dropping pending deletions with a failed dirty batch relies on coordinated implementations in Chainstate and BlockManager. Location: [`src/validation.cpp:2865`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d224fb87210badc347db34a0273100086445bb3c/src/validation.cpp#L2865>) (head). **🔵 Suggestion · The retry test does not cover deferred deletion scheduling** The immediate forced retry leaves untested the rule that pending deletions must not trigger a write on their own. Location: [`src/test/chainstate_write_tests.cpp:212`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d224fb87210badc347db34a0273100086445bb3c/src/test/chainstate_write_tests.cpp#L212>) (head). **🔵 Suggestion · The pending-unlink scheduling rationale is undocumented** The exchange comment explains index-write failure handling, but not why pending files intentionally wait for an independently triggered write. Location: [`src/node/blockstorage.h:316`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/d224fb87210badc347db34a0273100086445bb3c/src/node/blockstorage.h#L316>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`d224fb87210b`](<https://git.fish.foo/bitcoin/bitcoin/commit/d224fb87210badc347db34a0273100086445bb3c>) · [Full review report](<https://review.fish.foo/traces/36735-d224fb87210badc347db34a0273100086445bb3c-2020-1.html>) · [Previous reports](<https://review.fish.foo/traces/36735/>) [permalink](<https://review.fish.foo/traces/36735-d224fb87210badc347db34a0273100086445bb3c-2020-1.html>)
Yudis-bit (Migrated from github.com) reviewed 2026-11-03 16:32:27 +00:00
Yudis-bit (Migrated from github.com) commented 2026-11-03 16:32:27 +00:00

m_files_to_unlink.erase(nFile);

Is it actually possible for nFile to be in m_files_to_unlink here? m_last_blockfile contains the tip, and rolling over advances to a new file number.

Also, if an old file were somehow reused here, opening it with rb+ would overwrite the head without truncating the existing block data on disk.

> m_files_to_unlink.erase(nFile); Is it actually possible for `nFile` to be in `m_files_to_unlink` here? `m_last_blockfile` contains the tip, and rolling over advances to a new file number. Also, if an old file were somehow reused here, opening it with `rb+` would overwrite the head without truncating the existing block data on disk.
Yudis-bit (Migrated from github.com) commented 2026-11-03 16:32:27 +00:00

If a flush fails after PruneOneBlockFile() clears nSize, retrying pruneblockchain won't clean up the files. FindFilesToPruneManual() skips files with nSize == 0, so fFlushForPrune stays false. With FlushStateMode::NONE, should_write is false and the function returns early without ever unlinking them.

In chainstate_write_tests.cpp:211, the retry passes only because it calls FORCE_FLUSH instead of FlushStateMode::NONE, prune_height.

If a flush fails after `PruneOneBlockFile()` clears `nSize`, retrying `pruneblockchain` won't clean up the files. `FindFilesToPruneManual()` skips files with `nSize == 0`, so `fFlushForPrune` stays false. With `FlushStateMode::NONE`, `should_write` is false and the function returns early without ever unlinking them. In `chainstate_write_tests.cpp:211`, the retry passes only because it calls `FORCE_FLUSH` instead of `FlushStateMode::NONE, prune_height`.
nervana21 commented 2026-11-17 14:10:15 +00:00 (Migrated from github.com)

Concept ACK

Concept ACK
pzafonte (Migrated from github.com) reviewed 2026-11-19 20:41:37 +00:00
pzafonte (Migrated from github.com) commented 2026-11-19 20:41:37 +00:00

Sorry for the delay in response. You're right that the retry doesn't delete them. File 0 already has nSize == 0, so FindFilesToPruneManual() selects nothing and a NONE flush doesn't write. The files are still in m_files_to_unlink and the next flush that writes deletes them.

That's deliberate on my part and I believe it matches item 6 of ryanofsky's post-merge review where skipped writes are kept in memory until a later flush writes them. A NONE retry doesn't write the dirty block index or coins either. In the node a failed flush requests shutdown, and the shutdown flush calls ForceFlushStateToDisk(), which uses FORCE_FLUSH, which is the retry that actually runs, so it's what the test uses. Item 9 suggests documenting that kernel users should stop the same way.

Edit: item 4 of the same review fits better (arejula27 is working on it in #35731). A false from FlushStateToDisk() only means the disk write failed, and in-memory state is still valid. pruneblockchain already works that way. It logs the failure and returns the prune height from memory. The files are pruned in memory, and the disk catches up on the next write.

Sorry for the delay in response. You're right that the retry doesn't delete them. File 0 already has `nSize == 0`, so `FindFilesToPruneManual()` selects nothing and a `NONE` flush doesn't write. The files are still in `m_files_to_unlink` and the next flush that writes deletes them. That's deliberate on my part and I believe it matches item 6 of ryanofsky's [post-merge review](https://github.com/bitcoin/bitcoin/pull/35714#pullrequestreview-5697890316) where skipped writes are kept in memory until a later flush writes them. A `NONE` retry doesn't write the dirty block index or coins either. In the node a failed flush requests shutdown, and the shutdown flush calls `ForceFlushStateToDisk()`, which uses `FORCE_FLUSH`, which is the retry that actually runs, so it's what the test uses. Item 9 suggests documenting that kernel users should stop the same way. Edit: item 4 of the same review fits better (arejula27 is [working on it](https://github.com/bitcoin/bitcoin/pull/35731#issuecomment-7017968242) in #35731). A `false` from `FlushStateToDisk()` only means the disk write failed, and in-memory state is still valid. `pruneblockchain` already works that way. It logs the failure and returns the prune height from memory. The files are pruned in memory, and the disk catches up on the next write.
pzafonte (Migrated from github.com) reviewed 2026-11-19 20:47:54 +00:00
pzafonte (Migrated from github.com) commented 2026-11-19 20:47:54 +00:00

Yes, after an assumeutxo snapshot validates. There's no single m_last_blockfile anymore. m_blockfile_cursors has a NORMAL and an ASSUMED cursor, and until restart the NORMAL one still points at the last file the background chainstate wrote to. Once the snapshot is validated, GetPruneRange() starts at 0 and that file isn't MaxBlockfileNum() so it can be pruned. A block below the snapshot height (e.g. from getblockfrompeer) then goes into it at offset 0, because PruneOneBlockFile() reset its CBlockFileInfo. If the flush that pruned the file failed, it would still be pending, and without this line the next write would delete it with that block in it. I can put this in the comment above the line.

With regard to rb+ master reuses the file the same way after a failed prune flush, since it never deletes the file. The old data after the new block isn't read, because reads use the stored positions. Overwriting the head is a problem if the node crashes before the next write, because the index on disk still lists the old blocks there. But I think that can be a fix in a separate / followup PR.

Yes, after an assumeutxo snapshot validates. There's no single `m_last_blockfile` anymore. `m_blockfile_cursors` has a `NORMAL` and an `ASSUMED` cursor, and until restart the `NORMAL` one still points at the last file the background chainstate wrote to. Once the snapshot is validated, `GetPruneRange()` starts at 0 and that file isn't `MaxBlockfileNum()` so it can be pruned. A block below the snapshot height (e.g. from `getblockfrompeer`) then goes into it at offset 0, because `PruneOneBlockFile()` reset its `CBlockFileInfo`. If the flush that pruned the file failed, it would still be pending, and without this line the next write would delete it with that block in it. I can put this in the comment above the line. With regard to `rb+` master reuses the file the same way after a failed prune flush, since it never deletes the file. The old data after the new block isn't read, because reads use the stored positions. Overwriting the head is a problem if the node crashes before the next write, because the index on disk still lists the old blocks there. But I think that can be a fix in a separate / followup PR.
nervana21 (Migrated from github.com) reviewed 2026-11-28 14:53:33 +00:00
nervana21 (Migrated from github.com) commented 2026-11-28 14:51:32 +00:00

test: characterize a failed prune flush (108d7ff03c)

We could use the helper function here.

            BOOST_CHECK(blockman.IsBlockPruned(*chainstate.m_chain[height]));
test: characterize a failed prune flush (108d7ff03c6b7cc5b0fa8bddae18e208a9eb2f09) We could use the helper function here. ```suggestion BOOST_CHECK(blockman.IsBlockPruned(*chainstate.m_chain[height])); ```
nervana21 commented 2026-11-29 13:47:34 +00:00 (Migrated from github.com)

tACK d224fb8721

Left a minor, non-blocking suggestion

tACK d224fb87210badc347db34a0273100086445bb3c Left a minor, non-blocking suggestion
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36735
No description provided.