From cff03195077e6d9a28fd77ea0095a4abc7a9cfa0 Mon Sep 17 00:00:00 2001 From: Harald Ponce de Leon Date: Sat, 21 Nov 2026 09:49:53 +0100 Subject: [PATCH] [Parallel] Pass the command-line paths to the workers in a file, so many paths fit --- src/Configuration/Option.php | 6 ++ src/Console/Command/WorkerCommand.php | 8 ++ .../Application/ParallelFileProcessor.php | 13 ++- .../Command/WorkerCommandLineFactory.php | 11 ++- src/Parallel/ValueObject/WorkerPathsFile.php | 96 +++++++++++++++++++ .../Command/WorkerCommandLineFactoryTest.php | 52 ++++++++-- .../ValueObject/WorkerPathsFileTest.php | 48 ++++++++++ 7 files changed, 218 insertions(+), 16 deletions(-) create mode 100644 src/Parallel/ValueObject/WorkerPathsFile.php create mode 100644 tests/Parallel/ValueObject/WorkerPathsFileTest.php diff --git a/src/Configuration/Option.php b/src/Configuration/Option.php index 65b8e438eb3..0e8141bd21e 100644 --- a/src/Configuration/Option.php +++ b/src/Configuration/Option.php @@ -151,6 +151,12 @@ final class Option public const string PARALLEL_PORT = 'port'; + /** + * @internal The main process passes the command-line paths to its workers in this JSON file, + * as their command line would get too long for the OS with many paths + */ + public const string PARALLEL_PATHS_FILE = 'paths-file'; + /** * @internal Use @see \Rector\Config\RectorConfig::parallel() instead with pass int $jobSize parameter */ diff --git a/src/Console/Command/WorkerCommand.php b/src/Console/Command/WorkerCommand.php index cf5cf822fea..1998fd1d80a 100644 --- a/src/Console/Command/WorkerCommand.php +++ b/src/Console/Command/WorkerCommand.php @@ -15,6 +15,7 @@ use Rector\Configuration\ConfigurationRuleFilter; use Rector\Configuration\Option; use Rector\Console\ProcessConfigureDecorator; +use Rector\FileSystem\JsonFileSystem; use Rector\Parallel\Enum\Action; use Rector\Parallel\Enum\ReactCommand; use Rector\Parallel\Enum\ReactEvent; @@ -26,6 +27,7 @@ use Rector\ValueObject\Error\SystemError; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Throwable; use Webmozart\Assert\Assert; @@ -58,12 +60,18 @@ protected function configure(): void $this->setDescription('[INTERNAL] Support for parallel process'); ProcessConfigureDecorator::decorate($this); + $this->addOption(Option::PARALLEL_PATHS_FILE, null, InputOption::VALUE_REQUIRED); parent::configure(); } protected function execute(InputInterface $input, OutputInterface $output): int { + $pathsFile = $input->getOption(Option::PARALLEL_PATHS_FILE); + if (is_string($pathsFile)) { + $input->setArgument(Option::SOURCE, JsonFileSystem::readFilePath($pathsFile)); + } + $configuration = $this->configurationFactory->createFromInput($input); $this->memoryLimiter->adjust($configuration); $this->configurationRuleFilter->setConfiguration($configuration); diff --git a/src/Parallel/Application/ParallelFileProcessor.php b/src/Parallel/Application/ParallelFileProcessor.php index 1758b6df3c3..0701a8cac92 100644 --- a/src/Parallel/Application/ParallelFileProcessor.php +++ b/src/Parallel/Application/ParallelFileProcessor.php @@ -23,6 +23,7 @@ use Rector\Parallel\ValueObject\ParallelProcess; use Rector\Parallel\ValueObject\ProcessPool; use Rector\Parallel\ValueObject\Schedule; +use Rector\Parallel\ValueObject\WorkerPathsFile; use Rector\ValueObject\Error\SystemError; use Rector\ValueObject\ProcessResult; use Symfony\Component\Console\Command\Command; @@ -148,6 +149,10 @@ public function process( $timeoutInSeconds = SimpleParameterProvider::provideIntParameter(Option::PARALLEL_JOB_TIMEOUT_IN_SECONDS); $fileChunksBudgetPerProcess = []; + /** @var string[] $paths */ + $paths = $input->getArgument(Option::SOURCE); + $workerPathsFile = $paths === [] ? null : WorkerPathsFile::create($paths); + $pathsFile = $workerPathsFile?->getFilePath(); $processSpawner = function (int $bucketKey) use ( $parallelResultCollector, @@ -159,6 +164,7 @@ public function process( $mainScript, $input, $serverPort, + $pathsFile, $streamSelectLoop, $timeoutInSeconds, $handleErrorCallable, @@ -177,6 +183,7 @@ public function process( $input, $processIdentifier, $serverPort, + $pathsFile, ); $parallelProcess = new ParallelProcess($workerCommandLine, $streamSelectLoop, $timeoutInSeconds); @@ -266,7 +273,11 @@ function ($exitCode, string $stdErr) use ($parallelResultCollector, $processIden ($processSpawner)($bucketKey); } - $streamSelectLoop->run(); + try { + $streamSelectLoop->run(); + } finally { + $workerPathsFile?->remove(); + } if ($reachedSystemErrorsCountLimit) { $parallelResultCollector->collectSystemError(new SystemError(sprintf( diff --git a/src/Parallel/Command/WorkerCommandLineFactory.php b/src/Parallel/Command/WorkerCommandLineFactory.php index 99161845e71..e9cb62728d4 100644 --- a/src/Parallel/Command/WorkerCommandLineFactory.php +++ b/src/Parallel/Command/WorkerCommandLineFactory.php @@ -36,7 +36,8 @@ public function create( string $workerCommandName, InputInterface $input, string $identifier, - int $port + int $port, + ?string $pathsFile = null ): string { $commandArguments = array_slice($_SERVER['argv'], 1); @@ -82,10 +83,10 @@ public function create( $workerCommandArray[] = '--identifier'; $workerCommandArray[] = escapeshellarg($identifier); - /** @var string[] $paths */ - $paths = $input->getArgument(Option::SOURCE); - foreach ($paths as $path) { - $workerCommandArray[] = escapeshellarg($path); + // the paths go in a file, as many paths make the command line too long to start the worker + if ($pathsFile !== null) { + $workerCommandArray[] = self::OPTION_DASHES . Option::PARALLEL_PATHS_FILE; + $workerCommandArray[] = escapeshellarg($pathsFile); } // set json output diff --git a/src/Parallel/ValueObject/WorkerPathsFile.php b/src/Parallel/ValueObject/WorkerPathsFile.php new file mode 100644 index 00000000000..df32d2776bb --- /dev/null +++ b/src/Parallel/ValueObject/WorkerPathsFile.php @@ -0,0 +1,96 @@ + + */ + private array $previousSignalHandlers = []; + + private ?bool $previousAsyncSignals = null; + + private function __construct( + private readonly string $filePath + ) { + } + + /** + * @param string[] $paths + */ + public static function create(array $paths): self + { + $filePath = tempnam(sys_get_temp_dir(), 'rector_paths_'); + if ($filePath === false) { + throw new ParallelShouldNotHappenException('Failed creating temp file.'); + } + + FileSystem::write($filePath, Json::encode($paths)); + + $workerPathsFile = new self($filePath); + $workerPathsFile->removeOnInterrupt(); + + return $workerPathsFile; + } + + public function getFilePath(): string + { + return $this->filePath; + } + + public function remove(): void + { + if ($this->isRemoved) { + return; + } + + $this->isRemoved = true; + FileSystem::delete($this->filePath); + + foreach ($this->previousSignalHandlers as $signal => $previousSignalHandler) { + pcntl_signal($signal, $previousSignalHandler); + } + + if ($this->previousAsyncSignals !== null) { + pcntl_async_signals($this->previousAsyncSignals); + } + } + + private function removeOnInterrupt(): void + { + register_shutdown_function($this->remove(...)); + + if (! extension_loaded('pcntl')) { + return; + } + + $signalHandler = function (int $signal): never { + $this->remove(); + + // the conventional exit code of a process ended by a signal + exit(128 + $signal); + }; + + foreach ([SIGINT, SIGTERM] as $signal) { + $this->previousSignalHandlers[$signal] = pcntl_signal_get_handler($signal); + pcntl_signal($signal, $signalHandler); + } + + $this->previousAsyncSignals = pcntl_async_signals(true); + } +} diff --git a/tests/Parallel/Command/WorkerCommandLineFactoryTest.php b/tests/Parallel/Command/WorkerCommandLineFactoryTest.php index 5b7d0f7d6eb..27eaf976b09 100644 --- a/tests/Parallel/Command/WorkerCommandLineFactoryTest.php +++ b/tests/Parallel/Command/WorkerCommandLineFactoryTest.php @@ -73,7 +73,7 @@ public static function provideDataSpacedMainScript(): Iterator self::COMMAND => 'process', Option::SOURCE => ['src'], ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -82,7 +82,7 @@ public static function provideDataSpacedMainScript(): Iterator Option::SOURCE => ['src'], '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -92,7 +92,7 @@ public static function provideDataSpacedMainScript(): Iterator '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, '--' . Option::DEBUG => true, ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -103,7 +103,7 @@ public static function provideDataSpacedMainScript(): Iterator '--' . Option::DEBUG => true, '--' . Option::XDEBUG => true, ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --xdebug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::SPACED_DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --xdebug --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; } @@ -144,7 +144,7 @@ public static function provideData(): Iterator self::COMMAND => 'process', Option::SOURCE => ['src'], ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -153,7 +153,7 @@ public static function provideData(): Iterator Option::SOURCE => ['src'], '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -163,7 +163,7 @@ public static function provideData(): Iterator '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, '--' . Option::DEBUG => true, ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -174,7 +174,7 @@ public static function provideData(): Iterator '--' . Option::DEBUG => true, '--' . Option::XDEBUG => true, ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --xdebug --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --debug --xdebug --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -184,7 +184,7 @@ public static function provideData(): Iterator '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, '--' . Option::MEMORY_LIMIT => '-1', ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --memory-limit='-1' --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --memory-limit='-1' --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; yield [ @@ -194,10 +194,42 @@ public static function provideData(): Iterator '--' . Option::OUTPUT_FORMAT => ConsoleOutputFormatter::NAME, '--' . Option::PHP => true, ], - "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --php --port 2000 --identifier 'identifier' 'src' --output-format 'json' --no-ansi", + "'" . PHP_BINARY . "' " . self::turboExtensionArguments() . "'" . self::DUMMY_MAIN_SCRIPT . "' '" . $cliInputOptionsAsString . "' worker --php --port 2000 --identifier 'identifier' --output-format 'json' --no-ansi", ]; } + public function testPathsGoInPathsFile(): void + { + $inputDefinition = $this->prepareProcessCommandDefinition(); + + // ~260 KiB of paths, more than one command-line argument may hold on Linux (128 KiB) + $paths = []; + for ($index = 0; $index < 1000; ++$index) { + $paths[] = '/project/src/' . str_repeat('a_folder_with_a_long_name/', 9) . 'SomeClass' . $index . '.php'; + } + + $arrayInput = new ArrayInput([ + self::COMMAND => 'process', + Option::SOURCE => $paths, + ], $inputDefinition); + + $workerCommandLine = $this->workerCommandLineFactory->create( + self::DUMMY_MAIN_SCRIPT, + ProcessCommand::class, + 'worker', + $arrayInput, + 'identifier', + 2000, + '/tmp/rector_paths_file' + ); + + $this->assertStringNotContainsString($paths[0], $workerCommandLine); + $this->assertStringContainsString( + '--' . Option::PARALLEL_PATHS_FILE . ' ' . escapeshellarg('/tmp/rector_paths_file'), + $workerCommandLine + ); + } + /** * The worker gets PHPStan Turbo through two -d options, when PHPStan ships a binary for this runtime */ diff --git a/tests/Parallel/ValueObject/WorkerPathsFileTest.php b/tests/Parallel/ValueObject/WorkerPathsFileTest.php new file mode 100644 index 00000000000..bfbe4fdac2d --- /dev/null +++ b/tests/Parallel/ValueObject/WorkerPathsFileTest.php @@ -0,0 +1,48 @@ +getFilePath(); + + $this->assertSame(['src', 'tests/Some File.php'], Json::decode(FileSystem::read($filePath), forceArrays: true)); + + $workerPathsFile->remove(); + $this->assertFileDoesNotExist($filePath); + + // removing again, e.g. on shutdown after the run, is a no-op + $workerPathsFile->remove(); + $this->assertFileDoesNotExist($filePath); + } + + #[RequiresPhpExtension('pcntl')] + public function testRestoresSignalHandlersOnRemove(): void + { + $previousSigintHandler = pcntl_signal_get_handler(SIGINT); + $previousSigtermHandler = pcntl_signal_get_handler(SIGTERM); + $previousAsyncSignals = pcntl_async_signals(); + + $workerPathsFile = WorkerPathsFile::create(['src']); + $this->assertNotSame($previousSigintHandler, pcntl_signal_get_handler(SIGINT)); + $this->assertNotSame($previousSigtermHandler, pcntl_signal_get_handler(SIGTERM)); + $this->assertTrue(pcntl_async_signals()); + + $workerPathsFile->remove(); + + $this->assertSame($previousSigintHandler, pcntl_signal_get_handler(SIGINT)); + $this->assertSame($previousSigtermHandler, pcntl_signal_get_handler(SIGTERM)); + $this->assertSame($previousAsyncSignals, pcntl_async_signals()); + } +}