Skip to content

Commit 2160995

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/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 0c28068 commit 2160995

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/ipc/interfaces.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <tinyformat.h>
1414
#include <util/fs.h>
1515

16+
#include <csignal>
1617
#include <cstdio>
1718
#include <cstdlib>
1819
#include <cstring>
@@ -26,6 +27,28 @@
2627

2728
namespace ipc {
2829
namespace {
30+
#ifndef WIN32
31+
std::string g_ignore_ctrl_c;
32+
33+
void HandleCtrlC(int)
34+
{
35+
// (void)! needed to suppress -Wunused-result warning from GCC
36+
(void)!write(STDOUT_FILENO, g_ignore_ctrl_c.data(), g_ignore_ctrl_c.size());
37+
}
38+
#endif
39+
40+
void IgnoreCtrlC(std::string message)
41+
{
42+
#ifndef WIN32
43+
g_ignore_ctrl_c = std::move(message);
44+
struct sigaction sa{};
45+
sa.sa_handler = HandleCtrlC;
46+
sigemptyset(&sa.sa_mask);
47+
sa.sa_flags = SA_RESTART;
48+
sigaction(SIGINT, &sa, nullptr);
49+
#endif
50+
}
51+
2952
class IpcImpl : public interfaces::Ipc
3053
{
3154
public:
@@ -53,6 +76,7 @@ class IpcImpl : public interfaces::Ipc
5376
if (!m_process->checkSpawned(argc, argv, fd)) {
5477
return false;
5578
}
79+
IgnoreCtrlC(strprintf("[%s] SIGINT received — waiting for parent to shut down.\n", m_exe_name));
5680
m_protocol->serve(fd, m_exe_name, m_init);
5781
exit_status = EXIT_SUCCESS;
5882
return true;

0 commit comments

Comments
 (0)