Skip to content

Commit

Permalink
Merge pull request #47 from ingenerator/4.x-bug-cache-race-condition
Browse files Browse the repository at this point in the history
Fix race condition attempting to read cache files
  • Loading branch information
acoulton authored Jan 16, 2025
2 parents 4d839a6 + 83a9bb9 commit 53e4e75
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ You're really going to want to read this.

## Unreleased

## 4.12.1 (2025-01-16)

* Fix race condition attempting to read cache files causing error from `find_file`

## 4.12.0 (2024-09-24)

* Support PHP 8.3
Expand Down
14 changes: 11 additions & 3 deletions classes/Kohana/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ public static function init(array $settings = NULL)

if (Kohana::$caching === TRUE)
{
// Load the file path cache
Kohana::$_files = Kohana::cache('Kohana::find_file()');
// Load the file path cache if present
Kohana::$_files = Kohana::cache('Kohana::find_file()') ?? [];
}

if (isset($settings['charset']))
Expand Down Expand Up @@ -902,7 +902,15 @@ public static function cache($name, $data = NULL, $lifetime = NULL)
// Return the cache
try
{
return \unserialize(\file_get_contents($dir.$file));
$content = \file_get_contents($dir.$file);
if ($content === FALSE)
{
// Cache has been removed since the `is_file` call above e.g. it is on the moment of expiry
// Treat as cache miss
return NULL;
}

return \unserialize($content);
}
catch (Exception $e)
{
Expand Down

0 comments on commit 53e4e75

Please sign in to comment.