There are a few cases where we can mark a block and undo file as pruned in our block index, but not actually remove the files from disk.
- If we call
FindFilesToPrune
orFindFilesToPruneManual
and crash beforeUnlinkPrunedFiles
. - If on Windows there is an open file handle to the file somewhere else when calling
fs::remove
inUnlinkPrunedFiles
(https://en.cppreference.com/w/cpp/filesystem/remove, https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-deletefilew#remarks). This could be from another process, or if we are callingReadBlockFromDisk
/ReadRawBlockFromDisk
without having a lock oncs_main
(which has been allowed since https://github.com/bitcoin/bitcoin/commit/ccd8ef65f93ed82a87cee634660bed3ac17d9eb5).
This PR mitigates this by scanning all pruned block files on startup after LoadBlockIndexDB
and unlinking them again.