Indexes: Harden the flush-error notification invariant #35731

Open
arejula27 wants to merge 2 commits from flush-notify-invariant into master AGit
arejula27 commented 2026-07-15 17:35:12 +00:00 (Migrated from github.com)

FlushStateToDisk() returns state.Error(...) when FlushChainstateBlockFile() fails, relying on the invariant that the flush-error notification has already been emitted (so the node is aborted). Today that invariant holds only because every success = false branch in FlushBlockFile/FlushUndoFile happens to call flushError() itself. A future flush source, or a new error branch, that fails without notifying would make FlushStateToDisk() report an error without AbortNode() being triggered.

This PR makes that invariant hold by construction: whenever a flush reports failure, the flush-error notification has been emitted.

  • test: cover FlushBlockFile flush-error notifications: BlockManager unit tests that inject a failure on the block file and on the undo file separately, and assert that each failure emits flushError.
  • blockstorage: encapsulate flush+notify in FlushFile: a single FlushFile() helper performs the flush and emits the notification on failure; FlushBlockFile and FlushUndoFile delegate to it. No behaviour change.

Follow-up to #35714. Possible further work on flush error handling is listed in https://github.com/bitcoin/bitcoin/pull/35714#pullrequestreview-5697890316 (pruned-file deletion after a failed flush is handled in #36735).

`FlushStateToDisk()` returns `state.Error(...)` when `FlushChainstateBlockFile()` fails, relying on the invariant that the flush-error notification has already been emitted (so the node is aborted). Today that invariant holds only because every `success = false` branch in `FlushBlockFile`/`FlushUndoFile` happens to call `flushError()` itself. A future flush source, or a new error branch, that fails without notifying would make `FlushStateToDisk()` report an error without `AbortNode()` being triggered. This PR makes that invariant hold by construction: whenever a flush reports failure, the flush-error notification has been emitted. - **test: cover FlushBlockFile flush-error notifications**: `BlockManager` unit tests that inject a failure on the block file and on the undo file separately, and assert that each failure emits `flushError`. - **blockstorage: encapsulate flush+notify in FlushFile**: a single `FlushFile()` helper performs the flush and emits the notification on failure; `FlushBlockFile` and `FlushUndoFile` delegate to it. No behaviour change. Follow-up to #35714. Possible further work on flush error handling is listed in https://github.com/bitcoin/bitcoin/pull/35714#pullrequestreview-5697890316 (pruned-file deletion after a failed flush is handled in #36735).
DrahtBot commented 2026-07-15 17:35:20 +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
Concept ACK ryanofsky

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:

  • #36971 (blockstorage: fix partial-mask assertions and genesis availability checks by l0rinc)
  • #36946 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
  • #36858 (refactor: break the blockstorage/validation circular dependency by pzafonte)
  • #36548 (net: serve MSG_BLOCK by stripping witnesses in place by l0rinc)
  • #36326 (kernel: Use typed errors for fatal and flush error notifications by arejula27)
  • #36000 (validation: prefetch blocks while connecting by l0rinc)
  • #35003 (validation: improve block data I/O error handling in P2P paths by furszy)

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/35731 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35731.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35731#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 | | ---- | --------- | | Concept ACK | [ryanofsky](https://github.com/bitcoin/bitcoin/pull/35731#pullrequestreview-5757964652) | 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: * [#36971](https://github.com/bitcoin/bitcoin/pull/36971) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36971.svg"></sub> (blockstorage: fix partial-mask assertions and genesis availability checks by l0rinc) * [#36946](https://github.com/bitcoin/bitcoin/pull/36946) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36946.svg"></sub> (scripted-diff: [test] Add util/check.h includes for assertions by maflcko) * [#36858](https://github.com/bitcoin/bitcoin/pull/36858) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36858.svg"></sub> (refactor: break the blockstorage/validation circular dependency by pzafonte) * [#36548](https://github.com/bitcoin/bitcoin/pull/36548) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36548.svg"></sub> (net: serve `MSG_BLOCK` by stripping witnesses in place by l0rinc) * [#36326](https://github.com/bitcoin/bitcoin/pull/36326) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36326.svg"></sub> (kernel: Use typed errors for fatal and flush error notifications by arejula27) * [#36000](https://github.com/bitcoin/bitcoin/pull/36000) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36000.svg"></sub> (validation: prefetch blocks while connecting by l0rinc) * [#35003](https://github.com/bitcoin/bitcoin/pull/35003) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35003.svg"></sub> (validation: improve block data I/O error handling in P2P paths by furszy) 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-->
DrahtBot commented 2026-07-20 11:17:59 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/29437114552/job/88325958979
LLM reason (✨ experimental): CI failed because IWYU reported required header/include fixes (it modified src/node/blockstorage.h and exited non-zero).

Hints

Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:

  • Possibly due to a silent merge conflict (the changes in this pull request being
    incompatible with the current code in the target branch). If so, make sure to rebase on the latest
    commit of the target branch.

  • A sanitizer issue, which can only be found by compiling with the sanitizer and running the
    affected test.

  • An intermittent issue.

Leave a comment here, if you need help tracking down a confusing failure.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/29437114552/job/88325958979</sub> <sub>LLM reason (✨ experimental): CI failed because IWYU reported required header/include fixes (it modified `src/node/blockstorage.h` and exited non-zero).</sub> <details><summary>Hints</summary> Try to run the tests locally, according to the documentation. However, a CI failure may still happen due to a number of reasons, for example: * Possibly due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch. * A sanitizer issue, which can only be found by compiling with the sanitizer and running the affected test. * An intermittent issue. Leave a comment here, if you need help tracking down a confusing failure. </details>
First-time contributor

Earlier concerns remain unresolved; see the full report for their current assessments.

🔴 Major · Keep chain and coin state coherent after a failed flush

validation.cpp:3134-3159: ConnectTip (and likewise DisconnectTip) still flushes coin changes into CoinsTip, then returns on a failed IF_NEEDED flush before SetTip, so the coins tip can end up ahead of m_chain. After the rebase this is base code from merged #35714, and this PR does not touch validation.cpp. Impact on kernel applications is still unconfirmed.

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

🔵 Suggestion · Flush notification tests discard the error message

A block/undo message mix-up in the refactored helper calls would still pass both new unit tests.

Location: src/test/blockmanager_tests.cpp:47 (head).

🔵 Suggestion · FlushFile constructs error messages on successful flushes

Both callers now construct bilingual error strings and invoke any configured translator before flushing, even when the flush succeeds.

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

🔵 Suggestion · Undo failure test rebuilds the undo path through std::string

The undo test bypasses fs::path's string-conversion guards and duplicates the block/undo naming convention, although its existing friend accessor can obtain the actual undo path.

Location: src/test/blockmanager_tests.cpp:421 (head).

Review coverage was incomplete.


Reviewed: 2d7830b0928c · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:965af1044d9e41839565702e4d9bd0a751eda81f6e8b6402747fa296ef84ebea:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:ddea9196493d3b8fd841a36a6c53efd85dac7a05 --> <!-- ralph-head:2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd --> <!-- ralph-case:265608b0c22acdf747a08470725a5e43a788ab0a05c7ab822eb8a38185f40b3b --> Earlier concerns remain unresolved; see the full report for their current assessments. **🔴 Major · Keep chain and coin state coherent after a failed flush** validation.cpp:3134-3159: ConnectTip (and likewise DisconnectTip) still flushes coin changes into CoinsTip, then returns on a failed IF_NEEDED flush before SetTip, so the coins tip can end up ahead of m_chain. After the rebase this is base code from merged #35714, and this PR does not touch validation.cpp. Impact on kernel applications is still unconfirmed. Location: [`src/validation.cpp:3134`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd/src/validation.cpp#L3134>) (head). **🔵 Suggestion · Flush notification tests discard the error message** A block/undo message mix-up in the refactored helper calls would still pass both new unit tests. Location: [`src/test/blockmanager_tests.cpp:47`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd/src/test/blockmanager_tests.cpp#L47>) (head). **🔵 Suggestion · FlushFile constructs error messages on successful flushes** Both callers now construct bilingual error strings and invoke any configured translator before flushing, even when the flush succeeds. Location: [`src/node/blockstorage.cpp:750`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd/src/node/blockstorage.cpp#L750>) (head). **🔵 Suggestion · Undo failure test rebuilds the undo path through std::string** The undo test bypasses fs::path's string-conversion guards and duplicates the block/undo naming convention, although its existing friend accessor can obtain the actual undo path. Location: [`src/test/blockmanager_tests.cpp:421`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd/src/test/blockmanager_tests.cpp#L421>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`2d7830b0928c`](<https://git.fish.foo/bitcoin/bitcoin/commit/2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd>) · [Full review report](<https://review.fish.foo/traces/35731-2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd-1996-4.html>) · [Previous reports](<https://review.fish.foo/traces/35731/>) [permalink](<https://review.fish.foo/traces/35731-2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd-1996-4.html>)
DrahtBot commented 2026-10-28 19:02:18 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
arejula27 commented 2026-11-02 16:54:38 +00:00 (Migrated from github.com)

Rebased onto current master and converting from draft to open.
@ryanofsky's follow-up list on #35714 also flagged more to do here, I'll do that in the next few days.

Rebased onto current master and converting from draft to open. @ryanofsky's follow-up list on #35714 also flagged more to do here, I'll do that in the next few days.
ryanofsky (Migrated from github.com) approved these changes 2026-11-03 19:05:03 +00:00
ryanofsky (Migrated from github.com) left a comment

Concept ACK 2d7830b092 on new test coverage and bundling Flush/flushError together.

For the follow-ups in https://github.com/bitcoin/bitcoin/pull/35714#pullrequestreview-5697890316, note that #36735 is taking item 7 (deleting pruned files after a failed flush), so it might make sense to coordinate there. For items changing how errors are reported (like turning warnings into failures), I could be best to start with item 4, documenting how callers should use the FlushStateToDisk() return value, so later changes can follow a consistent rule instead of being made one at a time.

Would also be good to update description now that PR https://github.com/bitcoin/bitcoin/pull/35714 is merged, and maybe simplify a little so this can be understood without needing to understand the #35714 history

Concept ACK 2d7830b0928cf7e57c8e795f03b0b7d33e7cecfd on new test coverage and bundling Flush/flushError together. For the follow-ups in https://github.com/bitcoin/bitcoin/pull/35714#pullrequestreview-5697890316, note that #36735 is taking item 7 (deleting pruned files after a failed flush), so it might make sense to coordinate there. For items changing how errors are reported (like turning warnings into failures), I could be best to start with item 4, documenting how callers should use the `FlushStateToDisk()` return value, so later changes can follow a consistent rule instead of being made one at a time. Would also be good to update description now that PR https://github.com/bitcoin/bitcoin/pull/35714 is merged, and maybe simplify a little so this can be understood without needing to understand the #35714 history
arejula27 commented 2026-11-19 18:18:23 +00:00 (Migrated from github.com)

Thanks for the review @ryanofsky . I've updated the PR description so it no longer depends on #35714's history. I'll start with documenting how callers should use the FlushStateToDisk() return value (item 4) and leave item 7 to #36735.

Thanks for the review @ryanofsky . I've updated the PR description so it no longer depends on #35714's history. I'll start with documenting how callers should use the `FlushStateToDisk()` return value (item 4) and leave item 7 to #36735.
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!35731
No description provided.