interfaces: Avoid node crashes from invalid Chain IPC calls #36520

Open
ryanofsky wants to merge 3 commits from pr/chainsafe into master AGit
ryanofsky commented 2026-10-14 05:39:36 +00:00 (Migrated from github.com)

Problem: A program using the Chain IPC interface from #29409 can crash bitcoin-node with a single bad call: asking for a block hash above the tip, passing a negative height to hasBlocks, leaving the transaction argument of broadcastTransaction or checkChainLimits unset, or returning an error from a notification handler. Separately, findAncestorByHash reports success when the descendant block is unknown (#36472). The Chain methods were written for in-process C++ callers like the wallet, which follow documented preconditions, so they assert or dereference null instead of returning errors.

Solution: Make these Chain methods in src/node/interfaces.cpp return errors for invalid arguments, catch and log IPC errors from notification handlers, and fix the findAncestorByHash check. Three small commits, each with unit tests in src/test/interfaces_tests.cpp. Valid calls behave the same as before. Details are in the commit messages.

This PR does not depend on #29409, but the fixes matter if it is merged and interfaces::Chain is exposed over IPC. The problems were reported by zaidmstrr, willcl-ark and xyzconstant while reviewing #29409, by enirox001 in #36472, and by cyb3ralbert and xyzconstant here (hasBlocks). The findAncestorByHash fix is the same one previously proposed by Romansage6 and Yudis-bit in #36473, #36484 and #36490.

Fixes #36472.

**Problem:** A program using the `Chain` IPC interface from #29409 can crash `bitcoin-node` with a single bad call: asking for a block hash above the tip, passing a negative height to `hasBlocks`, leaving the transaction argument of `broadcastTransaction` or `checkChainLimits` unset, or returning an error from a notification handler. Separately, `findAncestorByHash` reports success when the descendant block is unknown (#36472). The `Chain` methods were written for in-process C++ callers like the wallet, which follow documented preconditions, so they assert or dereference null instead of returning errors. **Solution:** Make these `Chain` methods in `src/node/interfaces.cpp` return errors for invalid arguments, catch and log IPC errors from notification handlers, and fix the `findAncestorByHash` check. Three small commits, each with unit tests in `src/test/interfaces_tests.cpp`. Valid calls behave the same as before. Details are in the commit messages. This PR does not depend on #29409, but the fixes matter if it is merged and `interfaces::Chain` is exposed over IPC. The problems were reported by zaidmstrr, willcl-ark and xyzconstant while reviewing #29409, by enirox001 in #36472, and by cyb3ralbert and xyzconstant here (`hasBlocks`). The `findAncestorByHash` fix is the same one previously proposed by Romansage6 and Yudis-bit in #36473, #36484 and #36490. Fixes #36472.
DrahtBot commented 2026-10-14 05:39:44 +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
Stale ACK xyzconstant, enirox001

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:

  • #37094 (validation: validate assumeutxo snapshots with a single chainstate by fjahr)
  • #36946 (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)

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.

LLM Linter (✨ experimental)

Possible places where named args for integral literals may be used (e.g. func(x, /*named_arg=*/0) in C++, and func(x, named_arg=0) in Python):

  • src/test/interfaces_tests.cpp: chain->hasBlocks(tip->GetBlockHash(), 0, -1)

Possible places where comparison-specific test macros should replace generic comparisons:

  • src/test/interfaces_tests.cpp: BOOST_CHECK_THROW(chain->getBlockHash(tip->nHeight + 1), std::runtime_error) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected error message.
  • src/test/interfaces_tests.cpp: BOOST_CHECK_THROW(chain->getBlockHash(-1), std::runtime_error) → Prefer BOOST_CHECK_EXCEPTION with a predicate checking the expected error message.

2026-11-05 17:52:29

<!--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/36520 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36520.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36520#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 | | ---- | --------- | | Stale ACK | [xyzconstant](https://github.com/bitcoin/bitcoin/pull/36520#pullrequestreview-5621447619), [enirox001](https://github.com/bitcoin/bitcoin/pull/36520#pullrequestreview-5712899549) | 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: * [#37094](https://github.com/bitcoin/bitcoin/pull/37094) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37094.svg"></sub> (validation: validate assumeutxo snapshots with a single chainstate by fjahr) * [#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) 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--> ### LLM Linter (✨ experimental) Possible places where named args for integral literals may be used (e.g. `func(x, /*named_arg=*/0)` in C++, and `func(x, named_arg=0)` in Python): - `src/test/interfaces_tests.cpp`: `chain->hasBlocks(tip->GetBlockHash(), 0, -1)` Possible places where comparison-specific test macros should replace generic comparisons: - `src/test/interfaces_tests.cpp`: `BOOST_CHECK_THROW(chain->getBlockHash(tip->nHeight + 1), std::runtime_error)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected error message. - `src/test/interfaces_tests.cpp`: `BOOST_CHECK_THROW(chain->getBlockHash(-1), std::runtime_error)` → Prefer `BOOST_CHECK_EXCEPTION` with a predicate checking the expected error message. <sup>2026-11-05 17:52:29</sup>
First-time contributor

🟠 Minor · The new height exception leaves failed wallet loads subscribed

A height decrease during wallet attachment can return a load error while retaining the wallet's database lock until restart, and callbacks can advance its rescan checkpoint past unscanned blocks.

Location: src/node/interfaces.cpp:601 (head).

🔵 Suggestion · Notification tests do not enforce the selective exception policy

All throwing callbacks use ipc::Exception, so broadening Notify to catch std::runtime_error or std::exception would leave these assertions passing.

Location: src/test/interfaces_tests.cpp:207 (head).

Review coverage was incomplete.


Reviewed: e929e16f9843 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:4d44b6ffa09c54db24f8f0ab27ae388beae06a733efcae741ca64db76bb1c938:7efd3c3ba41667382004cfa7a7db56605da7d8f9b7ab69bc483f27117390ba48 --> <!-- ralph-base:83e4347e048d400cc4d92c9af7a6a0b5d8fc3733 --> <!-- ralph-head:e929e16f98432de82189670112c92218ececa8ac --> <!-- ralph-case:32f60a7b38ae193c80c20d9f399ce4bbed794c807ee22fcbef007edca64d606c --> **🟠 Minor · The new height exception leaves failed wallet loads subscribed** A height decrease during wallet attachment can return a load error while retaining the wallet's database lock until restart, and callbacks can advance its rescan checkpoint past unscanned blocks. Location: [`src/node/interfaces.cpp:601`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/e929e16f98432de82189670112c92218ececa8ac/src/node/interfaces.cpp#L601>) (head). **🔵 Suggestion · Notification tests do not enforce the selective exception policy** All throwing callbacks use `ipc::Exception`, so broadening `Notify` to catch `std::runtime_error` or `std::exception` would leave these assertions passing. Location: [`src/test/interfaces_tests.cpp:207`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/e929e16f98432de82189670112c92218ececa8ac/src/test/interfaces_tests.cpp#L207>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`e929e16f9843`](<https://git.fish.foo/bitcoin/bitcoin/commit/e929e16f98432de82189670112c92218ececa8ac>) · [Full review report](<https://review.fish.foo/traces/36520-e929e16f98432de82189670112c92218ececa8ac-2258-8.html>) · [Previous reports](<https://review.fish.foo/traces/36520/>) [permalink](<https://review.fish.foo/traces/36520-e929e16f98432de82189670112c92218ececa8ac-2258-8.html>)
enirox001 (Migrated from github.com) reviewed 2026-10-14 10:43:08 +00:00
enirox001 (Migrated from github.com) left a comment

Concept ACK

I like this approach, which includes the specific fixes while also making the chain interface more defensive.

These methods were originally being called in process where the callers are expected to follow the preconditions, but once they are exposed over IPC, it makes sense to be more defensive around invalid inputs. Ideally an unexpected/bad IPC call should result in an error rather than crashing the nod

Concept ACK I like this approach, which includes the specific fixes while also making the chain interface more defensive. These methods were originally being called in process where the callers are expected to follow the preconditions, but once they are exposed over IPC, it makes sense to be more defensive around invalid inputs. Ideally an unexpected/bad IPC call should result in an error rather than crashing the nod
cyb3ralbert commented 2026-10-22 09:45:00 +00:00 (Migrated from github.com)

Could hasBlocks() be covered here, or should it be a follow-up? It has the same kind of crash as the ones fixed here. With a negative max_height, block->GetAncestor(*max_height) returns null (chain.cpp:84), and the loop then dereferences it (node/interfaces.cpp:691-692 @ b67d871f95, reading the code). Over IPC, hasBlocks(tip, 0, -1) kills the node (exit -11) with and without this PR; an in-process scratch unit test hits a memory access violation.

Is out_of_range intended over a runtime_error-derived type? getBlockHash now throws std::out_of_range (a std::logic_error), while wallet/load.cpp:161 and wallet/wallet.cpp:313 catch only std::runtime_error. Reading the code, a throw during loadwallet would skip the FAILED_LOAD handling and reach the generic std::exception catch in rpc/server.cpp:891, and one during startup wallet loading would skip initError. I did not exercise this path.

Tested on Linux aarch64 (Debian 13), GCC 14.2.0, with b67d871f95 merged with #29409 (0928430e16, merges cleanly). The PR alone also merges cleanly onto master de96f3ae6d.

  • interfaces_tests: 8/8 pass. Full unit suite: 879 cases pass. Functional (tool_bitcoin, feature_init, wallet_*, mempool_*, interface_ipc*): 95 passed, 4 skipped (previous releases not available).
  • Reverting the src changes of one commit at a time, while keeping the PR's tests: 03e46542e6 makes findAncestorByHash fail at interfaces_tests.cpp:108; 42fcc504e2 makes invalidArguments abort in getBlockHash; b67d871f95 makes throwingNotifications terminate on the uncaught ipc::Exception. The other two tests pass in each case.
  • Over IPC with a Python capnp client, comparing the same tree with the PR's src changes reverted against the merged tree:
    • getBlockHash(tip+1) and getBlockHash(-1): node exits with -6 (assertion), vs error Block height N is not in the active chain with the node alive.
    • broadcastTransaction and checkChainLimits with tx unset: node exits with -11, vs Transaction is null.
    • broadcastTransaction with method 255: (true, ""), vs Invalid broadcast method 255.
    • findAncestorByHash(unknown block, known ancestor): true, vs false.
    • A handler that raises in its first blockConnected: node exits with -6, vs node alive, one Chain notification blockConnected failed warning, and the handler still receives blockConnected and updatedBlockTip 3/3 for three mined blocks.
    • Client process killed while a blockConnected call is in flight: terminate on ipc::Exception: IPC client method call interrupted by disconnect. (exit -6), vs node alive with Chain notification blockConnected failed: IPC client method call interrupted by disconnect. and the next block mined normally. Client killed while its handler is registered but no call is in flight: node alive in both.
  • Coverage nit: the PR's tests make only blockConnected and updatedBlockTip throw. A scratch case throwing ipc::Exception from all six Notifications methods passes on the head. Removing the Notify wrapper from transactionAddedToMempool, transactionRemovedFromMempool, blockDisconnected or chainStateFlushed one at a time leaves the PR's three tests passing, while the scratch case fails for each.

Not tested: a reorg between getHeight() and getBlockHash() at the wallet call sites.

Could `hasBlocks()` be covered here, or should it be a follow-up? It has the same kind of crash as the ones fixed here. With a negative `max_height`, `block->GetAncestor(*max_height)` returns null (`chain.cpp:84`), and the loop then dereferences it (`node/interfaces.cpp:691-692` @ b67d871f95, reading the code). Over IPC, `hasBlocks(tip, 0, -1)` kills the node (exit -11) with and without this PR; an in-process scratch unit test hits a memory access violation. Is `out_of_range` intended over a `runtime_error`-derived type? `getBlockHash` now throws `std::out_of_range` (a `std::logic_error`), while `wallet/load.cpp:161` and `wallet/wallet.cpp:313` catch only `std::runtime_error`. Reading the code, a throw during `loadwallet` would skip the `FAILED_LOAD` handling and reach the generic `std::exception` catch in `rpc/server.cpp:891`, and one during startup wallet loading would skip `initError`. I did not exercise this path. Tested on Linux aarch64 (Debian 13), GCC 14.2.0, with b67d871f95fa1b9c47fc2f8d08d80e27f0a81e16 merged with #29409 (0928430e16, merges cleanly). The PR alone also merges cleanly onto master de96f3ae6d. - `interfaces_tests`: 8/8 pass. Full unit suite: 879 cases pass. Functional (`tool_bitcoin`, `feature_init`, `wallet_*`, `mempool_*`, `interface_ipc*`): 95 passed, 4 skipped (previous releases not available). - Reverting the `src` changes of one commit at a time, while keeping the PR's tests: 03e46542e6 makes `findAncestorByHash` fail at `interfaces_tests.cpp:108`; 42fcc504e2 makes `invalidArguments` abort in `getBlockHash`; b67d871f95 makes `throwingNotifications` terminate on the uncaught `ipc::Exception`. The other two tests pass in each case. - Over IPC with a Python capnp client, comparing the same tree with the PR's `src` changes reverted against the merged tree: - `getBlockHash(tip+1)` and `getBlockHash(-1)`: node exits with -6 (assertion), vs error `Block height N is not in the active chain` with the node alive. - `broadcastTransaction` and `checkChainLimits` with `tx` unset: node exits with -11, vs `Transaction is null`. - `broadcastTransaction` with method 255: `(true, "")`, vs `Invalid broadcast method 255`. - `findAncestorByHash(unknown block, known ancestor)`: `true`, vs `false`. - A handler that raises in its first `blockConnected`: node exits with -6, vs node alive, one `Chain notification blockConnected failed` warning, and the handler still receives `blockConnected` and `updatedBlockTip` 3/3 for three mined blocks. - Client process killed while a `blockConnected` call is in flight: terminate on `ipc::Exception: IPC client method call interrupted by disconnect.` (exit -6), vs node alive with `Chain notification blockConnected failed: IPC client method call interrupted by disconnect.` and the next block mined normally. Client killed while its handler is registered but no call is in flight: node alive in both. - Coverage nit: the PR's tests make only `blockConnected` and `updatedBlockTip` throw. A scratch case throwing `ipc::Exception` from all six `Notifications` methods passes on the head. Removing the `Notify` wrapper from `transactionAddedToMempool`, `transactionRemovedFromMempool`, `blockDisconnected` or `chainStateFlushed` one at a time leaves the PR's three tests passing, while the scratch case fails for each. Not tested: a reorg between `getHeight()` and `getBlockHash()` at the wallet call sites.
xyzconstant (Migrated from github.com) reviewed 2026-10-22 23:09:58 +00:00
xyzconstant (Migrated from github.com) left a comment

LGTM ACK b67d871f95. Left some nits on the NotificationsProxy changes.

This PR fixes a set of issues reported by IPC consumers testing the Chain interface exposed in #29409.

For anyone wondering, here is a short summary of the flawed Chain interface methods and their fixes:

  • findAncestorByHash - Returned an incorrect value (true) if an unknown block was passed, fixed by explicitly checking if the value returned by BlockManager::LookupBlockIndex on the block_hash arg was null.
  • getBlockHash - The assertion failed if the provided height was out of the active chain bounds, crashing the process. Fixed by throwing std::out_of_range instead.
  • broadcastTransaction, checkChainLimits - Passing a null tx crashes the process since it is internally dereferenced. Fixed by adding an early check on tx ensuring it's not null.
  • broadcastTransaction - The broadcast_method can be anything other than the values in TxBroadcast when received over IPC. Fixed by adding a helper that checks this arg is valid rather than relying on the type system.

In addition to these Chain interface methods, the process can also perform IPC calls back to subscribers provided by clients. Such IPC calls may fail, and before this PR, we didn't handle these failures because in-process call failures are bugs; now, this PR catches only IPC exceptions (ipc::Exception) and logs a warning.

Checked whether these issues exist across other Chain methods not modified by this PR, but only hasBlocks() (as @cyb3ralbert reported in https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539) fails if a negative max_height is passed. Here's a diff to test it:

diff --git a/src/test/interfaces_tests.cpp b/src/test/interfaces_tests.cpp
index 031415bff1..98eb28a8e3 100644
--- a/src/test/interfaces_tests.cpp
+++ b/src/test/interfaces_tests.cpp
@@ -187,6 +187,8 @@ BOOST_FIXTURE_TEST_CASE(invalidArguments, TestChain100Setup)
     BOOST_CHECK_THROW(chain->getBlockHash(tip->nHeight + 1), std::out_of_range);
     BOOST_CHECK_THROW(chain->getBlockHash(-1), std::out_of_range);
 
+    BOOST_CHECK(!chain->hasBlocks(tip->GetBlockHash(), /*min_height=*/0, /*max_height=*/-1));
+
     BOOST_CHECK(!chain->checkChainLimits(nullptr));
 
     std::string err;

I think this issue should also be covered in this PR. Nonetheless, the current fixes here are clean and can be merged as is.

LGTM ACK b67d871f95. Left some nits on the NotificationsProxy changes. This PR fixes a set of issues reported by IPC consumers testing the Chain interface exposed in #29409. For anyone wondering, here is a short summary of the flawed Chain interface methods and their fixes: - `findAncestorByHash` - Returned an incorrect value (`true`) if an unknown block was passed, fixed by explicitly checking if the value returned by `BlockManager::LookupBlockIndex` on the block_hash arg was null. - `getBlockHash` - The assertion failed if the provided `height` was out of the active chain bounds, crashing the process. Fixed by throwing `std::out_of_range` instead. - `broadcastTransaction`, `checkChainLimits` - Passing a null tx crashes the process since it is internally dereferenced. Fixed by adding an early check on `tx` ensuring it's not null. - `broadcastTransaction` - The `broadcast_method` can be anything other than the values in `TxBroadcast` when received over IPC. Fixed by adding a helper that checks this arg is valid rather than relying on the type system. In addition to these Chain interface methods, the process can also perform IPC calls back to subscribers provided by clients. Such IPC calls may fail, and before this PR, we didn't handle these failures because in-process call failures are bugs; now, this PR catches only IPC exceptions (`ipc::Exception`) and logs a warning. Checked whether these issues exist across other Chain methods not modified by this PR, but only `hasBlocks()` (as @cyb3ralbert reported in https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539) fails if a negative `max_height` is passed. Here's a diff to test it: ```diff diff --git a/src/test/interfaces_tests.cpp b/src/test/interfaces_tests.cpp index 031415bff1..98eb28a8e3 100644 --- a/src/test/interfaces_tests.cpp +++ b/src/test/interfaces_tests.cpp @@ -187,6 +187,8 @@ BOOST_FIXTURE_TEST_CASE(invalidArguments, TestChain100Setup) BOOST_CHECK_THROW(chain->getBlockHash(tip->nHeight + 1), std::out_of_range); BOOST_CHECK_THROW(chain->getBlockHash(-1), std::out_of_range); + BOOST_CHECK(!chain->hasBlocks(tip->GetBlockHash(), /*min_height=*/0, /*max_height=*/-1)); + BOOST_CHECK(!chain->checkChainLimits(nullptr)); std::string err; ``` I think this issue should also be covered in this PR. Nonetheless, the current fixes here are clean and can be merged as is.
Lines 514-520
xyzconstant (Migrated from github.com) commented 2026-10-22 22:19:28 +00:00

Style nit, feel free to ignore:

I would like to see something shorter and more concise, like:

//! Call a chain notification method on the subscriber.
//!
//! For IPC subscribers: If the call fails because the subscriber returned an
//! error or disconnected, just log a warning. In-process subscribers are not
//! expected to fail.
Style nit, feel free to ignore: I would like to see something shorter and more concise, like: ```c++ //! Call a chain notification method on the subscriber. //! //! For IPC subscribers: If the call fails because the subscriber returned an //! error or disconnected, just log a warning. In-process subscribers are not //! expected to fail. ```
xyzconstant (Migrated from github.com) commented 2026-10-22 22:06:07 +00:00

nit: This msg could be more explicit with something like "Failed to deliver %s notification to IPC subscriber: %s"

nit: This msg could be more explicit with something like `"Failed to deliver %s notification to IPC subscriber: %s"`
enirox001 (Migrated from github.com) reviewed 2026-10-30 15:24:50 +00:00
enirox001 (Migrated from github.com) left a comment

tACK b67d871f95

The fixes in these commits do resolve the problems reported in #36472 and during review of #29409. I applied the changes and confirmed the problem in #36472 is resolved in a functional test.

