streams: handle buffered write failures outside the destructor #36049

pull l0rinc wants to merge 5 commits into bitcoin:master from l0rinc:l0rinc/streams-buffered-write-failure changing 3 files +55 −15
  1. l0rinc commented at 10:06 PM on August 20, 2026: contributor

    Problem: Block and undo writers buffer serialized data, then rely on the BufferedWriter destructor to write the remaining bytes. fwrite() can report fewer bytes written than requested after a local storage error, e.g., if the filesystem fills up while a block is being written (my 1 TB benchmarking servers have recently been filling up, so I've been seeing these failures more often). AutoFile::write_buffer() converts that result into an exception, and because the destructor is implicitly non-throwing, the exception invokes std::terminate. Calling flush() before destruction is insufficient on its own because a failed flush leaves the same bytes pending, so the destructor retries the write while the original exception unwinds.

    I introduced this bug in #31551 when adding BufferedWriter.

    I’d update the Fix section to include discard() and describe exactly what the destructor checks. The problem statement and reproducer can stay as written.

    Fix: Block and undo writers now flush explicitly before destruction. If serialization or writing throws std::ios_base::failure, they discard any pending bytes, log the error, close the file, and request shutdown through the existing fatal-error path. BufferedWriter clears the pending byte count before writing because the stream may obfuscate the bytes in place and a short write may already have put some of them on disk, making a retry unsafe. The destructor uses Assume to check that no bytes are pending before attempting a fallback flush. flush() continues to propagate exceptions, and AutoFile remains unchanged.

    <details> <summary>Manual write-error reproducer</summary>

    Force the error path in AutoFile::write_buffer():

    sed -i '' '/^.*AutoFile::write_buffer(/,/^}/s/!= src.size()/!= 0/' src/streams.cpp
    cmake -B build && cmake --build build -j5 && build/bin/bitcoind -regtest -datadir="$(mktemp -d)"; echo "exit=$?"
    

    Before this change, implicit flushing invokes std::terminate:

    libc++abi: terminating due to uncaught exception of type std::__1::ios_base::failure: AutoFile::write_buffer: write failed: unspecified iostream_category error zsh: abort build/bin/bitcoind -regtest -datadir="$(mktemp -d)" exit=134

    With this change, bitcoind logs the write failure, then completes normal shutdown and exits with status 1:

    Checking all blk files are present... [error] Failed to write block file FlatFilePos(nFile=0, nPos=8): AutoFile::write_buffer: write failed: unspecified iostream_category error ... Error: A fatal internal error occurred, see debug.log for details: Failed to write block. [error] Writing genesis block to disk failed [error] Error initializing block database ... Shutdown done exit=1

    </details>

  2. DrahtBot commented at 10:06 PM on August 20, 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
    Concept ACK ryanofsky
    Stale ACK sedited, w0xlt, maflcko

    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

    No conflicts as of last run.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  3. andrewtoth commented at 10:21 PM on August 20, 2026: contributor

    the exception invokes std::terminate instead of reaching normal storage-error handling.

    Can you expand on why this is a problem? If there is a storage error, don't we have to crash anyways?

  4. l0rinc commented at 10:35 PM on August 20, 2026: contributor

    If there is a storage error, don't we have to crash anyways?

    Yes, the node still has to stop, but std::terminate bypasses the existing error handling.

    Can you expand on why this is a problem?

    Handling the exception lets the node report the storage error and run its normal shutdown cleanup.

  5. maflcko commented at 11:56 AM on August 21, 2026: member

    This reminds me of the other "exceptions can terminate" topics.

    It would be good to find a code pattern that avoids this class of problem wholesale. It doesn't seem a great use of human (or LLM) review time to spend on such issues one-by-one. If we care about those issues, it would be better if the compiler or a clang-tidy analysis could find and prevent all of them.

  6. w0xlt commented at 8:25 PM on September 5, 2026: contributor

    Concept ACK

    I ran into the same bug while working in this area and implemented an alternative fix before finding this PR.

    My version removes writes from the BufferedWriter destructor and requires explicit final flushing. It also adjusts AutoFile cleanup during exception unwinding and adds an exception handler for undo-write failures.

    https://github.com/w0xlt/bitcoin/tree/streams/buffered-write-errors

    Feel free to reuse anything you find useful.

  7. l0rinc force-pushed on Sep 23, 2026
  8. l0rinc commented at 10:31 PM on September 23, 2026: contributor

    Thanks @w0xlt, I used your explicit-flush and undo-write error-handling ideas and credited you as a co-author.

    But I think forgetting to call flush() is a bigger practical risk than the rare exception from a destructor write, so I kept destructor flushing as a fallback. Block and undo writes now flush explicitly and handle failures, and a failed flush is no longer retried during exception unwinding.

  9. w0xlt commented at 5:22 AM on September 24, 2026: contributor

    ACK cac9501d355d9853c17dcfa874d85c8e952cece0

  10. in src/node/blockstorage.cpp:1016 in cac9501d35
    1011 | @@ -1012,7 +1012,11 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
    1012 |                  // Write undo data & checksum
    1013 |                  fileout << blockundo << hasher.GetHash();
    1014 |              }
    1015 | -            // BufferedWriter will flush pending data to file when fileout goes out of scope.
    1016 | +            fileout.flush();
    1017 | +        } catch (const std::exception& e) {
    


    sedited commented at 8:27 PM on November 5, 2026:

    I would prefer if we'd catch on std::runtime_error. For other exceptions, e.g. allocation failures, it might genuinely be better to immediately terminate. It would also match the exception behaviour in most of the code.


    l0rinc commented at 9:10 AM on November 6, 2026:

    Changed both handlers to catch std::runtime_error. If you meant explicitly calling std::abort() here as well (maybe in a separate catch), please explain that in more detail, unless you just meant that other exceptions should fall through.

  11. sedited commented at 9:11 PM on November 5, 2026: contributor

    LGTM

  12. l0rinc force-pushed on Nov 6, 2026
  13. sedited approved
  14. sedited commented at 10:18 AM on November 6, 2026: contributor

    ACK 6e17d090d18dbf1686b713bc285761a3aa042663

  15. DrahtBot requested review from w0xlt on Nov 6, 2026
  16. maflcko commented at 2:50 PM on November 6, 2026: member

    How is this different from AutoFile? Shouldn't this use the same bool m_was_written{false}; approach with flush+Assume+LogErr? Otherwise, we are just spending manual review on this and this will silently break again in the future?

  17. l0rinc commented at 8:43 PM on November 6, 2026: contributor

    How is this different from AutoFile?

    BufferedWriter flushes through write_buffer() (which can throw in the destructor), while AutoFile::fclose() returns an error code.

    Would you prefer keeping the explicit flushes, adding Assume(m_buf_pos == 0), and catching and logging fallback failures in the destructor, like this:

        ~BufferedWriter()
        {
            Assume(m_buf_pos == 0);
            try {
                flush();
            } catch (const std::runtime_error& e) {
                LogError("Failed to flush buffered writer: %s", e.what());
            }
        }
    
  18. maflcko commented at 10:49 AM on November 7, 2026: member

    Ok, I see. So the difference is whether the dtor can throw. That is probably an internal implementation detail.

    Though, the underlying issue seems to be that we want to notify the caller about a flush failure and that doesn't really work well via a dtor. So the fix for that is an explicit flush. However, if we want those explicit flushes, it should be enforced in the same way as it is done in AutoFile.

    I think your suggested diff achieves that, so my preference would be to focus this pull on that. In theory, one could even drop the exception fixup from this pull afterward, and instead make flush non-throwing, so that m_buf_pos=0 can be forced in the dtor, so that a failed flush isn't immediately tried again. (If it failed to flush, re-trying it a single CPU cycle later isn't going to fix it anyway). Up to you, if you want to make flush non-throwing and instead use a nodiscard-result, but I think your suggested diff with the Assume would be good regardless.

  19. l0rinc renamed this:
    streams: avoid termination on buffered write failure
    streams: log and abort on buffered write failures
    on Nov 9, 2026
  20. l0rinc force-pushed on Nov 9, 2026
  21. l0rinc commented at 11:03 AM on November 9, 2026: contributor

    Thanks for the hints @maflcko, played with it a bit, now write_buffer is noexcept with a bool return value - and buffered flush failures log their cause and abort. Explicit final flushing is enforced with Assume, while destructor flushing remains as a prod fallback. This seems like a more holistic fix than before, let me know what you think.

  22. maflcko commented at 11:25 AM on November 9, 2026: member

    Ok, I see you now changed write_buffer to be nodiscard and flush to abort on error. This seems fine, but is now basically the behavior that existed before this pull (it also crashes).

    My preference would be to make flush nodiscard (and possibly not even touch write_buffer/AutoFile here). This would be more consistent with the existing AutoFile fclose "flush" method.

  23. l0rinc force-pushed on Nov 9, 2026
  24. l0rinc commented at 12:29 PM on November 9, 2026: contributor

    basically the behavior that existed before this pull (it also crashes)

    We're always flushing explicitly outside the destructor now. It's a small difference, I know, mostly consistency and belts-and-suspenders hardening. These unlikely write/flush/read failure cases are still all over the place... Reverted the AutoFile changes and updated flush() to return a checked result.

  25. in src/test/streams_tests.cpp:763 in e296717f30
     758 | +        struct FailingStream {
     759 | +            int write_attempts{0};
     760 | +            void write_buffer(std::span<std::byte>) { if (++write_attempts == 1) throw std::ios_base::failure{"write failed"}; }
     761 | +        } stream;
     762 | +        BOOST_CHECK_EXCEPTION([&] {
     763 | +            BufferedWriter writer(stream, buffer_size);
    


    maflcko commented at 1:28 PM on November 9, 2026:

    nit in the first commit, could use C++11 {} init?


    l0rinc commented at 5:00 PM on November 9, 2026:

    Inside BOOST_CHECK_* I usually avoid brace init - I could use parenthesis around if you insist.


    maflcko commented at 7:27 AM on November 10, 2026:

    Ok, I see. Maybe assign the lambda to a var write_and_flush, which should also make the test code shroter?


    l0rinc commented at 6:26 AM on November 11, 2026:

    Sure, done

  26. in src/streams.h:713 in 30ac211a91
     712 | -        m_buf_pos = 0;
     713 | +        if (auto buf_pos{std::exchange(m_buf_pos, 0)}) {
     714 | +            try {
     715 | +                m_dst.write_buffer(std::span{m_buf}.first(buf_pos));
     716 | +            } catch (const std::exception& e) {
     717 | +                LogError("BufferedWriter::flush: %s", e.what());
    


    maflcko commented at 1:44 PM on November 9, 2026:

    nit in 30ac211a91603671fcdc8de757081a39224957c3: I feel like logging should stay out of this module and the caller can log, if it wants to.


    l0rinc commented at 5:01 PM on November 9, 2026:

    Moved

  27. in src/node/blockstorage.cpp:1171 in 74c1322aee
    1167 | @@ -1168,6 +1168,7 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
    1168 |          pos.nPos += STORAGE_HEADER_BYTES;
    1169 |          // Write block
    1170 |          fileout << TX_WITH_WITNESS(block);
    1171 | +        Assert(fileout.flush());
    


    maflcko commented at 1:46 PM on November 9, 2026:

    nit in the third commit: This still crashes, like before this pull.

    Also, the line immediately below will not crash. I think the code should be consistent and both places should do the same thing. Otherwise, it will be impossible to understand the goal of the code.


    maflcko commented at 1:51 PM on November 9, 2026:

    basically the behavior that existed before this pull (it also crashes)

    These unlikely write/flush/read failure cases are still all over the place...

    Right, but I think if the code is touched for style reasons, it would be good to make the style consistent in at least two immediately following lines.


    maflcko commented at 2:22 PM on November 9, 2026:

    I guess it may be easiest to restore the try-catch commit id from #36049#pullrequestreview-5798743901 , with the Assume(m_buf_pos == 0); nit addressed in the dtor


    l0rinc commented at 5:01 PM on November 9, 2026:

    Done, thanks


    maflcko commented at 7:28 AM on November 10, 2026:

    Are you sure you restored the old commit? I can't diff it and this line is still present in this pull:

    $ grd 6e17d090d18dbf1686b713bc285761a3aa042663 74c1322aee3844e043bce4474e956631a20df7ff | tail -4
    2:  6b2b0a2ebc < -:  ---------- streams: do not retry failed flushes
    3:  6e17d090d1 < -:  ---------- blockstorage: handle buffered write failures
    -:  ---------- > 2:  30ac211a91 streams: return checked buffered flush results
    -:  ---------- > 3:  74c1322aee streams: require explicit final flushing
    

    l0rinc commented at 6:26 AM on November 11, 2026:

    Indeed, seem the previous push failed.

  28. in src/streams.h:709 in 74c1322aee
     702 | @@ -702,7 +703,11 @@ class BufferedWriter
     703 |  public:
     704 |      explicit BufferedWriter(S& stream LIFETIMEBOUND, size_t size = 1 << 16) : m_dst{stream}, m_buf(size) {}
     705 |  
     706 | -    ~BufferedWriter() { Assert(flush()); }
     707 | +    ~BufferedWriter()
     708 | +    {
     709 | +        Assume(m_buf_pos == 0);
     710 | +        Assert(flush());
    


    maflcko commented at 1:49 PM on November 9, 2026:

    nit in the third commit: 74c1322aee3844e043bce4474e956631a20df7ff:

            Assume(m_buf_pos == 0); // Use explicit flush()
            Assert(flush());  // no-op, because flush was already called explicitly
    

    Also, could remove the "destructor flushes" from the doxygen comment, because there is no need to document dead no-op code.


    l0rinc commented at 5:02 PM on November 9, 2026:

    Did something similar

  29. in src/streams.h:712 in 74c1322aee
     709 | +        Assume(m_buf_pos == 0);
     710 | +        Assert(flush());
     711 | +    }
     712 |  
     713 | -    void flush()
     714 | +    [[nodiscard]] bool flush() noexcept
    


    maflcko commented at 2:10 PM on November 9, 2026:

    nit: Should document that a failed flush (returning false) will discard the buffer even though it wasn't written fully.


    l0rinc commented at 5:03 PM on November 9, 2026:

    Done

  30. l0rinc commented at 8:01 PM on November 9, 2026: contributor

    Restored the try/catch approach from the ACKed 6e17d09, following @maflcko's latest suggestion, and added the destructor’s Assume(m_buf_pos == 0) check. This means flush() isn't returning anymore (still throwing), with block and undo writers flushing explicitly and handling failures through their normal storage-error paths. It's not perfect, but it's better than before. (edit: previous push didn't succeed for some reason)

  31. l0rinc renamed this:
    streams: log and abort on buffered write failures
    streams: handle buffered write failures outside the destructor
    on Nov 9, 2026
  32. w0xlt commented at 7:20 PM on November 10, 2026: contributor

    reACK 74c1322aee3844e043bce4474e956631a20df7ff

  33. DrahtBot requested review from sedited on Nov 10, 2026
  34. maflcko commented at 7:41 PM on November 10, 2026: member

    Just for reference, there is outstanding/unaddressed review, as far as I can tell: #36049 (review) (maybe forgot to push?)

  35. test: characterize buffered write failures
    Record write errors and the destructor retry after a failed buffered flush before changing error reporting.
    Prepare the existing buffered stream tests for explicit flushing.
    a27c5f6c83
  36. l0rinc force-pushed on Nov 11, 2026
  37. in src/streams.h:705 in d74ff720f9
     701 | @@ -701,10 +702,12 @@ class BufferedWriter
     702 |  
     703 |      ~BufferedWriter() { flush(); }
     704 |  
     705 | +    /** Discard the buffered bytes even if writing them throws. */
    


    maflcko commented at 8:36 AM on November 11, 2026:

    style nit: I don't like the /* comments for new code, because they are so inconsistently formatted. Could use the //! or /// for new code (and also consistent with all comments on BufferedFile methods?


    l0rinc commented at 2:57 PM on November 11, 2026:

    Here I used /** to be consistent with BufferedWriter - adjusted both.

    I agree with the formatting objection in general (I assume you mean the alignment of the middle * characters and the closing */), but mostly because multiline block comments usually take two extra lines :p. I dislike most code comments in general, but when I do have to add one, I usually prefer a same-line ones so it's easy to ignore. But given that others do like comments, maybe we could update the docs:

    diff --git a/doc/developer-notes.md b/doc/developer-notes.md
    index 7e1e61652e..139cfd4d90 100644
    --- a/doc/developer-notes.md
    +++ b/doc/developer-notes.md
    @@ -203,40 +203,43 @@ git diff | ( cd ./src/ && clang-tidy-diff -p2 -path ../build -j $(nproc) )
     
     Bitcoin Core uses [Doxygen](https://www.doxygen.nl/) to generate its official documentation.
     
    -Use Doxygen-compatible comment blocks for functions, methods, and fields.
    +Use Doxygen-compatible comments for functions, methods, and fields.
    +For new documentation comments, prefer `//!` or `///` over `/** ... */`.
    +
    +  - *Rationale*: Line comments avoid aligning leading `*` characters and closing delimiters.
    +
    +Prefer `///` for multiline comments, as the current Doxygen configuration automatically uses their first sentence as the brief description.
     
     For example, to describe a function use:
     
     ```c++
    -/**
    - * ... Description ...
    - *
    - * [@param](/bitcoin-bitcoin/contributor/param/)[in]  arg1 input description...
    - * [@param](/bitcoin-bitcoin/contributor/param/)[in]  arg2 input description...
    - * [@param](/bitcoin-bitcoin/contributor/param/)[out] arg3 output description...
    - * [@return](/bitcoin-bitcoin/contributor/return/) Return cases...
    - * [@throws](/bitcoin-bitcoin/contributor/throws/) Error type and cases...
    - * [@pre](/bitcoin-bitcoin/contributor/pre/)  Pre-condition for function...
    - * [@post](/bitcoin-bitcoin/contributor/post/) Post-condition for function...
    - */
    +/// ... Description ...
    +///
    +/// [@param](/bitcoin-bitcoin/contributor/param/)[in]  arg1 input description...
    +/// [@param](/bitcoin-bitcoin/contributor/param/)[in]  arg2 input description...
    +/// [@param](/bitcoin-bitcoin/contributor/param/)[out] arg3 output description...
    +/// [@return](/bitcoin-bitcoin/contributor/return/) Return cases...
    +/// [@throws](/bitcoin-bitcoin/contributor/throws/) Error type and cases...
    +/// [@pre](/bitcoin-bitcoin/contributor/pre/)  Pre-condition for function...
    +/// [@post](/bitcoin-bitcoin/contributor/post/) Post-condition for function...
     bool function(int arg1, const char *arg2, std::string& arg3)
     ```
     
     A complete list of `@xxx` commands can be found at https://www.doxygen.nl/manual/commands.html.
    -As Doxygen recognizes the comments by the delimiters (`/**` and `*/` in this case), you don't
    -*need* to provide any commands for a comment to be valid; just a description text is fine.
    +Doxygen recognizes comments by their delimiters (`///` in this case), so you don't need to provide any commands for a comment to be valid.
    +A plain description is enough.
     
     To describe a class, use the same construct above the class definition:
    +
     ```c++
    -/**
    - * Alerts are for notifying old versions if they become too obsolete and
    - * need to upgrade. The message is displayed in the status bar.
    - * [@see](/bitcoin-bitcoin/contributor/see/) GetWarnings()
    - */
    +/// Alerts are for notifying old versions if they become too obsolete and need to upgrade.
    +/// The message is displayed in the status bar.
    +/// [@see](/bitcoin-bitcoin/contributor/see/) GetWarnings()
     class CAlert
     ```
     
    -To describe a member or variable, place the comment on the line(s) before it, using `/**` and `*/`, `//!`, or `///`:
    +To describe a member or variable, place the comment on the line(s) before it, using `//!` or `///`:
    +
     ```c++
     //! Description before the member
     int var;
    @@ -246,14 +249,6 @@ Avoid trailing (inline) member comments like `int var; //!< Description after th
     
       - *Rationale*: Forgetting the `<` silently breaks Doxygen output.
     
    -Also OK:
    -```c++
    -///
    -/// ... Description ...
    -///
    -bool function2(int arg1, const char *arg2)
    -```
    -
     Not picked up by Doxygen:
     ```c++
     //
    

    maflcko commented at 4:16 PM on November 11, 2026:
    +Prefer `///` for multiline comments, as the current Doxygen configuration automatically uses their first sentence as the brief description.
    

    Is this true? I don't think it matters for doxygen. This is just a style-nit

  38. maflcko commented at 8:45 AM on November 11, 2026: member

    Only change since the last sedited ack in #36049#pullrequestreview-5798743901 is adding a missing Assume(m_buf_pos == 0); // Use explicit flush().

    (re)review ACK 4f06757a16b456b2ec8e178224b2d528b2750c2 🥅

    <details><summary>Show signature</summary>

    Signature:

    untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
    RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
    trusted comment: (re)review ACK 4f06757a16b456b2ec8e178224b2d528b2750c2 🥅
    0GJG8Kno5/yMpP1OHAv4p96sM2+lMO0Pu9asy9OTpe2r3zQbAvJ062hFXD147gzSW4xGHwPVcqLH1d7Ijqr8Bg==
    

    </details>

  39. DrahtBot requested review from w0xlt on Nov 11, 2026
  40. streams: do not retry failed flushes
    A failed `BufferedWriter::flush()` leaves bytes pending, so the destructor retries the write while the exception unwinds.
    Clear the pending byte count before writing so the original exception can propagate without another write.
    a4199b39f9
  41. blockstorage: handle buffered write failures
    Flush block and undo data explicitly, catch write errors, log their cause, and request normal fatal shutdown, consistently with the adjacent file-close error handling.
    Detect missing explicit flushes with `Assume` in the `BufferedWriter` destructor.
    
    Co-authored-by: woltx <94266259+w0xlt@users.noreply.github.com>
    1c33fa05df
  42. l0rinc force-pushed on Nov 11, 2026
  43. maflcko commented at 4:13 PM on November 11, 2026: member

    re-ACK 1c33fa05dfe8a87444eebfdc0436d7cea3761600 🍛

    <details><summary>Show signature</summary>

    Signature:

    untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
    RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
    trusted comment: re-ACK 1c33fa05dfe8a87444eebfdc0436d7cea3761600 🍛
    nSvXaQ7LuHT0P85tH1f/G0+k6+76mFlGf0OF6Uj945Fs3tv9wFhIktkCWiay0+KnceT965zD0oGWKLjtVOcNCw==
    

    </details>

  44. in src/node/blockstorage.cpp:1176 in 1c33fa05df
    1172 |          fileout << GetParams().MessageStart() << block_size;
    1173 |          pos.nPos += STORAGE_HEADER_BYTES;
    1174 |          // Write block
    1175 |          fileout << TX_WITH_WITNESS(block);
    1176 | +        fileout.flush();
    1177 | +    } catch (const std::runtime_error& e) {
    


    ryanofsky commented at 8:39 PM on November 17, 2026:

    In commit "blockstorage: handle buffered write failures" (1c33fa05dfe8a87444eebfdc0436d7cea3761600)

    Could this catch std::ios_base::failure and call fileout.discard() (see the review summary), with fileout declared before the try so the catch can still call it?

    As written, the cleanup here is only correct for exceptions thrown by flush(), which clears m_buf_pos before writing. std::runtime_error makes it look like any failure in the block is handled, but if anything else threw while bytes were still buffered, ~BufferedWriter would run with data pending during unwinding, before this catch runs: the Assume would abort in debug builds, and release builds would write the failed block's partial data. Nothing else in the block throws right now, but nothing prevents other exceptions that would trigger the Assume. Adding a discard() call in the catch would make this actually exception-safe. Same in WriteBlockUndo.

    <details><summary>diff on top of 1c33fa05, with discard() from the review summary</summary>

    diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
    index 971f95cb18e..53e2110c935 100644
    --- a/src/node/blockstorage.cpp
    +++ b/src/node/blockstorage.cpp
    @@ -999,9 +999,8 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
                 LogError("OpenUndoFile failed for %s while writing block undo", pos.ToString());
                 return FatalError(m_opts.notifications, state, _("Failed to write undo data."));
             }
    +        BufferedWriter fileout{file};
             try {
    -            BufferedWriter fileout{file};
    -
                 // Write index header
                 fileout << GetParams().MessageStart() << blockundo_size;
                 pos.nPos += STORAGE_HEADER_BYTES;
    @@ -1013,7 +1012,8 @@ bool BlockManager::WriteBlockUndo(const CBlockUndo& blockundo, BlockValidationSt
                     fileout << blockundo << hasher.GetHash();
                 }
                 fileout.flush();
    -        } catch (const std::runtime_error& e) {
    +        } catch (const std::ios_base::failure& e) {
    +            fileout.discard();
                 LogError("Failed to write block undo file %s: %s", pos.ToString(), e.what());
                 (void)file.fclose();
                 return FatalError(m_opts.notifications, state, _("Failed to write undo data."));
    @@ -1164,16 +1164,16 @@ FlatFilePos BlockManager::WriteBlock(const CBlock& block, int nHeight)
             m_opts.notifications.fatalError(_("Failed to write block."));
             return FlatFilePos();
         }
    +    BufferedWriter fileout{file};
         try {
    -        BufferedWriter fileout{file};
    -
             // Write index header
             fileout << GetParams().MessageStart() << block_size;
             pos.nPos += STORAGE_HEADER_BYTES;
             // Write block
             fileout << TX_WITH_WITNESS(block);
             fileout.flush();
    -    } catch (const std::runtime_error& e) {
    +    } catch (const std::ios_base::failure& e) {
    +        fileout.discard();
             LogError("Failed to write block file %s: %s", pos.ToString(), e.what());
             (void)file.fclose();
             m_opts.notifications.fatalError(_("Failed to write block."));
    

    </details>

  45. in src/streams.h:705 in 1c33fa05df outdated
     697 | @@ -699,12 +698,18 @@ class BufferedWriter
     698 |  public:
     699 |      explicit BufferedWriter(S& stream LIFETIMEBOUND, size_t size = 1 << 16) : m_dst{stream}, m_buf(size) {}
     700 |  
     701 | -    ~BufferedWriter() { flush(); }
     702 | +    ~BufferedWriter()
     703 | +    {
     704 | +        Assume(m_buf_pos == 0); // Use explicit flush()
     705 | +        flush();
     706 | +    }
    


    ryanofsky commented at 8:46 PM on November 17, 2026:

    In commit "blockstorage: handle buffered write failures" (1c33fa05dfe8a87444eebfdc0436d7cea3761600)

    re: maflcko #36049 (comment)

    It would be good to find a code pattern that avoids this class of problem wholesale. It doesn't seem a great use of human (or LLM) review time to spend on such issues one-by-one. If we care about those issues, it would be better if the compiler or a clang-tidy analysis could find and prevent all of them.

    An established pattern is the one this PR moves toward: destructors don't do work that can fail, and every object ends in an explicit call that reports success or failure, checked by an Assume in the destructor, as AutoFile does with fclose(). With a discard() method (https://github.com/bitcoin/bitcoin/pull/36049#pullrequestreview-5946234150), BufferedWriter would follow it too.

    On a clang-tidy check: the existing bugprone-exception-escape check, which flags destructors that can throw, doesn't catch this bug. It reports nothing for ~BufferedWriter on master, probably because AutoFile::write_buffer() is defined in streams.cpp, where the check can't see that it throws. So it would probably need a custom bitcoin-tidy check.

    <details><summary>A possible better alternative: throwing destructors, as in Cap'n Proto</summary>

    Not something I'm suggesting for this PR, but another answer to the "wholesale" question: if ~BufferedWriter were allowed to throw, RAII would just work. The destructor would flush automatically and report a write failure like any other error, so callers wouldn't need explicit flush() or discard() calls, and if the writer were destroyed because some other exception was already propagating, that original exception would take precedence instead of the program calling std::terminate.

    This is how Cap'n Proto handles it: destructors are declared noexcept(false), and kj::UnwindDetector catches a destructor's failure only when another exception is already unwinding (the same rule as Java's try-with-resources). Both current callers would work this way, since each holds the writer as a local inside a try block.

    The downside is that a throwing destructor is only safe when no noexcept code sits between it and the catch. Holding the writer in std::unique_ptr, std::optional or std::vector, or destroying it inside a noexcept function, would turn the error back into std::terminate, so making this fully safe would need a bitcoin-tidy check that flags those uses.

    </details>


    l0rinc commented at 10:15 PM on November 17, 2026:

    Thanks Russ, added your two commits on top: I’m curious what the other reviewers think.

    This also reminded me of try/finally and try-with-resources in Java - I’d like us to find a structure that handles cleanup when we leave the scope, since it’s easy to miss a manual flush() or discard() on an error path. We can also work that out in a follow-up, and once we agree on a pattern, I’d love to add a bitcoin-tidy check for it.


    maflcko commented at 7:27 AM on November 18, 2026:

    On a clang-tidy check: the existing bugprone-exception-escape check, which flags destructors that can throw, doesn't catch this bug.

    Yeah, I think I looked into this earlier, but the results were too mixed. I guess it can't hurt to enable it still, because our assumption is that ctors shouldn't throw.

  46. ryanofsky commented at 9:36 PM on November 17, 2026: contributor

    Concept ACK 1c33fa05dfe8a87444eebfdc0436d7cea3761600. Turning the std::terminate on a failed block write into normal error handling is a clear improvement, but I think BufferedWriter needs a way to close without flushing.

    The new Assume(m_buf_pos == 0) in the destructor means a writer can only be destroyed safely after a flush(). That assumes the only thing that can go wrong while writing is the flush itself, and that nothing else is ever a reason to stop without writing the buffered data, such as an exception from other code in the same scope or a caller abandoning the write (interrupt/shutdown). Right now the only way to empty the buffer on an error path is a flush that fails, which flush() handles by clearing m_buf_pos before it writes. AutoFile doesn't have this problem, because fclose() satisfies its Assume whether or not the close succeeds.

    The try/catch blocks added to WriteBlock and WriteBlockUndo illustrate the problem (https://github.com/bitcoin/bitcoin/pull/36049#discussion_r4605490959): their cleanup is only correct because every exception in the block happens to come from flush(). A discard() method for error paths would mean every writer ends in an explicit flush() or discard(), with the destructor checking that, the same way AutoFile works.

        ~BufferedWriter()
        {
            Assume(m_buf_pos == 0); // Use explicit flush() or discard()
            flush();
        }
    
        //! Drop buffered bytes without writing them, e.g. after a write error.
        void discard() { m_buf_pos = 0; }
    
  47. in src/streams.h:707 in 1c33fa05df
     703 | +    {
     704 | +        Assume(m_buf_pos == 0); // Use explicit flush()
     705 | +        flush();
     706 | +    }
     707 |  
     708 | +    //! Discard the buffered bytes even if writing them throws
    


    ryanofsky commented at 9:44 PM on November 17, 2026:

    In commit "streams: do not retry failed flushes" (a4199b39f92c3e572196f464a067f70b1d77cda5)

    Note: It wasn't obvious to me why bytes were being discarded here. It looks like keeping them instead of discarding would be safer so a caller could retry, but a retry would write corrupt data: AutoFile::write_buffer() obfuscates the buffer in place before calling fwrite() and only advances its position after a successful write, so a second attempt would XOR the bytes again and write them unobfuscated. A short fwrite() can also leave part of the buffer already on disk. Maybe:

        //! Discard the buffered bytes even if writing them throws. After a failed
        //! write they can't be written again: the stream may have obfuscated them
        //! in place, and part of them may already be on disk.
    
  48. streams: explain why failed flushes discard bytes aade8e8a37
  49. blockstorage: discard buffered bytes on I/O errors
    A serialization failure can leave a partial record buffered. Keep the writer alive through the I/O error handler so it can drop those bytes before closing the file.
    1584299ae5
  50. maflcko commented at 7:14 AM on November 18, 2026: member

    The new Assume(m_buf_pos == 0) in the destructor means a writer can only be destroyed safely after a flush(). That assumes the only thing that can go wrong while writing is the flush itself

    Correct, and I think this assumption holds. If serializing one of our valid C++ structs (like a block) failed, it seems like a severe internal bug, and I think crashing here is better. Recall that your suggested patch will also continue to crash on non-io exceptions after the first buffered byte. Also, your patch will continue to crash if there was an interrupt (somewhere else due to an io error) and then the flush failed.

    No objection, just wondering what should be in scope and what should be better ignored.


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-20 15:51 UTC

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