Skip to content

curl and other native libraries block pcntl async signal propagation #1712

@olsavmic

Description

@olsavmic

Description

The following code:

<?php

pcntl_async_signals(true);

$handler = function() {
	echo 'HANDLER CALLED' . PHP_EOL;
	pcntl_alarm(1);
};

pcntl_signal(SIGALRM, $handler);
pcntl_alarm(1);

while (true) {
    $ch = curl_init();
    //curl_setopt($ch, CURLOPT_NOPROGRESS, false);
    //curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function() {});
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/timeout.php');
    curl_exec($ch);
    curl_close($ch);
}

Gives following output:

Request started
HANDLER CALLED
Request done

But I expected this output instead:

Request started
HANDLER CALLED
HANDLER CALLED
HANDLER CALLED
HANDLER CALLED
HANDLER CALLED
HANDLER CALLED
HANDLER CALLED
HANDLER CALLED
HANDLER CALLED
Request done

PHP main thread is blocked during the entire request, the handler is called only after that. This behaviour renders pcntl_async_signals unusable for most workloads (eg. sending heartbeats to keep connections open during long running operations). The issue is not restricted to SIGALRM only, it applies to all signals.

Such behaviour is not documented anywhere (including the RFC https://wiki.php.net/rfc/async_signals) yet the same problem actually applies to other native libraries like PDO/mysqli,... (see already reported issue for mysqli https://bugs.php.net/bug.php?id=81515)

If this is expected behaviour, it must be documented as right now it seems like the only RELIABLE way to implement specific features but it behaves unpredictably with the use of 3rd party libraries and it's very hard to debug.

Adding both curl_setopt($ch, CURLOPT_NOPROGRESS, false); and curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function() {}); fixes this particular issue but it's an undocumented required configuration and most likely just a side-effect.


Example of PDO query blocking:

<?php

pcntl_async_signals(true);

$handler = function () {
	echo 'Handler called';
	pcntl_alarm(1);
};

pcntl_signal(SIGALRM, $handler);
pcntl_alarm(1);

$conn = new PDO('mysql:host=localhost;dbname=test', 'root', 'admin');
$conn->query("SELECT SLEEP(10)");

PHP Version

8.1, 8.0

Operating System

Debian Buster, MacOS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions