ipc: Avoid waiting for clients to disconnect when shutting down

This fixes behavior reported by Antoine Poinsot <darosior@protonmail.com>
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.
This commit is contained in:
Ryan Ofsky
2025-04-24 15:02:19 -04:00
parent 6eb09fd614
commit 7f65aac78b
5 changed files with 29 additions and 0 deletions

View File

@@ -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());

View File

@@ -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<typename Interface>

View File

@@ -65,9 +65,20 @@ public:
m_loop.emplace(exe_name, &IpcLogFn, &m_context);
if (ready_fn) ready_fn();
mp::ServeStream<messages::Init>(*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<void()> 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<mp::EventLoopRef> m_loop_ref;
//! Connection to parent, if this is a child process spawned by a parent process.
mp::Connection* m_parent_connection{nullptr};
};
} // namespace

View File

@@ -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<void()> cleanup) override
{
m_protocol->addCleanup(type, iface, std::move(cleanup));

View File

@@ -58,6 +58,9 @@ public:
//! clients and servers independently.
virtual void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& 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<void()> cleanup) = 0;