|
45 | 45 | use function fopen; |
46 | 46 | use function fwrite; |
47 | 47 | use function get_loaded_extensions; |
| 48 | +use function getmypid; |
48 | 49 | use function hash_file; |
49 | 50 | use function implode; |
50 | 51 | use function in_array; |
|
53 | 54 | use function is_file; |
54 | 55 | use function ksort; |
55 | 56 | use function microtime; |
| 57 | +use function rename; |
56 | 58 | use function sort; |
57 | 59 | use function sprintf; |
58 | 60 | use function str_starts_with; |
59 | 61 | use function substr; |
60 | 62 | use function time; |
| 63 | +use function uniqid; |
61 | 64 | use function unlink; |
62 | 65 | use function var_export; |
63 | 66 | use const PHP_VERSION_ID; |
@@ -1238,15 +1241,27 @@ private function save( |
1238 | 1241 |
|
1239 | 1242 | $file = $this->cacheFilePath; |
1240 | 1243 |
|
| 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 | + |
1241 | 1253 | // streamed to the file section by section - building the whole |
1242 | 1254 | // var_export()ed contents in memory at once would take up roughly |
1243 | 1255 | // twice the size of the resulting file in the main process |
1244 | | - $handle = @fopen($file, 'w'); |
| 1256 | + $handle = @fopen($temporaryFile, 'w'); |
1245 | 1257 | if ($handle === false) { |
1246 | 1258 | $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'); |
1248 | 1260 | } |
1249 | 1261 |
|
| 1262 | + $closed = false; |
| 1263 | + $renamed = false; |
| 1264 | + |
1250 | 1265 | try { |
1251 | 1266 | $this->writeToHandle($handle, $file, "<?php declare(strict_types = 1); |
1252 | 1267 |
|
@@ -1280,8 +1295,23 @@ private function save( |
1280 | 1295 | $this->writeToHandle($handle, $file, '; }, |
1281 | 1296 | ]; |
1282 | 1297 | '); |
1283 | | - } finally { |
1284 | 1298 | 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 | + } |
1285 | 1315 | } |
1286 | 1316 | } |
1287 | 1317 |
|
|
0 commit comments