-
Notifications
You must be signed in to change notification settings - Fork 715
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix writeFile() mkdir() race condition #379
Fix writeFile() mkdir() race condition #379
Conversation
f960d31
to
1ae6b5d
Compare
This is a backport of the code in smarty-php/smarty#379 to Shopware's vendored Smarty 3.1.8.
This pull request is rejected as it calls mkdir() on each file write for each subfolder if needed or not. Meanwhile libs/sysplugins/smarty_internal_runtime_writefile.php has been rewritten to retry in case of error. The fix is in the trunk version and will later be included in 3.1.32 Note: |
Good point about the cache locking, I was unaware of this! |
I've just tried both of your approaches on a system where I was able to reproduce the issue consistently. Enabling I've also tried your workaround for the race condition. The effect of this was that I was getting |
I have added a clearstatcache() just in case. |
Yes, I'm sure that the permissions are correct. After all, everything works when I use the fix I've provided in this PR. I've also tested the version of your fix with |
PHP's
mkdir()
is not concurrency-safe when the$recursive
flag is enabled. When multiple concurrent processes try to create paths within the same directory structure, interleaving execution ofmkdir()
may cause some of those processes to fail if another process creates a subdirectory they were just about to create. Please see https://bugs.php.net/bug.php?id=35326 for detailed discussion.This race condition may cause Smarty template compilation to fail randomly when not all templates have been initialized. Lately, we've observed this problem with increasing frequency in https://github.com/shopware/shopware.
@a-shpota has added code that detects this situation and throws a more meaningful message in 6768340. Unfortunately, template compilation will still fail even with these changes applied.
This PR adds a concurrency-safe PHP reimplementation of recursive
mkdir()
. Using this implementation,writeFile()
will not fail anymore in situations that would have triggered themkdir()
race condition before.