|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Parallel; |
| 4 | + |
| 5 | +use PHPStan\ShouldNotHappenException; |
| 6 | +use React\EventLoop\LoopInterface; |
| 7 | +use React\EventLoop\TimerInterface; |
| 8 | +use React\Socket\TcpServer; |
| 9 | +use Symfony\Component\Console\Output\StreamOutput; |
| 10 | +use Throwable; |
| 11 | +use function fclose; |
| 12 | +use function pcntl_fork; |
| 13 | +use function pcntl_waitpid; |
| 14 | +use function pcntl_wexitstatus; |
| 15 | +use function pcntl_wifexited; |
| 16 | +use function rewind; |
| 17 | +use function stream_get_contents; |
| 18 | +use function tmpfile; |
| 19 | +use const WNOHANG; |
| 20 | + |
| 21 | +/** |
| 22 | + * Parallel worker backed by pcntl_fork(): the worker is forked from the |
| 23 | + * already-booted main process, so it inherits the DI container for free and |
| 24 | + * skips the application re-boot that a {@see SpawnedProcess} pays. |
| 25 | + * |
| 26 | + * The forked child still talks to ParallelAnalyser over the same TCP + NDJSON |
| 27 | + * protocol — only the process-creation mechanism differs. |
| 28 | + */ |
| 29 | +final class ForkedProcess extends ProcessBase |
| 30 | +{ |
| 31 | + |
| 32 | + private const WAITPID_POLL_INTERVAL = 0.01; |
| 33 | + |
| 34 | + /** @var resource|null */ |
| 35 | + private $stdOut = null; |
| 36 | + |
| 37 | + private ?TimerInterface $waitTimer = null; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param string[] $analysedFiles |
| 41 | + */ |
| 42 | + public function __construct( |
| 43 | + LoopInterface $loop, |
| 44 | + float $timeoutSeconds, |
| 45 | + private WorkerRunner $workerRunner, |
| 46 | + private TcpServer $server, |
| 47 | + private int $serverPort, |
| 48 | + private string $identifier, |
| 49 | + private array $analysedFiles, |
| 50 | + private ?string $tmpFile, |
| 51 | + private ?string $insteadOfFile, |
| 52 | + ) |
| 53 | + { |
| 54 | + parent::__construct($loop, $timeoutSeconds); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @param callable(mixed[] $json) : void $onData |
| 59 | + * @param callable(Throwable $exception): void $onError |
| 60 | + * @param callable(?int $exitCode, string $output) : void $onExit |
| 61 | + */ |
| 62 | + public function start(callable $onData, callable $onError, callable $onExit): void |
| 63 | + { |
| 64 | + $this->setCallbacks($onData, $onError); |
| 65 | + |
| 66 | + // Created before the fork so the parent can read what the child wrote. |
| 67 | + $tmpStdOut = tmpfile(); |
| 68 | + if ($tmpStdOut === false) { |
| 69 | + throw new ShouldNotHappenException('Failed creating temp file for stdout.'); |
| 70 | + } |
| 71 | + $this->stdOut = $tmpStdOut; |
| 72 | + |
| 73 | + $pid = pcntl_fork(); |
| 74 | + |
| 75 | + if ($pid === -1) { |
| 76 | + fclose($this->stdOut); |
| 77 | + $this->stdOut = null; |
| 78 | + // Deferred so it runs after ParallelAnalyser has attached this |
| 79 | + // process to the pool — otherwise tryQuitProcess() would no-op. |
| 80 | + $this->loop->futureTick(static function () use ($onExit): void { |
| 81 | + $onExit(null, 'pcntl_fork() failed.'); |
| 82 | + }); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if ($pid === 0) { |
| 87 | + // Child: drop the inherited listening socket immediately, then run |
| 88 | + // the worker on its own fresh event loop and never return. |
| 89 | + $this->server->close(); |
| 90 | + $output = new StreamOutput($this->stdOut); |
| 91 | + $exitCode = $this->workerRunner->run( |
| 92 | + $output, |
| 93 | + $this->analysedFiles, |
| 94 | + $this->serverPort, |
| 95 | + $this->identifier, |
| 96 | + $this->tmpFile, |
| 97 | + $this->insteadOfFile, |
| 98 | + ); |
| 99 | + exit($exitCode); |
| 100 | + } |
| 101 | + |
| 102 | + // Parent: poll for the child to exit and report it through $onExit. |
| 103 | + $this->waitTimer = $this->loop->addPeriodicTimer(self::WAITPID_POLL_INTERVAL, function () use ($pid, $onExit): void { |
| 104 | + $status = 0; |
| 105 | + $result = pcntl_waitpid($pid, $status, WNOHANG); |
| 106 | + if ($result === 0) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + $this->cancelWaitTimer(); |
| 111 | + $this->cancelTimer(); |
| 112 | + |
| 113 | + $exitCode = null; |
| 114 | + if ($result > 0 && pcntl_wifexited($status)) { |
| 115 | + $exitStatus = pcntl_wexitstatus($status); |
| 116 | + if ($exitStatus !== false) { |
| 117 | + $exitCode = $exitStatus; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + $output = ''; |
| 122 | + if ($this->stdOut !== null) { |
| 123 | + rewind($this->stdOut); |
| 124 | + $output = (string) stream_get_contents($this->stdOut); |
| 125 | + fclose($this->stdOut); |
| 126 | + $this->stdOut = null; |
| 127 | + } |
| 128 | + |
| 129 | + $onExit($exitCode, $output); |
| 130 | + }); |
| 131 | + } |
| 132 | + |
| 133 | + public function quit(): void |
| 134 | + { |
| 135 | + // Ending the connection makes the child's event loop drain and the |
| 136 | + // child exit; the waitpid poll timer must keep running until then so |
| 137 | + // the child is actually reaped (otherwise: zombie + hang). |
| 138 | + $this->endConnection(); |
| 139 | + } |
| 140 | + |
| 141 | + private function cancelWaitTimer(): void |
| 142 | + { |
| 143 | + if ($this->waitTimer === null) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + $this->loop->cancelTimer($this->waitTimer); |
| 148 | + $this->waitTimer = null; |
| 149 | + } |
| 150 | + |
| 151 | +} |
0 commit comments