16

I am working on an application that has periodically-called background processes. One of these was being called by cron, but I am looking for something more robust, so am converting it to run under Supervisor. (It will probably run for 10 minutes, during which time it can detect work to do, or idle. Once it exits, Supervisor will automatically respawn a clean instance.)

Since Supervisor is better at ensuring that only a specified number of instances of something are running in parallel, I can get away with running them longer. This does mean however that my processes are more likely to receive termination signals, either from kill directly, or because they have been stopped via Supervisor. I am therefore experimenting with how to handle this in PHP.

It looks like the basic solution is to use pcntl_signal() like so:

declare(ticks = 1);
pcntl_signal(SIGTERM, 'signalHandler');
pcntl_signal(SIGINT, 'signalHandler');

function signalHandler($signal) {
    switch($signal) {
        case SIGTERM:
        case SIGINT:
            echo "Exiting now...\n";
            exit();
    }
}

However, I have several points in my code that could do with careful shutdown handling. One approach is to make one handler call those various things, but it would require a bit of refactoring, which I would like to avoid. The alternative is to add pcntl_signal() everywhere I need it, but unfortunately it seems only one handler can be installed at once.

However, it looks like I might be able to use register_shutdown_function(). This does not trap ^C or other termination sigs on its own, and the manual is quite clear on this point:

Shutdown functions will not be executed if the process is killed with a SIGTERM or SIGKILL signal

What is surprising is that I have found that if I employ pcntl_signal() to just do an exit, then the shutdown handlers are indeed called. Furthermore, since one can have many shutdown handlers, this solves my problem nicely - each class in my code that wishes to handle termination gracefully can capture and manage its own shutdown.

My initial question is, why does this work? I have tried registering a shutdown function without a signal handler, and this does not seem to be called, as the manual says. I guess the process is kept alive by PHP in order to handle the signal, which causes shutdown handlers to be called?

Also, can I rely on this behaviour, when the manual casts doubt on it? I am using PHP 5.5, and am not looking to upgrade to PHP7 just yet. So I'd be interested in whether this works on 5.5 and 5.6, on a variety of distributions.

Of course, whether it would (or would not) work on 7.0 and/or 7.1, would be interesting too - I am aware though that ticks will be handled differently in 7.1, so there is a greater chance of this having a different behaviour.

6
  • I just wonder why you actually need to respawn the PHP processes at all (given that you do not leak memory randomly ;-))?
    – bwoebi
    Aug 17, 2016 at 9:26
  • Just good practice really, @bwoebi - it gives me confidence there's always a fresh instance around, even if PHP and my system are completely leak-free :-).
    – halfer
    Aug 17, 2016 at 12:34
  • I disagree that it's really good practice. For your use case it's maybe fine, but as soon as you need a permanently reachable socket [TCP/unix/...] (e.g. to dump info from live program), it's bad when it's intermittently unreachable.
    – bwoebi
    Aug 17, 2016 at 12:37
  • Oh yes, it isn't listening to anything. It's a web scraper, so the connection is going out, rather than coming in.
    – halfer
    Aug 17, 2016 at 12:47
  • @BenjaminBasmaci: I've accepted your edit, with a minor modification. However, please allow me to discourage you from making retaliatory edits. There's two reasons for that: (a) your edit work still need to go through the Review Queue, so you could potentially create a large number of smaller edits, which could slow down the approval of more important edits; (b) retaliation is not an ideal motivation for editing - it needs to be exclusively about maintaining quality, technical writing.
    – halfer
    Aug 27, 2020 at 21:09

2 Answers 2

8

PHP user shutdown functions are called during ordinary process termination, somewhat analogous to functions registered with atexit in other programming languages. An explicit exit or implicit exit at end-of-script is an ordinary process termination.

Signal death, however, is abnormal process termination. The default behavior for SIGTERM (and the mandatory behavior for SIGKILL) is to terminate the running process immediately, without running any cleanup handlers.

When you intercept a SIGTERM with pcntl_signal, you are installing different behavior and giving yourself the opportunity to experience ordinary process termination.

Yes, you can rely on this behavior. While the main manual entry is not explicit, the rest of the "Note" you quote reads:

[Y]ou can use pcntl_signal() to install a handler for a SIGTERM which uses exit() to end cleanly.

1
  • Alright, thanks for this. Yes, I saw that note, but I read it differently - "Shutdown functions will not be executed if the process is killed" is fairly explicit, but maybe is just poor wording. The later part of the note seemed to imply that one should use a signal handler instead, rather than in addition to, a shutdown handler. Your interpretation would lead me to believe this behaviour can be relied upon in PHP 7 as well - does that sound correct to you?
    – halfer
    Aug 17, 2016 at 7:13
4
+250

In addition to pilcrows correct answer:

The signal handler is necessary for changing the behavior of the signal (i.e. instead of the default action). Whatever happens after that is irrelevant.

You might use the signal handler to just do emergency cleanup and then call posix_kill(getmypid(), SIGKILL); to force termination without cleanup. If you exit() here, normally the shutdown sequence (i.e. calling of all destructors, shutdown handlers etc.) is initiated.

When you say in addition to, you're not strictly correct. You need to use the signal handler instead and may additionally have a shutdown handler. [referring to your comment]

Be also aware that calling exit() after the shutdown sequence has been initiated will abort the current part of it (i.e. when you're currently inside a shutdown handler and exit() gets called, all subsequent shutdown handlers are ignored - but e.g. destructors will still be called). Ideally you have some check against this:

register_shutdown_function(function() { $GLOBALS["#__in_shutdown"] = 1; });

and have a check around exit():

if (empty($GLOBALS["#__in_shutdown"])) {
    exit;
}

Just in case you want to be really safe unless someone fires SIGKILL on it.

Note that 7.0 or 7.1 won't change this behavior; all what changes in 7.1 is declare(ticks=1); being superfluous, the exhibited behavior is still the same.

3
  • Alright thanks. In relation to the "addition" remark, I probably phrased it badly - I meant that both are necessary if one explicitly wants to use shutdown handlers in order to get several callbacks in a sigterm/sigint situation.
    – halfer
    Aug 17, 2016 at 11:21
  • In order to cleanly close down each part of my system, I would use a dummy sig handler as per the original post, in the root script, and then a non-zero number of shutdown handlers set up in various classes, and none of the shutdown handlers would themselves call exit(), leaving it to the sig handler. Does that sound like a good approach?
    – halfer
    Aug 17, 2016 at 11:44
  • I meant adding an if() around the exit() in the signal handler, so that receiving a SIGTERM while you're already in the shutdown sequence doesn't abort it. For the rest, yes, sounds fine.
    – bwoebi
    Aug 17, 2016 at 12:33

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.