Skip to content

Commit 55d9d9b

Browse files
ondrejmirtesclaude
andcommitted
Run bootstrapFiles per forked worker instead of inheriting the parent's
A bootstrap file that opens a connection - private-packagist's phpstan-dba bootstrap boots the Symfony kernel and pins the entity manager's PDO, phpstan-doctrine's objectManagerLoader connects the same way - used to run once in the parent, and every forked worker inherited the one open socket. Workers then raced the postgres protocol on it (phpstan-dba executes the analysed SQL inside transactions), crashing the analysis with 'Failed to start transaction' and driver exceptions surfacing string SQLSTATE codes. BootstrapFilesRunner executes the files at most once per process. When the analysis will fork (ForkParallelChecker), CommandHelper::begin() defers them: each forked worker runs them right after the fork (WorkerRunner) - the same per-process semantics a spawned worker always had from its own boot - and the parent runs them once the analysis phase is over and no more forks can happen (AnalyseApplication), before the phases that may reflect analysed code (stub validation, collector rules). A single-threaded run (--debug, missing proc_open()) runs them before analysing in-process; spawned workers and every other command keep the eager begin()-time execution. Autoloaders the files register merge into the source-locator globals at the deferred point - the locators read them lazily. A bootstrap failure in a forked child exits the worker with the error already printed to its collected stdout instead of unwinding past the fork point. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GnwgpaeUXRkgSDyg95tfK8
1 parent bbd64b4 commit 55d9d9b

10 files changed

Lines changed: 203 additions & 51 deletions

‎build/spl-autoload-functions-php-8.neon‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ parameters:
22
ignoreErrors:
33
-
44
message: "#^PHPDoc tag @var with type list\\<callable\\(string\\): void\\>\\|false is not subtype of native type list\\<callable\\(string\\): void\\>\\.$#"
5-
count: 2
5+
count: 1
66
path: ../src/Command/CommandHelper.php
7+
-
8+
message: "#^PHPDoc tag @var with type list\\<callable\\(string\\): void\\>\\|false is not subtype of native type list\\<callable\\(string\\): void\\>\\.$#"
9+
count: 2
10+
path: ../src/Command/BootstrapFilesRunner.php

