Skip to content

Commit

Permalink
Don't hang when pcntl_fork is disabled (#4039)
Browse files Browse the repository at this point in the history
* Don't hang when pcntl_fork is disabled

Fixes #3951

* fix CS
  • Loading branch information
weirdan authored Aug 22, 2020
1 parent 4dcb718 commit 1f54c42
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Psalm/Internal/Analyzer/ProjectAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@
use function array_map;
use function end;
use Psalm\Internal\Codebase\Taint;
use function ini_get;
use function in_array;

/**
* @internal
Expand Down Expand Up @@ -454,13 +456,27 @@ public function server($address = '127.0.0.1:12345', bool $socket_server_mode =
exit(1);
}
fwrite(STDOUT, "Server listening on $address\n");

$fork_available = true;
if (!extension_loaded('pcntl')) {
fwrite(STDERR, "PCNTL is not available. Only a single connection will be accepted\n");
$fork_available = false;
}

$disabled_functions = array_map('trim', explode(',', ini_get('disable_functions')));
if (in_array('pcntl_fork', $disabled_functions)) {
fwrite(
STDERR,
"pcntl_fork() is disabled by php configuration (disable_functions directive)."
. " Only a single connection will be accepted\n"
);
$fork_available = false;
}

while ($socket = stream_socket_accept($tcpServer, -1)) {
fwrite(STDOUT, "Connection accepted\n");
stream_set_blocking($socket, false);
if (extension_loaded('pcntl')) {
if ($fork_available) {
// If PCNTL is available, fork a child process for the connection
// An exit notification will only terminate the child process
$pid = pcntl_fork();
Expand Down
9 changes: 9 additions & 0 deletions src/Psalm/Internal/Fork/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
use function unserialize;
use function usleep;
use function version_compare;
use function array_map;
use function in_array;

/**
* Adapted with relatively few changes from
Expand Down Expand Up @@ -115,6 +117,13 @@ public function __construct(
exit(1);
}

$disabled_functions = array_map('trim', explode(',', ini_get('disable_functions')));
if (in_array('pcntl_fork', $disabled_functions)) {
echo "pcntl_fork() is disabled by php configuration (disable_functions directive).\n"
. "Please enable it or run Psalm single-threaded with --threads=1 cli switch.\n";
exit(1);
}

if (ini_get('pcre.jit') === '1'
&& \PHP_OS === 'Darwin'
&& version_compare(PHP_VERSION, '7.3.0') >= 0
Expand Down

0 comments on commit 1f54c42

Please sign in to comment.