walkor / workerman

An asynchronous event driven PHP socket framework. Supports HTTP, Websocket, SSL and other custom protocols.
http://www.workerman.net
MIT License
11.03k stars 2.25k forks source link

Fixed issue when child process has signal handlers installed, they where ignored. #991

Closed akotulu closed 4 months ago

akotulu commented 7 months ago

Fixed issue with child process having custom handlers. When signal is called with child process PID, it cancels the sleep but handlers wont be called. Improved default select with stream_select error check.

Simple test, commenting pcntl_async_signals will result sleep disrupted but handler wont be called. Same is with Workerman, it waits for stream_select and when signal is called it breaks the select, but handlers wont be called.

<?php
pcntl_async_signals(true);

function sig_handler(int $signal, mixed $siginfo): void
{
    var_dump($siginfo);
    echo 'Yolo';
    fwrite(STDOUT, "Received singal $signal". PHP_EOL);
    fwrite(STDERR, "Received singal $signal". PHP_EOL);
    fflush(STDOUT);
    fflush(STDERR);
}
// pm2 sends SIGINT as quit signal
pcntl_signal(SIGINT, 'sig_handler');
pcntl_signal(SIGTERM, 'sig_handler');
pcntl_signal(SIGUSR1, 'sig_handler');

while (true) {
    echo 'sleeping 100'. PHP_EOL;
    sleep(100);
    echo 'woke up'. PHP_EOL;
    posix_kill(posix_getpid(), SIGUSR1);
}

In CLI run command kill -s 15 PID

akotulu commented 7 months ago

Oh, sorry about the \t. Forgot to remove them.