Skip to contentSkip to conversation

main-loop.c: Handle SIGINT, SIGHUP and SIGTERM synchronously

PATCH In review Peter Maydell 5 participants 6 messages Sep 25, 2014 – Oct 25, 2014

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 main-loop.c: Handle SIGINT, SIGHUP and SIGTERM synchronously

Add the termination signals SIGINT, SIGHUP and SIGTERM to the list of signals which we handle synchronously via a signalfd. This avoids a race condition where if we took the SIGTERM in the middle of qemu_shutdown_requested:

    int r = shutdown_requested;
[SIGTERM here...]
    shutdown_requested = 0;

then the setting of the shutdown_requested flag by termsig_handler() would be lost and QEMU would fail to shut down. This was causing 'make check' to hang occasionally.

---
 main-loop.c | 3 +++
 1 file changed, 3 insertions(+)
main-loop.c +3 -0
index 3cc79f8..8746abc 100644
@@ -84,6 +84,9 @@ static int qemu_signal_init(void)
     sigaddset(&set, SIGIO);
     sigaddset(&set, SIGALRM);
     sigaddset(&set, SIGBUS);
+    sigaddset(&set, SIGINT);
+    sigaddset(&set, SIGHUP);
+    sigaddset(&set, SIGTERM);
     pthread_sigmask(SIG_BLOCK, &set, NULL);
 
     sigdelset(&set, SIG_IPI);
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw

Il 25/09/2014 17:51, Peter Maydell ha scritto:

Quoted 24 lines

Add the termination signals SIGINT, SIGHUP and SIGTERM to the list of signals which we handle synchronously via a signalfd. This avoids a race condition where if we took the SIGTERM in the middle of qemu_shutdown_requested:

    int r = shutdown_requested;
[SIGTERM here...]
    shutdown_requested = 0;

then the setting of the shutdown_requested flag by termsig_handler() would be lost and QEMU would fail to shut down. This was causing 'make check' to hang occasionally.

---
 main-loop.c | 3 +++
 1 file changed, 3 insertions(+)
main-loop.c +0 -0
index 3cc79f8..8746abc 100644

main-loop.c static int qemu_signal_init(void) · line 92

Quoted 9 lines
main-loop.c +3 -0
@@ -84,6 +84,9 @@ static int qemu_signal_init(void)
     sigaddset(&set, SIGIO);
     sigaddset(&set, SIGALRM);
     sigaddset(&set, SIGBUS);
+    sigaddset(&set, SIGINT);
+    sigaddset(&set, SIGHUP);
+    sigaddset(&set, SIGTERM);
     pthread_sigmask(SIG_BLOCK, &set, NULL);
 
     sigdelset(&set, SIG_IPI);

Paolo

Reviewed-by Tested-by on v1 1/1
#2 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

Add the termination signals SIGINT, SIGHUP and SIGTERM to the list of signals which we handle synchronously via a signalfd. This avoids a race condition where if we took the SIGTERM in the middle of qemu_shutdown_requested:

    int r = shutdown_requested;
[SIGTERM here...]
    shutdown_requested = 0;
Quoted 6 lines

then the setting of the shutdown_requested flag by termsig_handler() would be lost and QEMU would fail to shut down. This was causing 'make check' to hang occasionally.

<snip>

I've been testing it with my latest Travis patches (- the make check once patch) and it seems a lot better now:

Signature · 1 line
Alex Bennée
#3 lore raw
Quoted 17 lines · Alex Bennée
Quoted 8 lines · Peter Maydell

Add the termination signals SIGINT, SIGHUP and SIGTERM to the list of signals which we handle synchronously via a signalfd. This avoids a race condition where if we took the SIGTERM in the middle of qemu_shutdown_requested:

    int r = shutdown_requested;
[SIGTERM here...]
    shutdown_requested = 0;
Quoted 6 lines

then the setting of the shutdown_requested flag by termsig_handler() would be lost and QEMU would fail to shut down. This was causing 'make check' to hang occasionally.

Quoted 7 lines

<snip>

I've been testing it with my latest Travis patches (- the make check once patch) and it seems a lot better now:

Thanks; applied to master.

-- PMM

#4 lore raw
In reply to Peter Maydell

Hi,

Quoted 11 lines

Subject: [Qemu-devel] [PATCH] main-loop.c: Handle SIGINT, SIGHUP and SIGTERM synchronously

Add the termination signals SIGINT, SIGHUP and SIGTERM to the list of signals which we handle synchronously via a signalfd. This avoids a race condition where if we took the SIGTERM in the middle of qemu_shutdown_requested:

    int r = shutdown_requested;
[SIGTERM here...]
    shutdown_requested = 0;
Quoted 5 lines

then the setting of the shutdown_requested flag by termsig_handler() would be lost and QEMU would fail to shut down. This was causing 'make check' to hang occasionally.

I met a really upset thing after this patch, which I can't using gdb debug Qemu process well. I use gdb attaching a Qemu process, and press 'c' to continue executing. When I want to set a breakpoint for debugging, then press 'ctrl + c', the Qemu will exit, because the SIGINT signal is captured by Qemu now. :(

What's your opinion? Thanks.

Best regards,
-Gonglei

Quoted 22 lines
---
 main-loop.c | 3 +++
 1 file changed, 3 insertions(+)
main-loop.c +3 -1
index 3cc79f8..8746abc 100644
@@ -84,6 +84,9 @@ static int qemu_signal_init(void)
     sigaddset(&set, SIGIO);
     sigaddset(&set, SIGALRM);
     sigaddset(&set, SIGBUS);
+    sigaddset(&set, SIGINT);
+    sigaddset(&set, SIGHUP);
+    sigaddset(&set, SIGTERM);
     pthread_sigmask(SIG_BLOCK, &set, NULL);
 
     sigdelset(&set, SIG_IPI);
--

1.9.1

#5 lore raw
Quoted 17 lines · Gonglei

Hi,

Quoted 15 lines

Subject: [Qemu-devel] [PATCH] main-loop.c: Handle SIGINT, SIGHUP and SIGTERM synchronously

Add the termination signals SIGINT, SIGHUP and SIGTERM to the list of signals which we handle synchronously via a signalfd. This avoids a race condition where if we took the SIGTERM in the middle of qemu_shutdown_requested:

    int r = shutdown_requested;
[SIGTERM here...]
    shutdown_requested = 0;

then the setting of the shutdown_requested flag by termsig_handler() would be lost and QEMU would fail to shut down. This was causing 'make check' to hang occasionally.

Quoted 8 lines
Quoted 1 line

I met a really upset thing after this patch, which I can't using gdb debug Qemu process well. I use gdb attaching a Qemu process, and press 'c' to continue executing. When I want to set a breakpoint for debugging, then press 'ctrl + c', the Qemu will exit, because the SIGINT signal is captured by Qemu now. :(

What's your opinion? Thanks.

Confirmed, you cannot interrupt a running qemu from gdb anymore.

I think this patch has to be reverted and the original issue fixed by making qemu_shutdown_requested signal-safe. Should be simple, just convert shutdown_requested into a counter. Need not be atomic, all happens over the main thread anyway.

Jan