Fix race condition attempting to read cache files #47
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
We were very occasionally seeing an error
PHP Warning: ErrorException: Automatic conversion of false to array is deprecated in .../classes/Kohana/Core.php:746
in particular when a site is under heavy load.This was caused by
Kohana::cache('Kohana::find_file()')
returningfalse
(rather thannull
orarray
). That can only have been coming from the result of theunserialize(file_get_contents($dir.$file)
call. The most likely cause of that would befile_get_contents
returning false due to a race condition where the file existed at the start of the if block but no longer existing by the time we attempt to read the file.My assumption is that with concurrent requests at the exact moment of cache expiry it is possible for one request to delete the file just before the other request tries to read it.
With this change we treat the failed read as any other failure to unserialise the cache file and just return null. Additionally we cast the null to
[]
specifically when loading thefind_files
cache so that we can guarantee theKohana::$files
will always be an array.