Skip to content

Wait on the clock that timestamps writes in ResultCacheManagerCollectedDataTest - #7089

Merged
ondrejmirtes merged 1 commit into
2.3.xfrom
wait-on-file-clock-in-collected-data-test
Nov 16, 2026
Merged

ondrejmirtes merged 1 commit into
2.3.xfrom
wait-on-file-clock-in-collected-data-test

Conversation

@ondrejmirtes

Copy link
Copy Markdown
Member

ResultCacheManagerCollectedDataTest::testFileChangedAndChangedBackDuringTheAnalysisIsAnalysedAgain fails intermittently at line 354. The getFilesChangedDuringAnalysis() result comes back as [] instead of the changed file. It failed on 2.3.x HEAD 02480d849 in Compile PHAR's Run with Turbo Extension (windows-latest, 8.2, nts, make tests) (job 132884291961), and every other check on that commit passed. It also failed once in a local full-suite run, and about 1 in 10 local runs of the single test.

Root cause (test only). waitUntilTheFilesAreInThePast() waits until time() passes the files' latest mtime. But writes are timestamped with the kernel's coarse clock, which right after a second boundary can still read the previous second for a few milliseconds. In this sandbox, a write made as soon as time() ticked over was stamped with the earlier second 40 of 40 times. When the helper's 50 ms poll wakes inside that window, the test writes the file back in what should be a later second. The file gets back its old size, mtime, ctime, inode and device, and so the very stat signature restore() hashed it with. The manager is then right to see nothing changed.

Fix. The helper now waits on the clock that timestamps writes. It rewrites a probe file next to src/ (outside the scanned directory, like the cache file) until its mtime passes the files' latest timestamp. The now-unused time import is removed.

Verification

  • Deterministic reproduction: the 50 ms sleep was temporarily replaced with a busy wait, so the helper returns right at the boundary. The test then fails 8 of 8 times at line 354 without the fix and passes 8 of 8 with it.
  • With the real helper, the fixed test passed 30 of 30 runs. The unfixed test failed 1 in 10 in one batch and 0 in 30 in another, so it is genuinely intermittent.
  • tests/PHPStan/Analyser/ResultCache passes (34 tests). phpcs and PHPStan report no errors on the file.

A related note for the maintainer, not changed here: ResultCacheManager::hashAnalysedFiles() and FileStatSignatures::begin() also take "now" from time(). A file written in the first milliseconds of a second can be stamped with the second before, so it is not treated as racy even though it was written after the run began. The window is a few milliseconds and needs a same-size rewrite, but if it matters, a reference timestamp taken from the file system (as git does from its index file) would close it.

Shipped code is unchanged, so the downgraded PHAR's PHP 7.4 compatibility is unaffected.

…edDataTest

Right after a second boundary a write can still be timestamped with the
second before, so waitUntilTheFilesAreInThePast() returning as soon as
time() moved on let testFileChangedAndChangedBackDuringTheAnalysisIsAnalysedAgain
write the file back with the signature it was hashed with.

Amp-Thread-ID: https://ampcode.com/threads/T-01a1976c-1bde-72f8-b96c-072279109d59
@ondrejmirtes
ondrejmirtes merged commit ab33112 into 2.3.x Nov 16, 2026
577 checks passed
@ondrejmirtes
ondrejmirtes deleted the wait-on-file-clock-in-collected-data-test branch November 16, 2026 08:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant