Skip to content

[Cache] ArrayAdapter serialization exception does not clean $expiries #60121

Closed
@bastien-wink

Description

@bastien-wink

Symfony version(s) affected

7.3 (since 5.0)

Description

In ArrayAdapter.php:339, if $value contains a Closure, an Exception is triggered and $this->values get cleaned.

  try {
      $serialized = serialize($value);
  } catch (\Exception $e) {
      unset($this->values[$key], $this->expiries[$key], $this->tags[$key]);
      $type = get_debug_type($value);
      $message = \sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
      CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);

      return null;
  }

$this->expiries is used in ArrayAdapter.php:300 to determine cache hit. If hash is found in $this->expiries but missing in $this->keys it generate a NOTICE.

How to reproduce

Use this Doctrine configuration with an Entity containing a Closure.

    orm:
        entity_managers:
            default:
                second_level_cache:
                    enabled: true
                    regions:
                        array_persistence:
                            # Store with Cache/Adapter/ArrayAdapter
                            cache_driver: pool

Possible Solution

On serialize($value); catch Exception, clean expiries with unset.

Incoming Pull Request.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions