zmq: improve handling of invalid configurations #37060

Open
jlopp wants to merge 1 commit from zmgConfigHandler into master AGit
jlopp commented 2026-12-02 17:44:05 +00:00 (Migrated from github.com)

While messing around with various zmq data ingestion setups for a side project I initially was perplexed because my node kept returning an empty array when I called bitcoin-cli getzmqnotifications even though I had 4 zmq emitters configured.

Upon closer inspection, it looks like if a single zmq config is invalid / fails to be bound, it prevents all other zmq configurations from being bound. And even worse, the failure occurs silently.

The goal of this PR is to not allow one invalid config to prevent valid configs from working and to make binding failures more obvious in the node logs. Alternatively, if reviewers find it preferable, we could make any zmq bind failure to be a hard error that blocks the node from launching.

Initialize() now attempts every configured notifier, erases only the failed ones, and returns success if at least one remains. Failures are logged:

  • Per-notifier: Failed to initialize ZMQ notifier at address ; it will be disabled
  • Summary: Initialized X of Y ZMQ notifier(s); Z failed

getzmqnotifications now reports the surviving notifiers instead of returning [].

Added ZMQAddressKey() to canonicalize addresses for socket sharing:

  • tcp://localhost:N and tcp://127.0.0.1:N now share one socket.
  • Hostname resolution prefers IPv4 unless the address is a numeric IPv6 literal, matching IsZMQAddressIPV6()/libzmq behavior.
  • Wildcard */empty hosts map to the IPv4 any address.

Each notifier now stores its canonical key, Shutdown() uses it, and failed init paths clear psocket after zmq_close() to avoid the previous double-close hazard.

While messing around with various zmq data ingestion setups for a side project I initially was perplexed because my node kept returning an empty array when I called `bitcoin-cli getzmqnotifications` even though I had 4 zmq emitters configured. Upon closer inspection, it looks like if a single zmq config is invalid / fails to be bound, it prevents all other zmq configurations from being bound. And even worse, the failure occurs silently. The goal of this PR is to not allow one invalid config to prevent valid configs from working and to make binding failures more obvious in the node logs. Alternatively, if reviewers find it preferable, we could make any zmq bind failure to be a hard error that blocks the node from launching. Initialize() now attempts every configured notifier, erases only the failed ones, and returns success if at least one remains. Failures are logged: * Per-notifier: Failed to initialize ZMQ notifier <type> at address <addr>; it will be disabled * Summary: Initialized X of Y ZMQ notifier(s); Z failed getzmqnotifications now reports the surviving notifiers instead of returning []. Added ZMQAddressKey() to canonicalize addresses for socket sharing: * tcp://localhost:N and tcp://127.0.0.1:N now share one socket. * Hostname resolution prefers IPv4 unless the address is a numeric IPv6 literal, matching IsZMQAddressIPV6()/libzmq behavior. * Wildcard */empty hosts map to the IPv4 any address. Each notifier now stores its canonical key, Shutdown() uses it, and failed init paths clear psocket after zmq_close() to avoid the previous double-close hazard.
DrahtBot commented 2026-12-02 18:13:30 +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.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #35902 (zmq: Log bind error at Error level, abort startup on init error by chriszeng1010)

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/37060 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/37060.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/37060#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. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#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) * [#35902](https://github.com/bitcoin/bitcoin/pull/35902) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35902.svg"></sub> (zmq: Log bind error at Error level, abort startup on init error by chriszeng1010) 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-12-02 19:37:22 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/46936548306/job/143659617167
LLM reason (✨ experimental): iwyu_fail: IWYU found required include changes in several files, leaving a diff that caused the check to fail.

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.

  • Intermittent network issues can occur; re-run CI for now, and report the issue if they keep happening.

  • A functional or integration test failure may be intermittent or unrelated to this pull request.
    If no dedicated issue thread exists, create a new one to track it.

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/46936548306/job/143659617167</sub> <sub>LLM reason (✨ experimental): iwyu_fail: IWYU found required include changes in several files, leaving a diff that caused the check to fail.</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. * Intermittent network issues can occur; re-run CI for now, and report the issue if they keep happening. * A functional or integration test failure may be intermittent or unrelated to this pull request. If no dedicated issue thread exists, create a new one to track it. Leave a comment here, if you need help tracking down a confusing failure. </details>
First-time contributor

🟠 Minor · Scheme-less addresses collide with canonical TCP keys

After a TCP notifier binds, a later notifier configured with the same scheme-less address can silently reuse its socket and appear active without undergoing bind validation.

Location: src/zmq/zmqutil.cpp:61 (head).

🔵 Suggestion · Hostname sharing keys are not tied to the bound endpoint

