Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix race conditions causing notices if directory does not exist #8302

Merged
merged 2 commits into from Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/Psalm/Config.php
Expand Up @@ -52,6 +52,7 @@
use function basename;
use function chdir;
use function class_exists;
use function clearstatcache;
use function count;
use function dirname;
use function error_log;
Expand Down Expand Up @@ -2268,6 +2269,7 @@ public function getPotentialComposerFilePathForClassLike(string $class): ?string

public static function removeCacheDirectory(string $dir): void
{
clearstatcache(true, $dir);
if (is_dir($dir)) {
$objects = scandir($dir, SCANDIR_SORT_NONE);

Expand All @@ -2276,17 +2278,29 @@ public static function removeCacheDirectory(string $dir): void
}

foreach ($objects as $object) {
if ($object !== '.' && $object !== '..') {
if (filetype($dir . '/' . $object) === 'dir') {
self::removeCacheDirectory($dir . '/' . $object);
} else {
unlink($dir . '/' . $object);
}
if ($object === '.' || $object === '..') {
continue;
}

// if it was deleted in the meantime/race condition with other psalm process
if (!file_exists($dir . '/' . $object)) {
continue;
}

if (filetype($dir . '/' . $object) === 'dir') {
self::removeCacheDirectory($dir . '/' . $object);
} else {
unlink($dir . '/' . $object);
}
}

reset($objects);
rmdir($dir);

// may have been removed in the meantime
clearstatcache(true, $dir);
if (is_dir($dir)) {
rmdir($dir);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Analyzer/FunctionLikeAnalyzer.php
Expand Up @@ -65,8 +65,6 @@
use function array_merge;
use function array_search;
use function array_values;
use function assert;
use function class_exists;
use function count;
use function end;
use function in_array;
Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Internal/Provider/ParserCacheProvider.php
Expand Up @@ -7,6 +7,7 @@
use Psalm\Config;
use RuntimeException;

use function clearstatcache;
use function error_log;
use function fclose;
use function file_get_contents;
Expand Down Expand Up @@ -302,6 +303,13 @@ public function saveFileContentHashes(): void
return;
}

// directory was removed
// most likely due to a race condition with other psalm instances that were manually started at the same time
clearstatcache(true, $root_cache_directory);
if (!is_dir($root_cache_directory)) {
return;
}

$file_content_hashes = $this->new_file_content_hashes + $this->getExistingFileContentHashes();

$file_hashes_path = $root_cache_directory . DIRECTORY_SEPARATOR . self::FILE_HASHES;
Expand Down