From 8ca7049ceac3ce28961aeb7084a6e82e43a8752c Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Thu, 24 Apr 2025 15:02:19 -0400 Subject: [PATCH] ipc: Avoid waiting for clients to disconnect when shutting down This fixes behavior reported by Antoine Poinsot https://github.com/bitcoin/bitcoin/pull/29409#issuecomment-2546088852 where if an IPC client is connected, the node will wait forever for it to disconnect before exiting. --- src/init.cpp | 6 ++++++ src/interfaces/ipc.h | 3 +++ src/ipc/capnp/protocol.cpp | 7 +++++++ src/ipc/interfaces.cpp | 4 ++++ src/ipc/protocol.h | 3 +++ 5 files changed, 23 insertions(+) diff --git a/src/init.cpp b/src/init.cpp index a151c9d0a9e..f949e1fa8fd 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -398,6 +398,12 @@ void Shutdown(NodeContext& node) RemovePidFile(*node.args); + // If any -ipcbind clients are still connected, disconnect them now so they + // do not block shutdown. + if (interfaces::Ipc* ipc = node.init->ipc()) { + ipc->disconnectIncoming(); + } + LogPrintf("%s: done\n", __func__); } diff --git a/src/interfaces/ipc.h b/src/interfaces/ipc.h index fb340552c5c..15e92d1050d 100644 --- a/src/interfaces/ipc.h +++ b/src/interfaces/ipc.h @@ -70,6 +70,9 @@ public: //! using provided callback. Throws an exception if there was an error. virtual void listenAddress(std::string& address) = 0; + //! Disconnect any incoming connections that are still connected. + virtual void disconnectIncoming() = 0; + //! Add cleanup callback to remote interface that will run when the //! interface is deleted. template diff --git a/src/ipc/capnp/protocol.cpp b/src/ipc/capnp/protocol.cpp index 487433d8a38..b8f36a80c79 100644 --- a/src/ipc/capnp/protocol.cpp +++ b/src/ipc/capnp/protocol.cpp @@ -68,6 +68,13 @@ public: m_loop->loop(); m_loop.reset(); } + void disconnectIncoming() override + { + if (!m_loop) return; + m_loop->sync([&] { + m_loop->m_incoming_connections.clear(); + }); + } void addCleanup(std::type_index type, void* iface, std::function cleanup) override { mp::ProxyTypeRegister::types().at(type)(iface).cleanup_fns.emplace_back(std::move(cleanup)); diff --git a/src/ipc/interfaces.cpp b/src/ipc/interfaces.cpp index 33555f05d4c..0c6d1d040a5 100644 --- a/src/ipc/interfaces.cpp +++ b/src/ipc/interfaces.cpp @@ -86,6 +86,10 @@ public: int fd = m_process->bind(gArgs.GetDataDirNet(), m_exe_name, address); m_protocol->listen(fd, m_exe_name, m_init); } + void disconnectIncoming() override + { + m_protocol->disconnectIncoming(); + } void addCleanup(std::type_index type, void* iface, std::function cleanup) override { m_protocol->addCleanup(type, iface, std::move(cleanup)); diff --git a/src/ipc/protocol.h b/src/ipc/protocol.h index cb964d802fb..335ffddc0b1 100644 --- a/src/ipc/protocol.h +++ b/src/ipc/protocol.h @@ -58,6 +58,9 @@ public: //! clients and servers independently. virtual void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function& ready_fn = {}) = 0; + //! Disconnect any incoming connections that are still connected. + virtual void disconnectIncoming() = 0; + //! Add cleanup callback to interface that will run when the interface is //! deleted. virtual void addCleanup(std::type_index type, void* iface, std::function cleanup) = 0;