Skip to content

Bug fix: problem with concurrency under stress - #34

Closed
chrisgraham wants to merge 4 commits into
nette:masterfrom
chrisgraham:concurrency_fix
Closed

chrisgraham wants to merge 4 commits into
nette:masterfrom
chrisgraham:concurrency_fix

Conversation

@chrisgraham

Copy link
Copy Markdown

This adds additional configurability to RobotLoader, to minimize unnecessary cache rewrites during stress. This reduces the chance of concurrency problems.

@dg
dg force-pushed the master branch 2 times, most recently from cca48ef to 45d6775 Compare June 18, 2024 20:26
@dg

dg commented Nov 4, 2024

Copy link
Copy Markdown
Member

Wouldn't it be better to set setAutoRefresh(false) in this case ?

@chrisgraham

Copy link
Copy Markdown
Author

In a development environment it's expected that new classes may appear, so auto refresh is needed.
But on the same development environment, PHPStan aggressively creates cache files as PHP code. Other environments could have similar issues as it is fairly common for PHP files to be used for caching systems.
I believe my PR provides a pretty modest solution to that, just making things a bit more configurable.
(Looking at it now I can see somehow it got noisy with other commits, so just look at the ones I did please.)

@dg

dg commented Oct 28, 2026

Copy link
Copy Markdown
Member

Thank you for the PR and the detailed report, and sorry for the long silence.

I am closing it without merging. The cache is written atomically (exclusive lock, temporary file, rename), and on Windows reads are guarded by a shared lock, so a reader should never see a half-written file. The most likely cause of what you saw is a transient Windows file lock (antivirus, indexer) making rename() fail. Version 4.1.3 retries in that case.

As for addExclusion() and setRetryLimit(): a class RobotLoader cannot find is looked up at most three times per cache and then never again, so they would save only a handful of writes, at the cost of two new methods in the API.

For PHPStan, I would recommend a separate bootstrap with $loader->setAutoRefresh(false). The code does not change during the analysis, so the cache does not need to change either; just refresh it before the run ($loader->refresh() in a step of its own). Auto-refresh stays on in your regular development environment.

If you still run into it with the current version, please reopen #33 with the error message you get.

@dg dg closed this Oct 28, 2026
@dg dg mentioned this pull request Oct 28, 2026
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.

2 participants