Skip to content

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

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

Closed
bastien-wink opened this issue Apr 2, 2025 · 0 comments · Fixed by #60122
Closed

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

bastien-wink opened this issue Apr 2, 2025 · 0 comments · Fixed by #60122

Comments

@bastien-wink
Copy link
Contributor

bastien-wink commented Apr 2, 2025

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

bastien-wink added a commit to bastien-wink/symfony that referenced this issue Apr 2, 2025
nicolas-grekas pushed a commit to bastien-wink/symfony that referenced this issue Apr 7, 2025
alexandre-daubois added a commit that referenced this issue Apr 8, 2025
* 6.4:
  Add tests
  bug #60121[Cache] ArrayAdapter serialization exception clean $expiries
  Fix #60160: ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface
alexandre-daubois added a commit that referenced this issue Apr 8, 2025
* 7.2:
  Add tests
  bug #60121[Cache] ArrayAdapter serialization exception clean $expiries
  Fix #60160: ChainAdapter accepts CacheItemPoolInterface, so it should work with adapter of CacheItemPoolInterface other than \Symfony\Component\Cache\Adapter\AdapterInterface
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants