validation: delete pruned files after a failed flush #36735

pull pzafonte wants to merge 4 commits into bitcoin:master from pzafonte:prune-unlink-after-retry changing 5 files +95 −5
  1. pzafonte commented at 10:09 PM on November 2, 2026: contributor

    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](/bitcoin-bitcoin/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.

  2. test: apply BlockManager args in test fixtures
    Without this, -prune and -fastprune in extra_args have no effect.
    24ad36d929
  3. test: check flush retry after failure
    Move the blk file aside instead of deleting it, so it can be restored.
    2334d27a24
  4. test: characterize a failed prune flush
    The pruned blk/rev files are not deleted, even after a later flush
    succeeds.
    108d7ff03c
  5. validation: delete pruned files on a later flush
    Files selected for pruning were kept in a local, so a failed flush left
    them on disk until the next startup.
    
    Keep them in BlockManager and delete them after the next block index
    write. Take them with the dirty entries, which WriteBlockIndexDB() drops
    even if the write fails. Drop a file from the set once a block is
    written to it.
    
    Pending files wait for the next periodic, forced or shutdown flush
    rather than forcing one, which would retry a failing write on every
    block.
    d224fb8721
  6. DrahtBot added the label Validation on Nov 2, 2026
  7. DrahtBot commented at 10:09 PM on November 2, 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.

    Type Reviewers
    ACK nervana21

    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:

    • #36854 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36854.svg"></sub> (kernel: let callers create the block manager by pzafonte)
    • #36739 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36739.svg"></sub> (refactor: pass BlockManager to ChainstateManager by pzafonte)
    • #36598 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36598.svg"></sub> ([Demo] validation: remove AssumeUTXO by l0rinc)

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

  8. in src/validation.cpp:2865 in d224fb8721
    2859 | @@ -2859,17 +2860,20 @@ bool Chainstate::FlushStateToDisk(
    2860 |                  }
    2861 |              }
    2862 |  
    2863 | +            // Take the pruned files with the dirty entries, which WriteBlockIndexDB() drops
    2864 | +            // even if it fails, so files are only deleted after the write that marks them pruned.
    2865 | +            const auto files_to_unlink{std::exchange(m_blockman.m_files_to_unlink, {})};
    


    Yudis-bit commented at 4:32 PM on November 3, 2026:

    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.


    pzafonte commented at 8:41 PM on November 19, 2026:

    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.

  9. in src/node/blockstorage.cpp:899 in d224fb8721
     894 | @@ -895,6 +895,8 @@ FlatFilePos BlockManager::FindNextBlockPos(unsigned int nAddSize, unsigned int n
     895 |              m_blockfile_info.resize(nFile + 1);
     896 |          }
     897 |      }
     898 | +    // Don't delete a pruned file that is about to hold a new block.
     899 | +    m_files_to_unlink.erase(nFile);
    


    Yudis-bit commented at 4:32 PM on November 3, 2026:

    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.


    pzafonte commented at 8:47 PM on November 19, 2026:

    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.

  10. pzafonte marked this as ready for review on Nov 13, 2026
  11. nervana21 commented at 2:10 PM on November 17, 2026: contributor

    Concept ACK

  12. in src/test/chainstate_write_tests.cpp:197 in d224fb8721
     192 | +    {
     193 | +        LOCK(::cs_main);
     194 | +        BOOST_CHECK_EQUAL(blockman.GetBlockFileInfo(0)->nSize, 0U);
     195 | +        BOOST_CHECK_EQUAL(blockman.GetBlockFileInfo(0)->nUndoSize, 0U);
     196 | +        for (int height{0}; height <= prune_height; ++height) {
     197 | +            BOOST_CHECK(!(chainstate.m_chain[height]->nStatus & BLOCK_HAVE_DATA));
    


    nervana21 commented at 2:51 PM on November 28, 2026:

    test: characterize a failed prune flush (108d7ff03c6b7cc5b0fa8bddae18e208a9eb2f09)

    We could use the helper function here.

                BOOST_CHECK(blockman.IsBlockPruned(*chainstate.m_chain[height]));
    
  13. nervana21 commented at 1:47 PM on November 29, 2026: contributor

    tACK d224fb87210badc347db34a0273100086445bb3c

    Left a minor, non-blocking suggestion


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-30 03:51 UTC

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