zmq: Log bind error at Error level, abort startup on init error #35902

Open
chriszeng1010 wants to merge 1 commit from zmq-bind-abort into master AGit
chriszeng1010 commented 2026-08-05 17:38:58 +00:00 (Migrated from github.com)

History

This picks up #33727, which was closed for inactivity

Problem

Currently when a user configures -zmqpub* and the socket can't be bound, the program proceeds normally. The only trace is a debug-level log line that's invisible in a default configuration.

#33715 hit this when their port was already taken.

What this PR does

The node refuses to start if ZMQ is configured and setup failed. This failure is now logged at error level.

Code path:
GetNotifiers builds the notifier objects from the config; Create initializes them — starts the ZMQ engine and binds each socket. When initialization fails, Initialize() returns a util::Result carrying an error that names the notifier, the address and the OS error. Create() propagates it and init.cpp returns InitError with that message. On a node with no -zmqpub* option, the notifier list is empty.

Why abort?

#17445 fixed bitcoind crash from bad assert due to ZMQ config (issue #17185).
Issue #33715 reports that when ZMQ port was already taken, bitcoind started but user could not see anything with getzmqnotifications. Maintainers at the time agreed it should log louder and abort at startup. Which is what this PR fixes.

Testing

bitcoind -regtest -zmqpubhashblock=foo
Result: exits with code 1, and prints:

[error] Failed to bind address foo for pubhashblock: Invalid argument
Error: Failed to bind address foo for pubhashblock: Invalid argument

interface_zmq.py asserts the exact full error message for five startup-failure cases: invalid address, invalid address on a later notifier after an earlier one bound successfully (partial success must still abort), unreachable IP (EADDRNOTAVAIL), and a port already taken by the node's own RPC server (EADDRINUSE, via rpc_port()). The expected errno text is derived with os.strerror on the platform running the test, since the string after the last ":" differs per platform; on Windows the two strings libzmq hardcodes are pinned instead.

Verified locally on Windows; the full CI matrix is green on my fork.

A node with no -zmqpub* options starts exactly as before — nothing changes for nodes that don't use ZMQ.

## History This picks up #33727, which was closed for inactivity ## Problem Currently when a user configures -zmqpub* and the socket can't be bound, the program proceeds normally. The only trace is a debug-level log line that's invisible in a default configuration. #33715 hit this when their port was already taken. ## What this PR does The node refuses to start if ZMQ is configured and setup failed. This failure is now logged at error level. Code path: GetNotifiers builds the notifier objects from the config; Create initializes them — starts the ZMQ engine and binds each socket. When initialization fails, Initialize() returns a util::Result carrying an error that names the notifier, the address and the OS error. Create() propagates it and init.cpp returns InitError with that message. On a node with no -zmqpub* option, the notifier list is empty. ## Why abort? #17445 fixed bitcoind crash from bad assert due to ZMQ config (issue #17185). Issue #33715 reports that when ZMQ port was already taken, bitcoind started but user could not see anything with getzmqnotifications. Maintainers at the time agreed it should log louder and abort at startup. Which is what this PR fixes. ## Testing bitcoind -regtest -zmqpubhashblock=foo Result: exits with code 1, and prints: ``` [error] Failed to bind address foo for pubhashblock: Invalid argument Error: Failed to bind address foo for pubhashblock: Invalid argument ``` interface_zmq.py asserts the exact full error message for five startup-failure cases: invalid address, invalid address on a later notifier after an earlier one bound successfully (partial success must still abort), unreachable IP (EADDRNOTAVAIL), and a port already taken by the node's own RPC server (EADDRINUSE, via rpc_port()). The expected errno text is derived with os.strerror on the platform running the test, since the string after the last ":" differs per platform; on Windows the two strings libzmq hardcodes are pinned instead. Verified locally on Windows; the full CI matrix is green on my fork. A node with no -zmqpub* options starts exactly as before — nothing changes for nodes that don't use ZMQ.
DrahtBot commented 2026-08-05 17:39:05 +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 pinheadmz
Approach ACK jeanpablojp

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:

  • #37060 (zmq: improve handling of invalid configurations by jlopp)
  • #36904 (log: Remove trailing and embedded newlines from log messages 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.

<!--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/35902 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35902.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35902#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 | [pinheadmz](https://github.com/bitcoin/bitcoin/pull/35902#issuecomment-5206864644) | | Approach ACK | [jeanpablojp](https://github.com/bitcoin/bitcoin/pull/35902#issuecomment-5316718875) | 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: * [#37060](https://github.com/bitcoin/bitcoin/pull/37060) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37060.svg"></sub> (zmq: improve handling of invalid configurations by jlopp) * [#36904](https://github.com/bitcoin/bitcoin/pull/36904) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36904.svg"></sub> (log: Remove trailing and embedded newlines from log messages 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-->
sedited commented 2026-08-05 18:32:14 +00:00 (Migrated from github.com)

Can you drop the second commit? We don't typically change these names in separate drive-by commits.

Can you drop the second commit? We don't typically change these names in separate drive-by commits.
chriszeng1010 commented 2026-08-05 18:38:24 +00:00 (Migrated from github.com)

Can you drop the second commit? We don't typically change these names in separate drive-by commits.

Okie. Done.

> Can you drop the second commit? We don't typically change these names in separate drive-by commits. Okie. Done.
pinheadmz commented 2026-08-06 15:22:21 +00:00 (Migrated from github.com)

Concept ACK. Built and tested locally with a variety of zmq misconfigurations, permissioned ports, busy ports, etc.

Before I review the code I just noticed that the error reason is not bubbled up to stdout, and I wonder if we can do that?

Example: reason is "Failed to bind address, msg: Permission denied"

--> bcd -regtest -zmqpubhashblock="tcp://127.0.0.1:80" -debug=zmq -printtoconsole=0
Error: Initializing ZMQ interface failed.

Example: reason is "Failed to bind address, msg: Address already in use"

--> bcd -regtest -zmqpubhashblock="tcp://127.0.0.1:9050" -debug=zmq -printtoconsole=0
Error: Initializing ZMQ interface failed.

etc...

Concept ACK. Built and tested locally with a variety of zmq misconfigurations, permissioned ports, busy ports, etc. Before I review the code I just noticed that the error _reason_ is not bubbled up to stdout, and I wonder if we can do that? Example: reason is "Failed to bind address, msg: Permission denied" ``` --> bcd -regtest -zmqpubhashblock="tcp://127.0.0.1:80" -debug=zmq -printtoconsole=0 Error: Initializing ZMQ interface failed. ``` Example: reason is "Failed to bind address, msg: Address already in use" ``` --> bcd -regtest -zmqpubhashblock="tcp://127.0.0.1:9050" -debug=zmq -printtoconsole=0 Error: Initializing ZMQ interface failed. ``` etc...
chriszeng1010 commented 2026-08-07 17:10:11 +00:00 (Migrated from github.com)

I refactored the Initialize()'s return type to util::Result and it returns the error code and failure reason.

Tested:

$ mkdir -p /tmp/zmqcheck
./build/bin/bitcoind.exe -regtest -datadir="$(cygpath -m /tmp/zmqcheck)" -zmqpubhashblock=foo -printtoconsole=0
echo "exit code: $?"
Error: Failed to bind address foo for pubhashblock: Invalid argument
exit code: 1

I refactored the Initialize()'s return type to util::Result<void> and it returns the error code and failure reason. Tested: $ mkdir -p /tmp/zmqcheck ./build/bin/bitcoind.exe -regtest -datadir="$(cygpath -m /tmp/zmqcheck)" -zmqpubhashblock=foo -printtoconsole=0 echo "exit code: $?" Error: Failed to bind address foo for pubhashblock: Invalid argument exit code: 1
pinheadmz (Migrated from github.com) reviewed 2026-08-07 18:14:05 +00:00
pinheadmz (Migrated from github.com) commented 2026-08-07 18:14:05 +00:00

Now that you've got the error messages spitting out, can we cover a few specifically with the functional test? See how other tests add expected_msg to this check

Now that you've got the error messages spitting out, can we cover a few specifically with the functional test? See how other tests add `expected_msg` to this check
chriszeng1010 commented 2026-08-11 20:47:23 +00:00 (Migrated from github.com)

Latest push should fix broken CI.
Added expected_msg to existing test cases, plus a secondary case with different notifier type.

Latest push should fix broken CI. Added `expected_msg `to existing test cases, plus a secondary case with different notifier type.
chriszeng1010 (Migrated from github.com) reviewed 2026-08-11 20:50:18 +00:00
chriszeng1010 (Migrated from github.com) commented 2026-08-11 20:50:18 +00:00

ack.

ack.
pinheadmz (Migrated from github.com) reviewed 2026-08-12 15:19:10 +00:00
pinheadmz (Migrated from github.com) commented 2026-08-12 15:19:05 +00:00

Why use the regex here? You should be able to assert that the error is exactly this, right?

Error: Failed to bind address foo for pubrawblock: Invalid argument

Also I'd like to see a test covering the other expected errors:

(IP not available locally)
Error: Failed to bind address tcp://1.2.3.4:100 for pubrawblock: Can't assign requested address

(Port in use already by RPC -- will need to use rpc_port() in the test framework)
Error: Failed to bind address tcp://127.0.0.1:18443 for pubrawblock: Address already in use

Why use the regex here? You should be able to assert that the error is exactly this, right? `Error: Failed to bind address foo for pubrawblock: Invalid argument` Also I'd like to see a test covering the other expected errors: (IP not available locally) `Error: Failed to bind address tcp://1.2.3.4:100 for pubrawblock: Can't assign requested address` (Port in use already by RPC -- will need to use `rpc_port()` in the test framework) `Error: Failed to bind address tcp://127.0.0.1:18443 for pubrawblock: Address already in use`
jeanpablojp commented 2026-08-17 13:29:34 +00:00 (Migrated from github.com)

Approach ACK.

Built and ran interface_zmq.py, plus some manual broken-config scenarios, and the abort works. Agree with pinheadmz's test coverage requests.

One small observation is that the PR description is stale relative to the head (Code path and Testing).

Approach ACK. Built and ran `interface_zmq.py`, plus some manual broken-config scenarios, and the abort works. Agree with pinheadmz's test coverage requests. One small observation is that the PR description is stale relative to the head (Code path and Testing).
chriszeng1010 commented 2026-08-17 21:16:21 +00:00 (Migrated from github.com)

Added new test cases and verified locally on Windows OS. Linux/MacOS are pending CI.

I replaced the Regex with a string check. Since the error string after : comes from zmq_strerror, which isn't stable across platforms. The same failure will print different error messages on different platforms. Ie "Cannot assign requested address" on Linux, "Can't assign requested address" on Mac and "Address not available" on Windows.

Also, in the test case we point -zmqpubrawblock at the node's own RPC port, looked up via rpc_port()

Added new test cases and verified locally on Windows OS. Linux/MacOS are pending CI. I replaced the Regex with a string check. Since the error string after `:` comes from zmq_strerror, which isn't stable across platforms. The same failure will print different error messages on different platforms. Ie `"Cannot assign requested address" `on Linux, `"Can't assign requested address" `on Mac and` "Address not available"` on Windows. Also, in the test case we point `-zmqpubrawblock` at the node's own RPC port, looked up via `rpc_port()`
chriszeng1010 commented 2026-08-30 19:10:17 +00:00 (Migrated from github.com)

Can the reviewers please take a look at this PR? Thanks.

Can the reviewers please take a look at this PR? Thanks.
pinheadmz (Migrated from github.com) reviewed 2026-09-23 19:06:40 +00:00
pinheadmz (Migrated from github.com) commented 2026-09-23 19:06:40 +00:00

I don't understand why you need this GetNotifiers intermediate function. I would just expect to see CZMQNotificationInterface::Create() return a Result bubbling up from CZMQNotificationInterface::Initialize() as it attempts to call CZMQAbstractNotifier->Initialize() on each member of notifiers

I don't understand why you need this `GetNotifiers` intermediate function. I would just expect to see `CZMQNotificationInterface::Create()` return a `Result` bubbling up from `CZMQNotificationInterface::Initialize()` as it attempts to call `CZMQAbstractNotifier->Initialize()` on each member of `notifiers`
First-time contributor

Base: 66776840beb558f7e84451c2c55457f0e06242f0
Head: 311847fffa210b58436df138c11727d18a4c8aa9

No verified findings are available from this partial review.

Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `66776840beb558f7e84451c2c55457f0e06242f0` Head: `311847fffa210b58436df138c11727d18a4c8aa9` No verified findings are available from this partial review. Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review did not complete. [Full review report](<https://review.fish.foo/traces/35902-311847fffa210b58436df138c11727d18a4c8aa9-340-1.html>)
sedited commented 2026-11-05 17:13:04 +00:00 (Migrated from github.com)

@chriszeng1010 are you still working on this? There is an unaddressed review comment.

@chriszeng1010 are you still working on this? There is an unaddressed review comment.
chriszeng1010 commented 2026-11-05 17:39:57 +00:00 (Migrated from github.com)

Thanks for the ping. Yes i am.

Thanks for the ping. Yes i am.
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!35902
No description provided.