Skip to content
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 race condition attempting to read cache files #47

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

acoulton
Copy link
Member

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()') returning false (rather than null or array). That can only have been coming from the result of the unserialize(file_get_contents($dir.$file) call. The most likely cause of that would be file_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 the find_files cache so that we can guarantee the Kohana::$files will always be an array.

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()')` returning
`false` (rather than `null` or `array`). That can only have been coming
from the result of the `unserialize(file_get_contents($dir.$file)` call.
The most likely cause of that would be `file_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 the `find_files` cache so
that we can guarantee the `Kohana::$files` will always be an array.
@acoulton acoulton merged commit 53e4e75 into 4.x Jan 16, 2025
12 checks passed
@acoulton acoulton deleted the 4.x-bug-cache-race-condition branch January 16, 2025 14:36
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