mhearse has asked for the wisdom of the Perl Monks concerning the following question:

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; } }

Replies are listed 'Best First'.
Re: Thread signal handlers: keepalive issue
by zentara (Cardinal) on Jul 19, 2008 at 11:11 UTC