Issue Bug Resolved

#95899Try to cache an extbase queryResult leads to "Serialization of 'Closure' is not allowed"

Alex Kellner 11/07/21, 12:50 updated 09/01/26, 20:05

Description

I would like to add some caching into my backend views in lux to speed up diagrams.

But in TYPO3 11.5.2 together (with PHP 7.4 or 8.0) I always ran into the exception:
“Serialization of ‘Closure’ is not allowed”

An example extbase model could be very simple (without any relations or Lazy Loading) with just one string property and extend AbstractEntity. Also the repository function is without magic here:

$query = $this->createQuery();
$query->setLimit(3);
$query->setOrderings(['uid' => \TYPO3\CMS\Extbase\Persistence\QueryInterface::ORDER_DESCENDING]);
return $query->execute();

If I try to cache the result like

$cacheInstance = GeneralUtility::makeInstance(CacheManager::class)->getCache('bar');
$cacheInstance->set('foo', $queryResult, ['bar'], 100000);

The error is happening:

closure.png

Any ideas?

Reviews 4

Files 4

History 510

  1. Alex Kellner commented <span data-processor="relativetime" data-value="2021-11-07T18:49:58+00:00" > 11/07/21, 18:49 </span>

    Update: Using an array instead of QueryResult helps as workarround:

    $query->execute()->toArray();
  2. Marvin Buchmann commented and changed attached File.php attached SerializableEntityInterface.php attached SerializableEntityTrait.php <span data-processor="relativetime" data-value="2022-12-21T08:52:59+00:00" > 12/21/22, 08:52 </span>

    Alex Kellner wrote in #note-1:
    > Update: Using an array instead of QueryResult helps as workaround:
    > […]

    Thanks! I had the same problem when trying to cache my QueryResult. But the workaround only seems to work if the model doesn’t contain any (lazy) ObjectStorage properties (tested in TYPO3 11.5).
    Since I needed those, I created a workaround that works for all my models. I got the idea from this fix https://review.typo3.org/c/Packages/TYPO3.CMS/+/68341.

    It’s basically just an interface and a trait unsetting all ObjectStorage properties by default when serialized and calling the initializeObject method of the object when unserialized. If not implemented, it provides one that just initializes the ObjectStorage properties empty.

    The model has to implement the interface and use the trait. Then it’s serializable and therefore cacheable. :) At least in my use cases.
    I attached an example model, the interface and the trait.

    I know traits are not really desirable, but the methods could be implemented in the AbstractEntity class if it works for all other use cases aswell. Then every extbase model would be cacheable by default, which I would really appreciate.

  3. Georg Ringer commented <span data-processor="relativetime" data-value="2026-08-27T19:51:33+00:00" > 08/27/26, 19:51 </span>

    I traced this on current main (v15-dev). The chain is verified with a functional
    test that walks the object graph honouring __sleep() and reports every Closure:

    $queryResult
     -> QueryResult::$query                       (__sleep => ['query'])
     -> Query::$querySettings                     (__sleep includes 'querySettings')
     -> Typo3QuerySettings::$configurationManager <-- no __sleep, this is where it leaks
     -> ConfigurationManager::$feConfigManager
     -> FrontendConfigurationManager::$flexFormTools
     -> FlexFormTools::$eventDispatcher
     -> EventDispatcher::$listenerProvider
     -> ListenerProvider::$container
     -> DependencyInjectionContainer_...::$getService   [Closure]

    QueryResult and Query both do their homework: they have __sleep()/@__wakeup()@
    and keep their DI dependencies out. @Typo3QuerySettings@ has none, so the whole
    compiled Symfony container hangs off the object graph through
    @$configurationManager@. That is the entire bug — the domain models are innocent,
    which is exactly why toArray() works.

    Four things not yet mentioned in this issue

    1. Both properties are dead code.

    Typo3QuerySettings never reads $configurationManager or $context after the
    constructor. Complete list of occurrences in the class:

    :93   $this->context = clone $context;
    :94   $this->configurationManager = $configurationManager;
    :95   $this->languageAspect = $this->context->getAspect('language');

    So the container is dragged along for nothing.

    2. Query::__wakeup() is broken on its own, independent of the Closure problem.

    // Query.php:649
    $this->persistenceManager = GeneralUtility::makeInstance(PersistenceManagerInterface::class);
    $this->dataMapFactory    = GeneralUtility::makeInstance(DataMapFactory::class);   // throws
    $this->qomFactory        = GeneralUtility::makeInstance(QueryObjectModelFactory::class);

    PersistenceManagerInterface is registered public: true in
    @extbase/Configuration/Services.yaml:17@, DataMapFactory is not. For private
    services makeInstance() falls back to new $className(), and
    @DataMapFactory::__construct()@ takes six arguments. Reproduced:

    ArgumentCountError: Too few arguments to function DataMapFactory::__construct(),
    0 passed in GeneralUtility.php on line 2489 and exactly 6 expected

    So even with the Closure issue solved, the wake-up path is dead. The correct call
    already exists two classes away, in LazyObjectStorage::__unserialize():314 —
    @GeneralUtility::getContainer()->get(DataMapper::class)@.

    3. Query::__wakeup() does not restore $container.

    $container (Query.php:78) was added later when execute() was switched over to
    the container (Query.php:217) and was forgotten in __wakeup(). After waking up,
    @execute()@ would hit “Typed property must not be accessed before initialization”.

    4. QueryResult::__wakeup() silently loses the language configuration.

    setQuery() (QueryResult.php:65) normally does two things: remember the query
    *and* call $this->dataMapper->setQuery($query). __wakeup() only does the first.
    Verified: after waking up, DataMapper::$query is null. Consequences:

    • DataMapper.php:184 — $this->query?->getQuerySettings()->getLanguageAspect()
      falls back to new LanguageAspect(), so a cached result would be mapped with
      default language behaviour instead of the query’s.
    • DataMapper.php:471 — setParentQuery() is skipped, lazy relations lose their
      query context.

    Not a crash, a silent correctness bug. Nobody notices today because you never get
    that far.

    Marvin’s ObjectStorage concern is obsolete

    The very change referenced in note-3 (68341 landed: LazyObjectStorage::__serialize():298 and
    @LazyLoadingProxy::__serialize():235@ drop the DataMapper and fetch it from the
    container on wake-up. Verified on main with blog_example (Blog::$posts is
    @#[Lazy]@):

    serialize(toArray()):           OK, 3322 bytes
    unserialize:                    OK
    lazy posts after unserialize:   LazyObjectStorage, count=10

    toArray() serialises cleanly today, lazy relations included, and lazy loading
    still works afterwards. The attached interface + trait are no longer needed.

    The bigger point: a cached QueryResult caches no data

    QueryResult::__sleep() returns ['query']. $queryResult (the mapped objects) and
    @$numberOfResults@ are dropped. What ends up in the cache is the query, not the
    result
    — on first access after waking up, initialize() runs the database query
    again.

    The original goal here was “add some caching into my backend views in lux to speed
    up diagrams”. Even with working serialisation that would have gained nothing.
    @toArray()@ is not a workaround, it is the correct answer for that use case.

    Suggested way forward

    1. Give Typo3QuerySettings a __sleep() returning only its own scalar state plus
      languageAspect, and a __wakeup() restoring Context (SingletonInterface, no
      ctor args) and ConfigurationManagerInterface (Services.yaml:26, public: true)
      — both reachable via makeInstance(). Non-breaking and backportable.
    2. Fix the three broken wake-up spots above, otherwise one exception is just traded
      for the next.
    3. Document that toArray() is the right call when you want to cache the data.

    The alternative would be to declare QueryResult off-limits for caching altogether
    and have __sleep() throw a descriptive exception (“use toArray() to cache the
    result set”). That solves the reported use case better, but blocks legitimate cases
    such as caching a query for re-execution in a later request. That decision should be
    made before anyone writes the patch — happy to provide it either way.

    Code is identical on main, 14.3 and 13.4 (line offsets only), so a fix is
    backportable to both.

    #91364 has the same root cause and could be
    closed as a duplicate of this one.

  4. Georg Ringer commented and changed status Under Review Resolved done 0 % 100 % <span data-processor="relativetime" data-value="2026-09-01T20:05:09+00:00" > 09/01/26, 20:05 </span>

    Applied in changeset commit:0dfa5ff2b0a5fa4311ee8a9105fdd86578311641.