ZMQAddressKey selects a resolved address, but zmq_bind receives the original hostname, leaving socket identity dependent on unverified resolver agreement; no actual endpoint mismatch has been established.

Location: src/zmq/zmqutil.cpp:53 (head).

🔵 Suggestion · New ZMQ tests do not check message delivery

The partial-failure and alias tests check logs and RPC metadata, but would not detect a notifier that remains listed while its shared socket fails to deliver its messages.

Location: test/functional/interface_zmq.py:596 (head).

🔵 Suggestion · One commit couples partial initialization with DNS-based sharing

bf5464bd combines failure isolation with a separate DNS-based socket-sharing policy, making the fix harder to review, backport, or revert independently.

Location: src/zmq/zmqutil.cpp:26 (commit).

🔵 Suggestion · The ZMQ startup behavior change lacks a release note

A failed notifier no longer disables every notification, so operators monitoring getzmqnotifications for an empty result need upgrade guidance.

Location: src/zmq/zmqnotificationinterface.cpp:126 (head).

🔵 Suggestion · Cannot parse also reports hostname-resolution failures

A well-formed tcp:// hostname that cannot resolve is logged as a parse failure, directing operators toward syntax rather than name resolution.

Location: src/zmq/zmqpublishnotifier.cpp:103 (head).

Review coverage was incomplete.


Reviewed: bf5464bdc9f2 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:e1a29e5e677c47d383756f0fcf8f3760725c91d1104dd3ccb2f333b6217eba26:47880f5569fd2047d249266f337c4f2a509adf7d96c3ccb1c7644c2c2a650bff --> <!-- ralph-base:8e5a5a25dad4744219c579589231feff3f0b6d47 --> <!-- ralph-head:bf5464bdc9f2f916800536df3a39591ea0ffff0c --> <!-- ralph-case:1795d1f636544ed7f87846c4d0dc26d8098091008cea4f7679106cde9042727e --> **🟠 Minor · Scheme-less addresses collide with canonical TCP keys** After a TCP notifier binds, a later notifier configured with the same scheme-less address can silently reuse its socket and appear active without undergoing bind validation. Location: [`src/zmq/zmqutil.cpp:61`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bf5464bdc9f2f916800536df3a39591ea0ffff0c/src/zmq/zmqutil.cpp#L61>) (head). **🔵 Suggestion · Hostname sharing keys are not tied to the bound endpoint** `ZMQAddressKey` selects a resolved address, but `zmq_bind` receives the original hostname, leaving socket identity dependent on unverified resolver agreement; no actual endpoint mismatch has been established. Location: [`src/zmq/zmqutil.cpp:53`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bf5464bdc9f2f916800536df3a39591ea0ffff0c/src/zmq/zmqutil.cpp#L53>) (head). **🔵 Suggestion · New ZMQ tests do not check message delivery** The partial-failure and alias tests check logs and RPC metadata, but would not detect a notifier that remains listed while its shared socket fails to deliver its messages. Location: [`test/functional/interface_zmq.py:596`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bf5464bdc9f2f916800536df3a39591ea0ffff0c/test/functional/interface_zmq.py#L596>) (head). **🔵 Suggestion · One commit couples partial initialization with DNS-based sharing** `bf5464bd` combines failure isolation with a separate DNS-based socket-sharing policy, making the fix harder to review, backport, or revert independently. Location: src/zmq/zmqutil.cpp:26 (commit). **🔵 Suggestion · The ZMQ startup behavior change lacks a release note** A failed notifier no longer disables every notification, so operators monitoring `getzmqnotifications` for an empty result need upgrade guidance. Location: [`src/zmq/zmqnotificationinterface.cpp:126`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bf5464bdc9f2f916800536df3a39591ea0ffff0c/src/zmq/zmqnotificationinterface.cpp#L126>) (head). **🔵 Suggestion · `Cannot parse` also reports hostname-resolution failures** A well-formed `tcp://` hostname that cannot resolve is logged as a parse failure, directing operators toward syntax rather than name resolution. Location: [`src/zmq/zmqpublishnotifier.cpp:103`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/bf5464bdc9f2f916800536df3a39591ea0ffff0c/src/zmq/zmqpublishnotifier.cpp#L103>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`bf5464bdc9f2`](<https://git.fish.foo/bitcoin/bitcoin/commit/bf5464bdc9f2f916800536df3a39591ea0ffff0c>) · [Review report](<https://review.fish.foo/report/37060-bf5464bdc9f2f916800536df3a39591ea0ffff0c-3452-1.html>) · [Previous reports](<https://review.fish.foo/traces/37060/>) [permalink](<https://review.fish.foo/report/37060-bf5464bdc9f2f916800536df3a39591ea0ffff0c-3452-1.html>)
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!37060
No description provided.