From 68c725f8b19f3e38d23edfc49a076915920934d2 Mon Sep 17 00:00:00 2001 From: Dmitrii Metelkin Date: Fri, 1 Sep 2023 13:15:20 +1000 Subject: [PATCH] MDL-79185 cache: don't throw exception if cache data is corrupted --- cache/stores/file/lib.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cache/stores/file/lib.php b/cache/stores/file/lib.php index 12bb8b09fea..4f011d4d08c 100644 --- a/cache/stores/file/lib.php +++ b/cache/stores/file/lib.php @@ -441,8 +441,17 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i } while (!feof($handle)); $this->lastiobytes = strlen($data); + if ($this->lastiobytes == 0) { + // Potentially statcache is stale. File can be deleted, let's clear cache and recheck. + clearstatcache(true, $file); + if (!file_exists($file)) { + // It's a completely normal condition. Just ignore and keep going. + return false; + } + } + // Return it unserialised. - return $this->prep_data_after_read($data); + return $this->prep_data_after_read($data, $file); } /** @@ -548,13 +557,14 @@ class cachestore_file extends cache_store implements cache_is_key_aware, cache_i * Prepares the data it has been read from the cache. Undoing what was done in prep_data_before_save. * * @param string $data + * @param string $path * @return mixed - * @throws coding_exception */ - protected function prep_data_after_read($data) { + protected function prep_data_after_read($data, $path) { $result = @unserialize($data); if ($result === false && $data != serialize(false)) { - throw new coding_exception('Failed to unserialise data from file. Either failed to read, or failed to write.'); + debugging('Failed to unserialise data from cache file: ' . $path . '. Data: ' . $data, DEBUG_DEVELOPER); + return false; } return $result; }