phpsigpipe

PHP how to simulate SIGPIPE?


My environment: Ubuntu 18.04 LTS PHP 7.2.2 ZTS no-debug

I have a big application where sometimes Broken pipe error is happening. I want to handle it but for this I need to simulate this error for development. How can I do that ?

I have tried:

posix_kill(posix_getpid(), SIGPIPE);
while(1) {
    sleep(5);
}

Also:

sudo kill -13 pid

But script keep working.

Expected result:

Thread 1 "php" received signal SIGPIPE, Broken pipe.

and script should get stopped.


Solution

  • signal_example.php:

    pcntl_async_signals(true);
    
    pcntl_signal(SIGPIPE, function (int $signo) {
        echo 'Process ' . posix_getpid() . ' "php" received signal SIGPIPE, Broken pipe.';
        exit;
    });
    
    while (1) {
        sleep(1);
    }
    

    kill -13 990:

    artemev@Vitaly-PC:~/project/example$ php signal_example.php 
    Process 990 "php" received signal SIGPIPE, Broken pipe.