Skip to content

Commit 7f65aac

Browse files
committed
ipc: Avoid waiting for clients to disconnect when shutting down
This fixes behavior reported by Antoine Poinsot <[email protected]> bitcoin/bitcoin#29409 (comment) where if an IPC client is connected, the node will wait forever for it to disconnect before exiting.
1 parent 6eb09fd commit 7f65aac

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

src/init.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ void Shutdown(NodeContext& node)
374374
client->stop();
375375
}
376376

377+
// If any -ipcbind clients are still connected, disconnect them now so they
378+
// do not block shutdown.
379+
if (interfaces::Ipc* ipc = node.init->ipc()) {
380+
ipc->disconnectIncoming();
381+
}
382+
377383
#ifdef ENABLE_ZMQ
378384
if (g_zmq_notification_interface) {
379385
if (node.validation_signals) node.validation_signals->UnregisterValidationInterface(g_zmq_notification_interface.get());

src/interfaces/ipc.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class Ipc
7070
//! using provided callback. Throws an exception if there was an error.
7171
virtual void listenAddress(std::string& address) = 0;
7272

73+
//! Disconnect any incoming connections that are still connected.
74+
virtual void disconnectIncoming() = 0;
75+
7376
//! Add cleanup callback to remote interface that will run when the
7477
//! interface is deleted.
7578
template<typename Interface>

src/ipc/capnp/protocol.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,20 @@ class CapnpProtocol : public Protocol
6565
m_loop.emplace(exe_name, &IpcLogFn, &m_context);
6666
if (ready_fn) ready_fn();
6767
mp::ServeStream<messages::Init>(*m_loop, fd, init);
68+
m_parent_connection = &m_loop->m_incoming_connections.back();
6869
m_loop->loop();
6970
m_loop.reset();
7071
}
72+
void disconnectIncoming() override
73+
{
74+
if (!m_loop) return;
75+
// Delete incoming connections, except the connection to a parent
76+
// process (if there is one), since a parent process should be able to
77+
// monitor and control this process, even during shutdown.
78+
m_loop->sync([&] {
79+
m_loop->m_incoming_connections.remove_if([this](mp::Connection& c) { return &c != m_parent_connection; });
80+
});
81+
}
7182
void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
7283
{
7384
mp::ProxyTypeRegister::types().at(type)(iface).cleanup_fns.emplace_back(std::move(cleanup));
@@ -95,6 +106,8 @@ class CapnpProtocol : public Protocol
95106
//! creation, decrements on destruction. The loop thread exits when the
96107
//! refcount reaches 0. Other IPC objects also hold their own EventLoopRef.
97108
std::optional<mp::EventLoopRef> m_loop_ref;
109+
//! Connection to parent, if this is a child process spawned by a parent process.
110+
mp::Connection* m_parent_connection{nullptr};
98111
};
99112
} // namespace
100113

src/ipc/interfaces.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ class IpcImpl : public interfaces::Ipc
8686
int fd = m_process->bind(gArgs.GetDataDirNet(), m_exe_name, address);
8787
m_protocol->listen(fd, m_exe_name, m_init);
8888
}
89+
void disconnectIncoming() override
90+
{
91+
m_protocol->disconnectIncoming();
92+
}
8993
void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
9094
{
9195
m_protocol->addCleanup(type, iface, std::move(cleanup));

src/ipc/protocol.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class Protocol
5858
//! clients and servers independently.
5959
virtual void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& ready_fn = {}) = 0;
6060

61+
//! Disconnect any incoming connections that are still connected.
62+
virtual void disconnectIncoming() = 0;
63+
6164
//! Add cleanup callback to interface that will run when the interface is
6265
//! deleted.
6366
virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;

0 commit comments

Comments
 (0)