Skip to content

Commit e1d6eb8

Browse files
Sander Mullerclaude
authored andcommitted
Rename the result cache into place instead of writing it over the old one
save() opens the final cache path and streams into it, so a run that dies while saving - killed by a CI timeout, a full disk, a Ctrl-C - leaves a partial file exactly where the next run looks for it. What happens then depends on the format: a truncated var_export'd file fails to parse and is discarded, which is luck rather than design, since any format that can be read halfway hands the next run a cache missing whatever came after the cut. The cache is now streamed to a sibling of the final path and renamed into place once it is complete. rename() within a directory is atomic, so a concurrent run reads either the whole old cache or the whole new one, and a run that dies leaves the previous cache untouched. The temporary file is removed when the save does not finish. The e2e fixture asserts the file is replaced rather than rewritten, which is the observable half of it: killing a process at the right microsecond is not something to put in CI. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent f13caf1 commit e1d6eb8

5 files changed

Lines changed: 68 additions & 3 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,22 @@ jobs:
3434
fail-fast: false
3535
matrix:
3636
include:
37+
- script: |
38+
cd e2e/result-cache-atomic-save
39+
../../bin/phpstan
40+
INODE=$(ls -i tmp/resultCache.php | awk '{print $1}')
41+
../../bin/phpstan
42+
# The cache is written next to its final path and renamed into place, so saving it
43+
# again replaces the file rather than truncating and rewriting the one the next run
44+
# reads. That is what keeps a run killed mid-save from leaving a partial cache behind.
45+
REPLACED=$([ "$INODE" != "$(ls -i tmp/resultCache.php | awk '{print $1}')" ] && echo replaced || echo 'written in place')
46+
../bashunit -a equals 'replaced' "$REPLACED"
47+
# A completed save leaves nothing next to the cache file.
48+
LEFTOVERS=$(ls tmp/ | grep -c '\.tmp$' || true)
49+
../bashunit -a equals '0' "$LEFTOVERS"
50+
OUTPUT=$(../../bin/phpstan -vvv 2>&1)
51+
echo "$OUTPUT"
52+
../bashunit -a contains 'Result cache restored. 0 files will be reanalysed.' "$OUTPUT"
3753
- script: |
3854
cd e2e/result-cache-1
3955
echo -n > phpstan-baseline.neon

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ e2e/bashunit
1717
/.phpbench
1818
/e2e/turbo-arena/tmp
1919
/e2e/fork-unsafe-extension/tmp
20+
/e2e/result-cache-atomic-save/tmp
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
parameters:
2+
level: 8
3+
tmpDir: tmp
4+
paths:
5+
- src
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ResultCacheE2EAtomicSave;
4+
5+
class Foo
6+
{
7+
8+
public function doFoo(): int
9+
{
10+
return 1;
11+
}
12+
13+
}

‎src/Analyser/ResultCache/ResultCacheManager.php‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use function fopen;
4646
use function fwrite;
4747
use function get_loaded_extensions;
48+
use function getmypid;
4849
use function hash_file;
4950
use function implode;
5051
use function in_array;
@@ -53,11 +54,13 @@
5354
use function is_file;
5455
use function ksort;
5556
use function microtime;
57+
use function rename;
5658
use function sort;
5759
use function sprintf;
5860
use function str_starts_with;
5961
use function substr;
6062
use function time;
63+
use function uniqid;
6164
use function unlink;
6265
use function var_export;
6366
use const PHP_VERSION_ID;
@@ -1238,15 +1241,27 @@ private function save(
12381241

12391242
$file = $this->cacheFilePath;
12401243

1244+
// Written to a sibling of the final path and renamed into place, so a run that dies while
1245+
// saving - or is killed by a CI timeout - leaves the previous cache untouched instead of a
1246+
// half-written one at the path the next run reads. rename() within a directory is atomic, so
1247+
// a concurrent run either reads the whole old cache or the whole new one.
1248+
// Named after the process so two runs sharing a tmpDir cannot write the same temporary file,
1249+
// and so a leftover from a run that was killed is reused rather than accumulating.
1250+
$pid = getmypid();
1251+
$temporaryFile = sprintf('%s.%s.tmp', $file, $pid === false ? uniqid() : $pid);
1252+
12411253
// streamed to the file section by section - building the whole
12421254
// var_export()ed contents in memory at once would take up roughly
12431255
// twice the size of the resulting file in the main process
1244-
$handle = @fopen($file, 'w');
1256+
$handle = @fopen($temporaryFile, 'w');
12451257
if ($handle === false) {
12461258
$error = error_get_last();
1247-
throw new CouldNotWriteFileException($file, $error !== null ? $error['message'] : 'unknown cause');
1259+
throw new CouldNotWriteFileException($temporaryFile, $error !== null ? $error['message'] : 'unknown cause');
12481260
}
12491261

1262+
$closed = false;
1263+
$renamed = false;
1264+
12501265
try {
12511266
$this->writeToHandle($handle, $file, "<?php declare(strict_types = 1);
12521267
@@ -1280,8 +1295,23 @@ private function save(
12801295
$this->writeToHandle($handle, $file, '; },
12811296
];
12821297
');
1283-
} finally {
12841298
fclose($handle);
1299+
$closed = true;
1300+
1301+
if (!@rename($temporaryFile, $file)) {
1302+
$error = error_get_last();
1303+
throw new CouldNotWriteFileException($file, $error !== null ? $error['message'] : 'unknown cause');
1304+
}
1305+
1306+
$renamed = true;
1307+
} finally {
1308+
if (!$closed) {
1309+
fclose($handle);
1310+
}
1311+
1312+
if (!$renamed) {
1313+
@unlink($temporaryFile);
1314+
}
12851315
}
12861316
}
12871317

0 commit comments

Comments
 (0)