Skip to content

Commit 9365d0c

Browse files
committed
ipc: Add Ctrl-C handler for spawned subprocesses
This fixes an error reported by Antoine Poinsot <[email protected]> in bitcoin-core/libmultiprocess#123 that does not happen in master, but does happen with bitcoin#10102 applied, where if Ctrl-C is pressed when `bitcoin-node` is started, it is handled by both `bitcoin-node` and `bitcoin-wallet` processes, causing the wallet to shutdown abruptly instead of waiting for the node and shutting down cleanly. This change fixes the problem by having the wallet process print to stdout when it receives a Ctrl-C signal but not otherwise react, letting the node shut everything down cleanly.
1 parent 53b4d22 commit 9365d0c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/ipc/interfaces.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cstdlib>
1818
#include <functional>
1919
#include <memory>
20+
#include <signal.h>
2021
#include <stdexcept>
2122
#include <string.h>
2223
#include <string>
@@ -26,6 +27,27 @@
2627

2728
namespace ipc {
2829
namespace {
30+
#ifndef WIN32
31+
std::string g_ignore_ctrl_c;
32+
33+
void HandleCtrlC(int)
34+
{
35+
write(STDOUT_FILENO, g_ignore_ctrl_c.data(), g_ignore_ctrl_c.size());
36+
}
37+
#endif
38+
39+
void IgnoreCtrlC(std::string message)
40+
{
41+
#ifndef WIN32
42+
g_ignore_ctrl_c = std::move(message);
43+
struct sigaction sa{};
44+
sa.sa_handler = HandleCtrlC;
45+
sigemptyset(&sa.sa_mask);
46+
sa.sa_flags = SA_RESTART;
47+
sigaction(SIGINT, &sa, nullptr);
48+
#endif
49+
}
50+
2951
class IpcImpl : public interfaces::Ipc
3052
{
3153
public:
@@ -53,6 +75,7 @@ class IpcImpl : public interfaces::Ipc
5375
if (!m_process->checkSpawned(argc, argv, fd)) {
5476
return false;
5577
}
78+
IgnoreCtrlC(strprintf("[%s] SIGINT received — waiting for parent to shut down.\n", m_exe_name));
5679
m_protocol->serve(fd, m_exe_name, m_init);
5780
exit_status = EXIT_SUCCESS;
5881
return true;

0 commit comments

Comments
 (0)