I think the negative max_height case in hasBlocks and the findAncestorByHash issue look related, since both rely on assumptions about lookup results. hasBlocks can dereference nullptr after GetAncestor receives a negative height, while findAncestorByHash could return true when the starting block was unknown.

I wonder if there is a shared way to handle invalid inputs and failed lookups across the exposed Chain interface in Core, so other methods don't repeat the same mistakes? Probably something for a followup.

tACK b67d871f95 The fixes in these commits do resolve the problems reported in [#36472](https://github.com/bitcoin/bitcoin/issues/36472) and during review of [#29409](https://github.com/bitcoin/bitcoin/pull/29409). I applied the changes and confirmed the problem in #36472 is resolved in a functional test. I think the negative max_height case in `hasBlocks` and the `findAncestorByHash` issue look related, since both rely on assumptions about lookup results. `hasBlocks` can dereference nullptr after `GetAncestor` receives a negative height, while `findAncestorByHash` could return true when the starting block was unknown. I wonder if there is a shared way to handle invalid inputs and failed lookups across the exposed Chain interface in Core, so other methods don't repeat the same mistakes? Probably something for a followup.
enirox001 (Migrated from github.com) commented 2026-10-30 13:25:51 +00:00

In commit : interfaces: Catch IPC exceptions thrown by Chain notification handlers

We can remove the redundant queue synchronization in throwingNotifications as CreateAndProcessBlock already synchronizes notifications by default

In commit : _interfaces: Catch IPC exceptions thrown by Chain notification handlers_ We can remove the redundant queue synchronization in throwingNotifications as `CreateAndProcessBlock` already synchronizes notifications by default
ryanofsky (Migrated from github.com) reviewed 2026-11-05 17:52:38 +00:00
ryanofsky (Migrated from github.com) left a comment

Thanks for the reviews!

Rebased b67d871f95 -> e929e16f98 (pr/chainsafe.1 -> pr/chainsafe.2, compare) to fix the hasBlocks crash with a negative max_height (https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539, https://github.com/bitcoin/bitcoin/pull/36520#pullrequestreview-5621447619), throw std::runtime_error from getBlockHash so the wallet's existing error handling applies, make the notification test throw from all six Notifications methods, and take the comment and test suggestions from xyzconstant and enirox001

Thanks for the reviews! <!-- begin push-2 --> Rebased b67d871f95fa1b9c47fc2f8d08d80e27f0a81e16 -> e929e16f98432de82189670112c92218ececa8ac ([`pr/chainsafe.1`](https://github.com/ryanofsky/bitcoin/commits/pr/chainsafe.1) -> [`pr/chainsafe.2`](https://github.com/ryanofsky/bitcoin/commits/pr/chainsafe.2), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/chainsafe.1-rebase..pr/chainsafe.2))<!-- end --> to fix the `hasBlocks` crash with a negative `max_height` (https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539, https://github.com/bitcoin/bitcoin/pull/36520#pullrequestreview-5621447619), throw `std::runtime_error` from `getBlockHash` so the wallet's existing error handling applies, make the notification test throw from all six `Notifications` methods, and take the comment and test suggestions from xyzconstant and enirox001
Lines 514-520
ryanofsky (Migrated from github.com) commented 2026-11-05 17:48:26 +00:00

re: https://github.com/bitcoin/bitcoin/pull/36520#discussion_r4354127118

Thanks, took this in the latest push: the doc comment is now close to your version, and the sentence about why in-process exceptions are not caught moved next to the catch.

re: https://github.com/bitcoin/bitcoin/pull/36520#discussion_r4354127118 Thanks, took this in the latest push: the doc comment is now close to your version, and the sentence about why in-process exceptions are not caught moved next to the `catch`.
ryanofsky (Migrated from github.com) commented 2026-11-05 17:48:24 +00:00

re: https://github.com/bitcoin/bitcoin/pull/36520#discussion_r4354026937

Thanks, agree it should say an IPC client was involved. Changed to "IPC client failed to handle %s notification: %s" in the latest push, since the exception also covers a client that received the call and returned an error.

re: https://github.com/bitcoin/bitcoin/pull/36520#discussion_r4354026937 Thanks, agree it should say an IPC client was involved. Changed to `"IPC client failed to handle %s notification: %s"` in the latest push, since the exception also covers a client that received the call and returned an error.
ryanofsky (Migrated from github.com) commented 2026-11-05 17:48:22 +00:00

re: cyb3ralbert https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539

In commit "interfaces: Return errors instead of crashing on invalid Chain arguments" (42fcc504e2)

Good point, out_of_range wasn't chosen for any reason that outweighs this. The latest push throws std::runtime_error instead, so the wallet's existing loadwallet and startup error handling applies if a reorg makes the height invalid between getHeight() and getBlockHash(). IPC clients see the same error message either way.

re: cyb3ralbert https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539 In commit "interfaces: Return errors instead of crashing on invalid Chain arguments" (42fcc504e21b6a7c38e696f7a5e4b7b808d624bd) Good point, `out_of_range` wasn't chosen for any reason that outweighs this. The latest push throws `std::runtime_error` instead, so the wallet's existing `loadwallet` and startup error handling applies if a reorg makes the height invalid between `getHeight()` and `getBlockHash()`. IPC clients see the same error message either way.
Lines 698-699
ryanofsky (Migrated from github.com) commented 2026-11-05 17:49:06 +00:00

re: cyb3ralbert https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539

In commit "interfaces: Return errors instead of crashing on invalid Chain arguments" (42fcc504e2)

Thanks for finding this, and @xyzconstant for checking the other Chain methods and confirming it is the only remaining one. Since it is the same kind of bug as the others here, the latest push fixes it in this PR: hasBlocks now returns false when max_height is negative instead of dereferencing null, and invalidArguments has xyzconstant's test case.

re: cyb3ralbert https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539 In commit "interfaces: Return errors instead of crashing on invalid Chain arguments" (42fcc504e21b6a7c38e696f7a5e4b7b808d624bd) Thanks for finding this, and @xyzconstant for checking the other `Chain` methods and confirming it is the only remaining one. Since it is the same kind of bug as the others here, the latest push fixes it in this PR: `hasBlocks` now returns false when `max_height` is negative instead of dereferencing null, and `invalidArguments` has xyzconstant's test case.
ryanofsky (Migrated from github.com) commented 2026-11-05 17:48:30 +00:00

re: enirox001 https://github.com/bitcoin/bitcoin/pull/36520#pullrequestreview-5712899549

In commit "interfaces: Return errors instead of crashing on invalid Chain arguments" (42fcc504e2)

Thanks for testing. Agree a shared approach would be better than fixing methods one at a time as reports come in, and that it fits a follow-up. A fuzz target that calls Chain methods over IPC with arbitrary arguments might be the most direct way to find the remaining cases.

re: enirox001 https://github.com/bitcoin/bitcoin/pull/36520#pullrequestreview-5712899549 In commit "interfaces: Return errors instead of crashing on invalid Chain arguments" (42fcc504e21b6a7c38e696f7a5e4b7b808d624bd) Thanks for testing. Agree a shared approach would be better than fixing methods one at a time as reports come in, and that it fits a follow-up. A fuzz target that calls `Chain` methods over IPC with arbitrary arguments might be the most direct way to find the remaining cases.
ryanofsky (Migrated from github.com) commented 2026-11-05 17:48:28 +00:00
re: https://github.com/bitcoin/bitcoin/pull/36520#discussion_r4424094065 Thanks, removed in the latest push.
ryanofsky (Migrated from github.com) commented 2026-11-05 17:48:23 +00:00

re: cyb3ralbert https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539

In commit "interfaces: Catch IPC exceptions thrown by Chain notification handlers" (b67d871f95)

Thanks for the very thorough testing, especially the per-commit reverts and the client-killed-during-a-call case. Agree on the coverage gap: the latest push makes all six Notifications methods throw in this test, so removing any of the Notify wrappers makes it fail.

re: cyb3ralbert https://github.com/bitcoin/bitcoin/pull/36520#issuecomment-6337747539 In commit "interfaces: Catch IPC exceptions thrown by Chain notification handlers" (b67d871f95fa1b9c47fc2f8d08d80e27f0a81e16) Thanks for the very thorough testing, especially the per-commit reverts and the client-killed-during-a-call case. Agree on the coverage gap: the latest push makes all six `Notifications` methods throw in this test, so removing any of the `Notify` wrappers makes it fail.
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!36520
No description provided.