Registry Mirror
Skip to content

Conversation

@mutatrum

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.
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
Concept 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)

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
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 957c3e2 to fa1ac8b Compare December 1, 2026 23:07
@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.

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