Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pcntl_wait not interrupted by SIGTERM

Tags:

php

sigterm

pcntl

According to the PHP docs for pcntl_wait,

The wait function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function.

However, when I run the following code and send SIGTERM to the parent process with kill -s SIGTERM [pid] the signal handler is only called after the child exits (i.e. I have to wait for the sleep to finish. Shouldn't pcntl_wait() be interrupted by SIGTERM?

fork_test.php:

<?php
  declare(ticks = 1);

  function sig_handler($signo) {
    switch ($signo) {
      case SIGTERM:
        echo 'SIGTERM' . PHP_EOL;
        break;
      default:
    }
  }

  pcntl_signal(SIGTERM, 'sig_handler');

  $pid = pcntl_fork();

  if ($pid == -1) {
     die('could not fork');
  }
  else if ($pid) {
    echo 'parent' . PHP_EOL;

    pcntl_wait($status);
  }
  else {
    echo 'child' . PHP_EOL;
    sleep(30);
  }
?>

Output (SIGTERM only appears after waiting 30 seconds):

$ php fork_test.php
child
parent
SIGTERM

PHP Version => 5.3.3

like image 352
ThisSuitIsBlackNot Avatar asked Oct 08 '22 00:10

ThisSuitIsBlackNot


1 Answers

Your call to pcntl_signal specifies that the call should be restarted. Check the docs, restart_syscalls is true by default. So your call to pcntl_signal doesn't return until the child has terminated.

You have no call to flush. So PHP can hold the output of echo in a buffer.

So the behavior you are seeing is precisely the behavior you are requesting. The system call is restarted and the output is buffered.

like image 145
David Schwartz Avatar answered Oct 12 '22 20:10

David Schwartz