]> git.ktnx.net Git - mpd-feeder.git/commitdiff
a second HIP/INT signal causes a hard exit()
authorDamyan Ivanov <dmn@debian.org>
Fri, 12 Nov 2021 09:03:56 +0000 (09:03 +0000)
committerDamyan Ivanov <dmn@debian.org>
Fri, 12 Nov 2021 09:03:56 +0000 (09:03 +0000)
lib/App/MPD/Feeder.pm

index 1b5ed30e5bb36f82a8a39c33b664c8f2838d2ae4..9cb1a2543f901751839e563a15ba0802f50dde8c 100644 (file)
@@ -3,6 +3,7 @@ package App::MPD::Feeder;
 use strict;
 use warnings;
 use utf8;
+use feature 'state';
 
 use App::MPD::Feeder::Options;
 use App::MPD::Feeder::DB;
@@ -60,31 +61,33 @@ use constant DEFAULT_CONFIG_FILE => '/etc/mpd-feeder/mpd-feeder.conf';
 
         $mpd = Net::Async::MPD->new(%conn);
 
-        $mpd->loop->add(
-            IO::Async::Signal->new(
-                name       => 'TERM',
-                on_receipt => sub {
-                    $log->debug("SIGTERM received. Stopping loop");
-                    $mpd->loop->stop('quit');
-                },
-            )
-        );
+        my $int_signal_handler = sub {
+            state $signal_count = 0;
+            $signal_count++;
+            $log->debug("Signal received. Stopping loop");
+            $mpd->loop->stop('quit');
 
-        $mpd->loop->add(
-            IO::Async::Signal->new(
-                name       => 'INT',
-                on_receipt => sub {
-                    $log->debug("SIGINT received. Stopping loop");
-                    $mpd->loop->stop('quit');
-                },
-            )
-        );
+            if ( $signal_count > 1 ) {
+                $log->warn("Another signal received (#$signal_count)");
+                $log->warn("Exiting abruptly");
+                exit 2;
+            }
+        };
+
+        for (qw(TERM INT)) {
+            $mpd->loop->add(
+                IO::Async::Signal->new(
+                    name       => $_,
+                    on_receipt => $int_signal_handler,
+                )
+            );
+        }
 
         $mpd->loop->add(
             IO::Async::Signal->new(
                 name       => 'HUP',
                 on_receipt => sub {
-                    $log->debug("SIGHUP received. Stopping loop");
+                    $log->debug("SIGHUP received. Scheduling reload");
                     $mpd->loop->stop('reload');
                 },
             )