diff --git a/src/init.cpp b/src/init.cpp index 1f72c22ec2f..cae52675525 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -374,6 +374,12 @@ void Shutdown(NodeContext& node) client->stop(); } + // 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(); + } + #ifdef ENABLE_ZMQ if (g_zmq_notification_interface) { if (node.validation_signals) node.validation_signals->UnregisterValidationInterface(g_zmq_notification_interface.get()); 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 7bc653d25ce..4150f9f4664 100644 --- a/src/ipc/capnp/protocol.cpp +++ b/src/ipc/capnp/protocol.cpp @@ -65,9 +65,20 @@ public: m_loop.emplace(exe_name, &IpcLogFn, &m_context); if (ready_fn) ready_fn(); mp::ServeStream(*m_loop, fd, init); + m_parent_connection = &m_loop->m_incoming_connections.back(); m_loop->loop(); m_loop.reset(); } + void disconnectIncoming() override + { + if (!m_loop) return; + // Delete incoming connections, except the connection to a parent + // process (if there is one), since a parent process should be able to + // monitor and control this process, even during shutdown. + m_loop->sync([&] { + m_loop->m_incoming_connections.remove_if([this](mp::Connection& c) { return &c != m_parent_connection; }); + }); + } 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)); @@ -95,6 +106,8 @@ public: //! creation, decrements on destruction. The loop thread exits when the //! refcount reaches 0. Other IPC objects also hold their own EventLoopRef. std::optional m_loop_ref; + //! Connection to parent, if this is a child process spawned by a parent process. + mp::Connection* m_parent_connection{nullptr}; }; } // namespace diff --git a/src/ipc/interfaces.cpp b/src/ipc/interfaces.cpp index 1d9c3992607..50cef794aa5 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;