Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Cache] Fix filesystem cache collision#39786
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
[Cache] Fix filesystem cache collision#39786
Uh oh!
There was an error while loading.Please reload this page.
Conversation
carsonbot commentedJan 11, 2021
Hey! I see that this is your first PR. That is great! Welcome! Symfony has acontribution guide which I suggest you to read. In short:
Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change. When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor! I am going to sit back now and wait for the reviews. Cheers! Carsonbot |
2844a6f to185c17aCompare| trait FilesystemCommonTrait | ||
| { | ||
| private$directory; | ||
| private$tmp; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This is a BC break isn't it?
nicolas-grekas commentedJan 11, 2021
Thanks for raising the issue and sending a PR! |
…lesystem adapter (nicolas-grekas)This PR was merged into the 4.4 branch.Discussion----------[Cache] fix possible collision when writing tmp file in filesystem adapter| Q | A| ------------- | ---| Branch? | 4.4| Bug fix? | yes| New feature? | no| Deprecations? | no| Tickets |Fix#39786| License | MIT| Doc PR | -Commits-------340d15e [Cache] fix possible collision when writing tmp file in filesystem adapter
NOTE: this isn't a theoretical case but a real issue I had in a production environment.
Current filesystem cache write implementation leads to cache collision under certain conditions.
I'll try to explain the conditions that lead to it and the reasoning behind each change step by step.
Once initialized,
Symfony/Component/Cache/Traits/FilesystemCommonTrait::$tmpnever changes. It means that if process is forked, both processes now use the same tmp file path to write to the cache. You don't really need that much concurrency to end up with a collision this way.The first fix iteration that comes to mind is removing the
ifcondition here:symfony/src/Symfony/Component/Cache/Traits/FilesystemCommonTrait.php
Lines 95 to 97 incce5a42
That would work better, but there's still a possibility of a race condition.
As you know
uniqid()is based on timestamp in microseconds. With enough luck and concurrency you may get exactly the same value from it in different processes/threads. Themore_entropyargument is supposed to help with that, however it doesn't work very well if it had been used before forking.Check out this example:
Output:
As you can see the 2nd and the 3rd line have the same "more entropy" value. The problem is that under the hood it's using anLCG algorithm. Once its constants are initialized, they never change and the sequence of generated numbers is pre-determined.
Thus
uniqid()was replaced withbin2hex(random_bytes(40)).$this->tmp) still leads to problems when used with coroutines (Swoole) and presumably - threads (I have no experience with multi-threading solutions in PHP, maybe they do some magic around it to prevent this kind of issue).As a result,
$tmpproperty was replaced with a local variable.