mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-25 23:41:12 +02:00
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:
@@ -374,6 +374,12 @@ void Shutdown(NodeContext& node)
|
|||||||
client->stop();
|
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
|
#ifdef ENABLE_ZMQ
|
||||||
if (g_zmq_notification_interface) {
|
if (g_zmq_notification_interface) {
|
||||||
if (node.validation_signals) node.validation_signals->UnregisterValidationInterface(g_zmq_notification_interface.get());
|
if (node.validation_signals) node.validation_signals->UnregisterValidationInterface(g_zmq_notification_interface.get());
|
||||||
|
@@ -70,6 +70,9 @@ public:
|
|||||||
//! using provided callback. Throws an exception if there was an error.
|
//! using provided callback. Throws an exception if there was an error.
|
||||||
virtual void listenAddress(std::string& address) = 0;
|
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
|
//! Add cleanup callback to remote interface that will run when the
|
||||||
//! interface is deleted.
|
//! interface is deleted.
|
||||||
template<typename Interface>
|
template<typename Interface>
|
||||||
|
@@ -65,9 +65,20 @@ public:
|
|||||||
m_loop.emplace(exe_name, &IpcLogFn, &m_context);
|
m_loop.emplace(exe_name, &IpcLogFn, &m_context);
|
||||||
if (ready_fn) ready_fn();
|
if (ready_fn) ready_fn();
|
||||||
mp::ServeStream<messages::Init>(*m_loop, fd, init);
|
mp::ServeStream<messages::Init>(*m_loop, fd, init);
|
||||||
|
m_parent_connection = &m_loop->m_incoming_connections.back();
|
||||||
m_loop->loop();
|
m_loop->loop();
|
||||||
m_loop.reset();
|
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
|
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));
|
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
|
//! creation, decrements on destruction. The loop thread exits when the
|
||||||
//! refcount reaches 0. Other IPC objects also hold their own EventLoopRef.
|
//! refcount reaches 0. Other IPC objects also hold their own EventLoopRef.
|
||||||
std::optional<mp::EventLoopRef> m_loop_ref;
|
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
|
} // namespace
|
||||||
|
|
||||||
|
@@ -86,6 +86,10 @@ public:
|
|||||||
int fd = m_process->bind(gArgs.GetDataDirNet(), m_exe_name, address);
|
int fd = m_process->bind(gArgs.GetDataDirNet(), m_exe_name, address);
|
||||||
m_protocol->listen(fd, m_exe_name, m_init);
|
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
|
void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) override
|
||||||
{
|
{
|
||||||
m_protocol->addCleanup(type, iface, std::move(cleanup));
|
m_protocol->addCleanup(type, iface, std::move(cleanup));
|
||||||
|
@@ -58,6 +58,9 @@ public:
|
|||||||
//! clients and servers independently.
|
//! clients and servers independently.
|
||||||
virtual void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& ready_fn = {}) = 0;
|
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
|
//! Add cleanup callback to interface that will run when the interface is
|
||||||
//! deleted.
|
//! deleted.
|
||||||
virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;
|
virtual void addCleanup(std::type_index type, void* iface, std::function<void()> cleanup) = 0;
|
||||||
|
Reference in New Issue
Block a user