I'm having difficulty exiting from an application which uses threads. The program has a keepalive subroutine which is designed to restart the inotify thread in the event that it stops running (sometimes the inotifywait system call stops). When issuing a INT signal, instead of the program cleaning up and exiting, it appears that the keepalive thread just restarts the inotify thread. I'm probably going about this the wrong way, so I'll gladly accept any feedback.
Occasionally, I get this:
Signal SIGTERM received, but no signal handler set.
### $q will be used as a queue to store the output
my $q = new Thread::Queue;
### $inotify_pid will be hold the pid of the inotifywait thread.
my $inotify_pid : shared;
### Run inotifywait as a thread.
my $inotify_thread = async { \&inotifywait(), $q, $inotify_pid };
usleep 50;
### Run keepalive as a thread.
my $keepalive_thread = async { \&keepalive() };
usleep 50;
### Install master signal handler to cleanup before we exit.
$SIG{'TERM'} = sub {
debug(qq{Caught signal: INT.\n});
$keepalive_thread->kill('INT');
$keepalive_thread->join();
kill 9, $inotify_pid;
$inotify_thread->join();
exit;
};
process_output();
#################################################################
sub keepalive {
#################################################################
$SIG{'INT'} = sub { threads->exit(); };
while (1) {
### Check to see if inotifywait thread is still running.
### If not, we restart it.
if (!$inotify_thread->is_running()) {
debug(qq{Inotifywait thread not running.\n});
kill 9, $inotify_pid;
$inotify_thread->join();
$inotify_thread = async { \&inotifywait(), $q, $inotify_pi
+d };
}
usleep 50;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.