Skip to content

proxy: allow stopping ListenConnections() via returned Listener handle - #403

Open
mutatrum wants to merge 1 commit into
bitcoin-core:masterfrom
mutatrum:fix/stop-listener-385
Open

mutatrum wants to merge 1 commit into
bitcoin-core:masterfrom
mutatrum:fix/stop-listener-385

Conversation

@mutatrum

@mutatrum mutatrum commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Return std::shared_ptr<Listener> from ListenConnections() with stop() and stopped() methods so callers can stop accepting new connections and close the listening socket on the event loop thread without disconnecting already-accepted clients (#385).

Run accept() promises in a dedicated kj::TaskSet on Listener so stop() can cancel pending accepts immediately, capture weak_ptr<Listener> in _Listen() callbacks to avoid reference cycles, and attach a teardown guard to EventLoop::m_task_set so Listener::stop() runs if the event loop shuts down first.

Add unit tests for stopping an active listener and stopping while at the max_connections limit.

Fixes: #385.
Related: bitcoin/bitcoin#36585.
Follow-up: bitcoin/bitcoin#37009
Work-around in ckpool: https://git.mutatrum.com/mutatrum/ckpool/commit/0d2fe57b311ccfefb5059d8f01c6542c91b4bae1

@DrahtBot

DrahtBot commented Nov 29, 2026 •

Copy link
Copy Markdown

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
Stale ACK ryanofsky

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #390 (proxy: Rethrow EventLoop::sync() callback exceptions in the calling thread by ryanofsky)
  • #231 (Add windows support by ryanofsky)

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.

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review 957c3e2. Concept ACK, and thanks for picking up #385. The approach looks right to me: returning the listener, cancelling the accept by destroying its task set, closing the socket so backlogged clients are dropped, and the guard that calls stop() if the event loop exits first all fit together, and the return type change keeps Bitcoin Core compiling unchanged. The suggestions below are mostly simplifications, plus one test addition.

Thanks also for opening the Bitcoin Core side in bitcoin/bitcoin#37009. This touches the same accept loop as #361, so whichever merges second will need a rebase, and the stop logic should be rechecked then.

Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread test/mp/test/listen_tests.cpp Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
@mutatrum

mutatrum commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Processed all review comments, thank you! There's a lot of finesse in properly handling closing, tricky to get it right.

One addition: stop() calls m_accept_tasks.reset() so it's destroyed on the event loop thread, before stopImpl().

And the immediate "connection refused" handling, so this changeset is not dependent on #384.

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK fa1ac8b. This adds a way to stop accepting connections and close the listening socket without disconnecting existing clients, which master doesn't have, so Bitcoin Core can stop its listeners on shutdown (#385). It also closes the socket immediately after an accept() error instead of leaving it open but unserved.

Thanks for applying all the suggestions so quickly. Since the last review, the error handler and stop() share stopImpl(), stop() also resets m_accept_tasks after an error, and there are comments explaining the ownership and teardown, which I think was the hardest part to follow before.

Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
Comment thread include/mp/proxy-io.h Resolved
@DrahtBot DrahtBot mentioned this pull request Dec 3, 2026
Return std::shared_ptr<Listener> from ListenConnections() with stop() and
stopped() methods so callers can stop accepting new connections and close
the listening socket on the event loop thread without disconnecting
already-accepted clients (bitcoin-core#385).

Run accept() promises in a dedicated kj::TaskSet on Listener so stop()
can cancel pending accepts immediately, capture weak_ptr<Listener> in
_Listen() callbacks to avoid reference cycles, and attach a kj::defer
teardown guard to EventLoop::m_task_set so Listener::stop() runs if the
event loop shuts down first.

Also close the listening socket immediately if accept() fails with an
error, rather than leaving it open without accepting while other
connections remain active (bitcoin-core#384).

Add unit tests for stopping an active listener and stopping while at the
max_connections limit, including verifying that a backlogged client sees
a disconnect.
@mutatrum
mutatrum force-pushed the fix/stop-listener-385 branch from fa1ac8b to 38da0df Compare December 4, 2026 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Listeners can't be stopped, so bitcoin-node can hang after shutdown when an IPC client reconnects

3 participants