Skip to content

Commit fa4bdf1

Browse files
committed
Make gen_stub parallelism safe
If PHP-Parser is not yet installed, make sure we don't try to install it N times in parallel.
1 parent d554d91 commit fa4bdf1

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

build/gen_stub.php

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -997,29 +997,50 @@ function generateFunctionEntries(?string $className, array $funcInfos): string {
997997
return $code;
998998
}
999999

1000-
function initPhpParser() {
1001-
$version = "4.3.0";
1002-
$phpParserDir = __DIR__ . "/PHP-Parser-$version";
1003-
if (!is_dir($phpParserDir)) {
1000+
function installPhpParser(string $version, string $phpParserDir) {
1001+
$lockFile = __DIR__ . "/PHP-Parser-install-lock";
1002+
$lockFd = fopen($lockFile, 'w+');
1003+
if (!flock($lockFd, LOCK_EX)) {
1004+
throw new Exception("Failed to acquire installation lock");
1005+
}
1006+
1007+
try {
1008+
// Check whether a parallel process has already installed PHP-Parser.
1009+
if (is_dir($phpParserDir)) {
1010+
return;
1011+
}
1012+
10041013
$cwd = getcwd();
10051014
chdir(__DIR__);
10061015

1007-
passthru("wget https://github.com/nikic/PHP-Parser/archive/v$version.tar.gz", $exit);
1016+
$tarName = "v$version.tar.gz";
1017+
passthru("wget https://github.com/nikic/PHP-Parser/archive/$tarName", $exit);
10081018
if ($exit !== 0) {
1009-
passthru("curl -LO https://github.com/nikic/PHP-Parser/archive/v$version.tar.gz", $exit);
1019+
passthru("curl -LO https://github.com/nikic/PHP-Parser/archive/$tarName", $exit);
10101020
}
10111021
if ($exit !== 0) {
10121022
throw new Exception("Failed to download PHP-Parser tarball");
10131023
}
10141024
if (!mkdir($phpParserDir)) {
10151025
throw new Exception("Failed to create directory $phpParserDir");
10161026
}
1017-
passthru("tar xvzf v$version.tar.gz -C PHP-Parser-$version --strip-components 1", $exit);
1027+
passthru("tar xvzf $tarName -C PHP-Parser-$version --strip-components 1", $exit);
10181028
if ($exit !== 0) {
10191029
throw new Exception("Failed to extract PHP-Parser tarball");
10201030
}
1021-
unlink(__DIR__ . "/v$version.tar.gz");
1031+
unlink(__DIR__ . "/$tarName");
10221032
chdir($cwd);
1033+
} finally {
1034+
flock($lockFd, LOCK_UN);
1035+
@unlink($lockFile);
1036+
}
1037+
}
1038+
1039+
function initPhpParser() {
1040+
$version = "4.3.0";
1041+
$phpParserDir = __DIR__ . "/PHP-Parser-$version";
1042+
if (!is_dir($phpParserDir)) {
1043+
installPhpParser($version, $phpParserDir);
10231044
}
10241045

10251046
spl_autoload_register(function(string $class) use($phpParserDir) {

0 commit comments

Comments
 (0)