‎src/Command/AnalyseApplication.php‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ final class AnalyseApplication
3737
{
3838

3939
public function __construct(
40+
private BootstrapFilesRunner $bootstrapFilesRunner,
4041
private AnalyserRunner $analyserRunner,
4142
private AnalyserResultFinalizer $analyserResultFinalizer,
4243
private StubValidator $stubValidator,
@@ -50,6 +51,7 @@ public function __construct(
5051
/**
5152
* @param string[] $files
5253
* @param mixed[]|null $projectConfigArray
54+
* @throws InceptionNotSuccessfulException
5355
*/
5456
public function analyse(
5557
array $files,
@@ -214,6 +216,7 @@ private function mapCollectedData(array $collectedData): array
214216
/**
215217
* @param string[] $files
216218
* @param string[] $allAnalysedFiles
219+
* @throws InceptionNotSuccessfulException
217220
*/
218221
private function runAnalyser(
219222
array $files,
@@ -230,6 +233,11 @@ private function runAnalyser(
230233
$filesCount = count($files);
231234
$allAnalysedFilesCount = count($allAnalysedFiles);
232235
if ($filesCount === 0) {
236+
// nothing to analyse, but the deferred bootstrapFiles still must
237+
// run in the main thread: the phases that follow may reflect
238+
// analysed code (stub validation, collector rules from the result
239+
// cache)
240+
$this->bootstrapFilesRunner->run($errorOutput, $debug);
233241
$errorOutput->getStyle()->progressStart($allAnalysedFilesCount);
234242
$errorOutput->getStyle()->progressAdvance($allAnalysedFilesCount);
235243
$errorOutput->getStyle()->progressFinish();
@@ -296,7 +304,7 @@ private function runAnalyser(
296304
}
297305
}
298306

299-
$analyserResult = $this->analyserRunner->runAnalyser($files, $allAnalysedFiles, $preFileCallback, $postFileCallback, $debug, true, $projectConfigFile, $tmpFile, $insteadOfFile, $input);
307+
$analyserResult = $this->analyserRunner->runAnalyser($files, $allAnalysedFiles, $preFileCallback, $postFileCallback, $debug, true, $projectConfigFile, $tmpFile, $insteadOfFile, $input, $errorOutput);
300308

301309
if (!$debug) {
302310
$errorOutput->getStyle()->progressFinish();

‎src/Command/AnalyseCommand.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
201201
$tmpFile,
202202
$insteadOfFile,
203203
true,
204+
deferBootstrapFiles: true,
204205
);
205206
} catch (InceptionNotSuccessfulException) {
206207
return 1;

‎src/Command/AnalyserRunner.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public function __construct(
3030
private Analyser $analyser,
3131
private ParallelAnalyser $parallelAnalyser,
3232
private CpuCoreCounter $cpuCoreCounter,
33+
private BootstrapFilesRunner $bootstrapFilesRunner,
3334
)
3435
{
3536
}
@@ -39,6 +40,7 @@ public function __construct(
3940
* @param string[] $allAnalysedFiles
4041
* @param Closure(string $file): void|null $preFileCallback
4142
* @param Closure(int, list<string>=): void|null $postFileCallback
43+
* @throws InceptionNotSuccessfulException
4244
*/
4345
public function runAnalyser(
4446
array $files,
@@ -51,6 +53,7 @@ public function runAnalyser(
5153
?string $tmpFile,
5254
?string $insteadOfFile,
5355
InputInterface $input,
56+
Output $errorOutput,
5457
): AnalyserResult
5558
{
5659
$filesCount = count($files);
@@ -93,10 +96,22 @@ public function runAnalyser(
9396
if ($result === null) {
9497
throw new ShouldNotHappenException();
9598
}
99+
// the parallel analysis is over and no more workers fork - the
100+
// main thread runs the deferred bootstrapFiles now, before the
101+
// phases that may reflect analysed code (stub validation,
102+
// collector rules)
103+
$this->bootstrapFilesRunner->run($errorOutput, $debug);
104+
96105
return $result;
97106
}
98107
}
99108

109+
// every path below analyses in-process - including the fall-throughs
110+
// from the parallel branch above (no main script, zero-process
111+
// schedule) - so the main thread runs the deferred bootstrapFiles
112+
// first
113+
$this->bootstrapFilesRunner->run($errorOutput, $debug);
114+
100115
return $this->analyser->analyse(
101116
$this->switchTmpFile($files, $insteadOfFile, $tmpFile),
102117
$preFileCallback,
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Command;
4+
5+
use PHPStan\DependencyInjection\AutowiredService;
6+
use PHPStan\DependencyInjection\Container;
7+
use function array_merge;
8+
use function PHPStan\collectNewAutoloadFunctions;
9+
use function spl_autoload_functions;
10+
11+
/**
12+
* Executes the configured bootstrapFiles, once per process.
13+
*
14+
* The analyse flow calls run() at exactly the places that need the files
15+
* executed - resources they open (an objectManagerLoader's database
16+
* connection, a query reflector's PDO) must not be inherited by forked
17+
* children:
18+
*
19+
* - the main thread right before an in-process analysis (AnalyserRunner),
20+
* - every worker, spawned or forked (WorkerRunner),
21+
* - the main thread after the workers of a parallel analysis
22+
* (AnalyserRunner), and after a fully cached run (AnalyseApplication) -
23+
* before the phases that may reflect analysed code.
24+
*
25+
* Every other command runs them eagerly in CommandHelper::begin(). The
26+
* once-per-process latch exists for a worker forked from a parent that
27+
* already ran the files (the fixer flow runs them eagerly before its
28+
* repeated analysis rounds): such a worker inherits that execution and
29+
* run() must not repeat it.
30+
*/
31+
#[AutowiredService]
32+
final class BootstrapFilesRunner
33+
{
34+
35+
private bool $hasRun = false;
36+
37+
public function __construct(private Container $container)
38+
{
39+
}
40+
41+
/**
42+
* @throws InceptionNotSuccessfulException
43+
*/
44+
public function run(Output $errorOutput, bool $debugEnabled): void
45+
{
46+
if ($this->hasRun) {
47+
return;
48+
}
49+
$this->hasRun = true;
50+
51+
/** @var list<callable(string): void>|false $autoloadFunctionsBefore */
52+
$autoloadFunctionsBefore = spl_autoload_functions();
53+
54+
foreach ($this->container->getParameter('bootstrapFiles') as $bootstrapFile) {
55+
CommandHelper::executeBootstrapFile($bootstrapFile, $this->container, $errorOutput, $debugEnabled);
56+
}
57+
58+
self::mergeNewAutoloadFunctions($autoloadFunctionsBefore);
59+
}
60+
61+
/**
62+
* Merges autoloaders registered since $autoloadFunctionsBefore into the
63+
* globals the BetterReflection source locators consult lazily (see
64+
* autoloadFunctions.php) - late merging in a deferred or forked run is
65+
* picked up by the next reflection ask.
66+
*
67+
* @param list<callable(string): void>|false $autoloadFunctionsBefore
68+
*/
69+
public static function mergeNewAutoloadFunctions(array|false $autoloadFunctionsBefore): void
70+
{
71+
/** @var list<callable(string): void>|false $autoloadFunctionsAfter */
72+
$autoloadFunctionsAfter = spl_autoload_functions();
73+
if ($autoloadFunctionsBefore === false || $autoloadFunctionsAfter === false) {
74+
return;
75+
}
76+
77+
$collectedAutoloadFunctions = collectNewAutoloadFunctions($autoloadFunctionsBefore, $autoloadFunctionsAfter);
78+
$GLOBALS['__phpstanAutoloadFunctions'] = array_merge(
79+
$GLOBALS['__phpstanAutoloadFunctions'] ?? [],
80+
$collectedAutoloadFunctions['appended'],
81+
);
82+
$GLOBALS['__phpstanAutoloadFunctionsPrependedToComposer'] = array_merge(
83+
$GLOBALS['__phpstanAutoloadFunctionsPrependedToComposer'] ?? [],
84+
$collectedAutoloadFunctions['prepended'],
85+
);
86+
}
87+
88+
}

‎src/Command/CommandHelper.php‎

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
use function array_filter;
4242
use function array_key_exists;
4343
use function array_map;
44-
use function array_merge;
4544
use function array_values;
4645
use function class_exists;
4746
use function count;
@@ -57,7 +56,6 @@
5756
use function is_file;
5857
use function is_readable;
5958
use function is_string;
60-
use function PHPStan\collectNewAutoloadFunctions;
6159
use function register_shutdown_function;
6260
use function spl_autoload_functions;
6361
use function sprintf;
@@ -96,6 +94,7 @@ public static function begin(
9694
?string $singleReflectionFile,
9795
?string $singleReflectionInsteadOfFile,
9896
bool $cleanupContainerCache,
97+
bool $deferBootstrapFiles = false,
9998
): InceptionResult
10099
{
101100
$stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output)));
@@ -520,23 +519,25 @@ public static function begin(
520519
$defaultLevelUsed = false;
521520
}
522521

523-
foreach ($container->getParameter('bootstrapFiles') as $bootstrapFileFromArray) {
524-
self::executeBootstrapFile($bootstrapFileFromArray, $container, $errorOutput, $debugEnabled);
525-
}
526-
527-
/** @var list<callable(string): void>|false $autoloadFunctionsAfter */
528-
$autoloadFunctionsAfter = spl_autoload_functions();
529-
530-
if ($autoloadFunctionsBefore !== false && $autoloadFunctionsAfter !== false) {
531-
$collectedAutoloadFunctions = collectNewAutoloadFunctions($autoloadFunctionsBefore, $autoloadFunctionsAfter);
532-
$GLOBALS['__phpstanAutoloadFunctions'] = array_merge(
533-
$GLOBALS['__phpstanAutoloadFunctions'] ?? [],
534-
$collectedAutoloadFunctions['appended'],
535-
);
536-
$GLOBALS['__phpstanAutoloadFunctionsPrependedToComposer'] = array_merge(
537-
$GLOBALS['__phpstanAutoloadFunctionsPrependedToComposer'] ?? [],
538-
$collectedAutoloadFunctions['prepended'],
539-
);
522+
// merges autoloaders registered since the top of begin() - the
523+
// --autoload-file require, anything container creation pulled in. This
524+
// cannot wait for the bootstrapFiles run: when that run is deferred,
525+
// the parent analyses long before it happens, and the source locators
526+
// must see these autoloaders throughout. It also cannot fold into
527+
// BootstrapFilesRunner::run() - by the time run() snapshots its own
528+
// before-list these are already registered, so its diff would exclude
529+
// them. run() brackets only the bootstrap files themselves, in
530+
// whichever process executes them.
531+
BootstrapFilesRunner::mergeNewAutoloadFunctions($autoloadFunctionsBefore);
532+
533+
// the analyse flow defers bootstrapFiles to the exact places that need
534+
// them: the main thread right before an in-process analysis, every
535+
// worker (spawned or forked - WorkerRunner), and the main thread after
536+
// the workers of a parallel analysis (AnalyserRunner) - resources the
537+
// files open (database connections!) must not be inherited by forked
538+
// children. Every other command runs them here.
539+
if (!$deferBootstrapFiles) {
540+
$container->getByType(BootstrapFilesRunner::class)->run($errorOutput, $debugEnabled);
540541
}
541542

542543
if (PHP_VERSION_ID >= 80000) {
@@ -627,7 +628,7 @@ public static function begin(
627628
/**
628629
* @throws InceptionNotSuccessfulException
629630
*/
630-
private static function executeBootstrapFile(
631+
public static function executeBootstrapFile(
631632
string $file,
632633
Container $container,
633634
Output $errorOutput,

‎src/Command/WorkerCommand.php‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
100100
$tmpFile,
101101
$insteadOfFile,
102102
false,
103+
deferBootstrapFiles: true,
103104
);
104105
} catch (InceptionNotSuccessfulException) {
105106
return 1;
@@ -143,14 +144,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
143144
// child can reuse it without re-booting (see ParallelAnalyser).
144145
$workerRunner = $container->getByType(WorkerRunner::class);
145146

146-
return $workerRunner->run(
147-
$output,
148-
$analysedFiles,
149-
(int) $port,
150-
$identifier,
151-
$tmpFile,
152-
$insteadOfFile,
153-
);
147+
try {
148+
return $workerRunner->run(
149+
$output,
150+
$analysedFiles,
151+
(int) $port,
152+
$identifier,
153+
$tmpFile,
154+
$insteadOfFile,
155+
);
156+
} catch (InceptionNotSuccessfulException) {
157+
// a deferred bootstrap file failed - its error is already printed
158+
return 1;
159+
}
154160
}
155161

156162
}

‎src/Parallel/ForkedProcess.php‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Parallel;
44

5+
use PHPStan\Command\InceptionNotSuccessfulException;
56
use PHPStan\ShouldNotHappenException;
67
use React\EventLoop\LoopInterface;
78
use React\EventLoop\TimerInterface;
@@ -88,14 +89,20 @@ public function start(callable $onData, callable $onError, callable $onExit): vo
8889
// the worker on its own fresh event loop and never return.
8990
$this->server->close();
9091
$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-
);
92+
try {
93+
$exitCode = $this->workerRunner->run(
94+
$output,
95+
$this->analysedFiles,
96+
$this->serverPort,
97+
$this->identifier,
98+
$this->tmpFile,
99+
$this->insteadOfFile,
100+
);
101+
} catch (InceptionNotSuccessfulException) {
102+
// a deferred bootstrap file failed - its error is already
103+
// printed to the child's collected stdout
104+
exit(1);
105+
}
99106
exit($exitCode);
100107
}
101108

0 commit comments

Comments
 (0)