Database backends cannot deal with corrupt serialized data: app completey broken

Created on 25 April 2017, almost 8 years ago
Updated 19 February 2024, 12 months ago

Problem/Motivation

Some cache backends including the database implementation rely on serialization to store cached data:

    // Unserialize and return the cached data.
    if ($cache->serialized) {
      $cache->data = unserialize($cache->data);
    }

I had this situation where the container definition (that is by default stored in the database cache) could not be unserialized (there are several reasons that could lead to such a situation. I.e. a change in the environment/runtime can make something that could be previously unserialized not unserializable anymore).

In such a situation, the cache backend FAILS to provide reliable information, because it will return the stored data as FALSE (that is a completely valid cache data value).

The FALSE value propagates leading to a completely dead Drupal application:

[25-Apr-2017 15:13:32 Europe/Madrid] TypeError: Argument 1 passed to Drupal\Component\DependencyInjection\Container::__construct() must be of the type array, boolean given, called in D:\_Webs\chf_envivoui_FZd\app\web\core\lib\Drupal\Core\DrupalKernel.php on line 883 in D:\_Webs\chf_envivoui_FZd\app\web\core\lib\Drupal\Component\DependencyInjection\Container.php on line 119 #0 D:\_Webs\chf_envivoui_FZd\app\web\core\lib\Drupal\Core\DrupalKernel.php(883): Drupal\Component\DependencyInjection\Container->__construct(false)
#1 D:\_Webs\chf_envivoui_FZd\app\web\core\lib\Drupal\Core\DrupalKernel.php(461): Drupal\Core\DrupalKernel->initializeContainer()
#2 D:\_Webs\chf_envivoui_FZd\app\web\core\lib\Drupal\Core\DrupalKernel.php(651): Drupal\Core\DrupalKernel->boot()
#3 D:\_Webs\chf_envivoui_FZd\app\web\index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request))
#4 {main}

Of course, in such a situation, a container rebuild will save they day. But I can imagine many other places where a corrupt serialized cache item could render the system unusable, and there would be no other solution but to completely wipe all caches - and there is no warranty that Drupal itself will be able to wipe the caches using regular mechanisms as it could be broken as in the previous situation - so you will need to do this in a more manual way.

Proposed resolution

When retrieving cache items using DatabaseCacheBackend, make sure that corrupt items are properly identified and treated as cache misses.

Currently broken items are treated as cache hits that return a FALSE as the cached data.

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
Cache 

Last updated about 1 hour ago

Created by

🇪🇸Spain david_garcia

Live updates comments and jobs are added and updated live.
  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024