Skip to content

Commit d8041d4

Browse files
committed
blockstorage: Return on fatal undo file flush error
By returning an error code if either `FlushUndoFile` or `FlushBlockFile` fail, the caller now has to explicitly handle block undo file flushing errors. Before this change such errors were non-explicitly ignored without a clear rationale. Besides the call to `FlushUndoFile` in `FlushBlockFile`, ignore its return code at its call site in `WriteUndoDataForBlock`. There, a failed flush of the undo data should not be indicative of a failed write. Add [[nodiscard]] annotations to `FlushUndoFile` such that its return value is not just ignored in the future.
1 parent f0207e0 commit d8041d4

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/node/blockstorage.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -538,12 +538,14 @@ bool BlockManager::UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex& in
538538
return true;
539539
}
540540

541-
void BlockManager::FlushUndoFile(int block_file, bool finalize)
541+
bool BlockManager::FlushUndoFile(int block_file, bool finalize)
542542
{
543543
FlatFilePos undo_pos_old(block_file, m_blockfile_info[block_file].nUndoSize);
544544
if (!UndoFileSeq().Flush(undo_pos_old, finalize)) {
545545
m_opts.notifications.flushError("Flushing undo file to disk failed. This is likely the result of an I/O error.");
546+
return false;
546547
}
548+
return true;
547549
}
548550

549551
bool BlockManager::FlushBlockFile(bool fFinalize, bool finalize_undo)
@@ -567,7 +569,11 @@ bool BlockManager::FlushBlockFile(bool fFinalize, bool finalize_undo)
567569
}
568570
// we do not always flush the undo file, as the chain tip may be lagging behind the incoming blocks,
569571
// e.g. during IBD or a sync after a node going offline
570-
if (!fFinalize || finalize_undo) FlushUndoFile(m_last_blockfile, finalize_undo);
572+
if (!fFinalize || finalize_undo) {
573+
if (!FlushUndoFile(m_last_blockfile, finalize_undo)) {
574+
success = false;
575+
}
576+
}
571577
return success;
572578
}
573579

@@ -764,7 +770,14 @@ bool BlockManager::WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValid
764770
// with the block writes (usually when a synced up node is getting newly mined blocks) -- this case is caught in
765771
// the FindBlockPos function
766772
if (_pos.nFile < m_last_blockfile && static_cast<uint32_t>(block.nHeight) == m_blockfile_info[_pos.nFile].nHeightLast) {
767-
FlushUndoFile(_pos.nFile, true);
773+
// Do not propagate the return code, a failed flush here should not
774+
// be an indication for a failed write. If it were propagated here,
775+
// the caller would assume the undo data not to be written, when in
776+
// fact it is. Note though, that a failed flush might leave the data
777+
// file untrimmed.
778+
if (!FlushUndoFile(_pos.nFile, true)) {
779+
LogPrintLevel(BCLog::BLOCKSTORAGE, BCLog::Level::Warning, "Failed to flush undo file %05i\n", _pos.nFile);
780+
}
768781
} else if (_pos.nFile == m_last_blockfile && static_cast<uint32_t>(block.nHeight) > m_undo_height_in_last_blockfile) {
769782
m_undo_height_in_last_blockfile = block.nHeight;
770783
}

src/node/blockstorage.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ class BlockManager
9292
bool LoadBlockIndex()
9393
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
9494

95-
/** Return false if block file flushing fails. */
95+
/** Return false if block file or undo file flushing fails. */
9696
[[nodiscard]] bool FlushBlockFile(bool fFinalize = false, bool finalize_undo = false);
9797

98-
void FlushUndoFile(int block_file, bool finalize = false);
98+
/** Return false if undo file flushing fails. */
99+
[[nodiscard]] bool FlushUndoFile(int block_file, bool finalize = false);
100+
99101
[[nodiscard]] bool FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown);
100102
bool FindUndoPos(BlockValidationState& state, int nFile, FlatFilePos& pos, unsigned int nAddSize);
101103

0 commit comments

Comments
 (0)