Skip to content

Commit

Permalink
improve unlinking potential race condition
Browse files Browse the repository at this point in the history
* fix rare race condition on file cache unlink
* remove unnecessary reset()
* improve code readability using variable
  • Loading branch information
kkmuffme committed Aug 24, 2022
1 parent 35bf0b5 commit 7373465
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2296,20 +2296,29 @@ public static function removeCacheDirectory(string $dir): void
continue;
}

$full_path = $dir . '/' . $object;

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

if (filetype($dir . '/' . $object) === 'dir') {
self::removeCacheDirectory($dir . '/' . $object);
if (filetype($full_path) === 'dir') {
self::removeCacheDirectory($full_path);
} else {
unlink($dir . '/' . $object);
try {
unlink($full_path);
} catch (RuntimeException $e) {
clearstatcache(true, $full_path);
if (file_exists($full_path)) {
// rethrow the error with default message
// it contains the reason why deletion failed
throw $e;
}
}
}
}

reset($objects);

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

0 comments on commit 7373465

Please sign in to comment.