Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I have been debugging a program which uses signals and threads this weekend.

The program goes somewhat like this:

1. Fork a child which sends a "child ready" signal (USR2) to parent after it has done processing a thing.

2. Start a thread in parent, which does things in background while waiting for the child to be ready.

3. Handle the "child ready" signal in parent which indicates the child is ready to proceed to doing another thing.

4. Repeat this until receiving an INT or TERM signal.


Now this works mostly fine. A behaviour, which I don't understand, starts happening when I add a signal handler for INT and TERM, in addition to the "child ready" signal, which is USR2.


I have simulated the program with a code snippet. It starts a child which is constantly sending TERM and USR2 signal to parent.

(TERM signal normally comes from init process, here it comes from the child)

Parent has handlers for both signals AND has a thread which has handlers for both those signals.

I expected the thread signal handlers to never be called, since I don't call threads->kill anywhere.

Here is the code, it outputs a message every time a signal handler is called:

use 5.20.2; use warnings; use threads; use Time::HiRes qw/ usleep /; $SIG{USR2} = 'IGNORE'; $SIG{TERM} = 'IGNORE'; if (fork() == 0) { sleep 1; my $ppid = getppid(); for (1 .. 1000) { usleep(int(rand(100000))); kill 'USR2', $ppid; kill 'TERM', $ppid; } } $SIG{TERM} = sub { say "TERM"; }; $SIG{USR2} = sub { say "USR2"; }; my $thread = threads->create(sub { $SIG{TERM} = sub { say "THREAD TERM"; }; $SIG{USR2} = sub { say "THREAD USR2"; }; sleep 10; threads->exit(); }); sleep 10; while ($thread->is_running()) { usleep 1000; } while (! $thread->is_joinable()) { usleep 1000; } $thread->join();

I get output like this:

USR2 TERM USR2 TERM THREAD USR2 THREAD TERM

The thread signal handlers are not called if I send only one type of signal from child process.

If I comment out one of kill-commands from child code, so that I send only USR2 or TERM, I don't get the thread signal handler output.

It doesn't make a difference if I change the TERM signal to be USR1

Can someone explain why this happens?


Perl version information:

This is perl 5, version 24, subversion 1 (v5.24.1) built for x86_64-li +nux-gnu-thread-multi (with 85 registered patches, see perl -V for more detail)

Running on Debian Linux Stretch


In reply to Interesting behaviour with signals and threads by shulam

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-26 03:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found