#110909Extbase ReflectionService destructor writes corrupt class schema cache
Description
TYPO3\CMS\Extbase\Reflection\ReflectionService::__destruct writes the class schemata into the extbase cache. When the PHP garbage collector runs that destructor while another serialize call is in progress, the serialize call inside the destructor does not start a reference table of its own. It continues the table of the outer call, so the back references it writes (r:N; and R:N;) are numbered in the outer table. VariableFrontend appends a valid HMAC to that string and stores it. The next ReflectionService reads the entry, the HMAC validates, and unserializing it fails:
bc. PHP Warning: unserialize: Error at offset 874 of 9256 bytes in typo3/sysext/core/Classes/Serializer/AuthenticatedMessageDeserializer.php line 71
The empty parentheses after unserialize are left out in this issue, the tracker’s firewall rejects them. The call chain is AuthenticatedMessageDeserializer from VariableFrontend::get from ReflectionService::__construct.
The outer string is damaged as well. Every value the destructor serializes is counted in the table of the outer call, so each back reference the outer call writes afterwards points to the wrong value. Which outer call is hit depends on when the collector runs, it is not limited to cache writes of Extbase.
When it happens
Three conditions, all met in a functional test run and possible in production:
- A
ReflectionServicethat built a class schema becomes cyclic garbage. A discarded DI container leaves every service it holds in reference cycles, which is what happens between functional tests. - Another serialize call runs over a graph whose serialization creates root candidates for the cycle collector. Every array a
__serializemethod returns and that is freed afterwards drops the refcount of the objects in it to a non-zero value. Class schemata themselves are such a graph. - The root buffer fills up during that call (10000 roots by default), so the collector runs inside the serialize call, between two values, and calls the destructor.
In a functional test suite it shows up deterministically for a given set of test classes in one PHP process and moves whenever that set changes. It was found in an extension’s functional test suite, where a test class failed with the warning above depending on which other test classes ran before it in the same process.
Production can be hit too, rarely: any long running process (CLI commands, queue workers, long-poll requests) that serializes large structures while ReflectionService instances are destroyed.
Why v14 and main hit it far more often than v13.4
Since v14 the class schemata carry shared Symfony\Component\TypeInfo objects, which are serialized as back references. A schema without any back reference is not broken by the shifted numbering, which is why the schema of FileReference survives on v13.4 while it breaks on v14. Older schemata do share values, for example default values of properties and method parameters, and the same failure was observed on v12.4 as well, only much less often.
Reproduction
Two standalone scripts are attached:
php-nested-serialize.phpneeds PHP only. A destructor that serializes, run by the collector inside another serialize call. This is also the reproduction for the PHP mechanism itself.typo3-reflection-service.phpuses the realReflectionServiceandVariableFrontendwith an in-memory cache backend that is not transient, so the frontend serializes and signs exactly as with the file or database backend. Nothing is bootstrapped, only an encryption key is set.
bc. php php-nested-serialize.php
bc. php typo3-reflection-service.php vendor/autoload.php
Results against current main (15.0.0-dev) in the ghcr.io/typo3/core-testing-php85:1.8 and ghcr.io/typo3/core-testing-php86:1.0 images, PHP 8.5.10 and 8.6.0alpha3, identical on both:
bc. Destructor ran during the outer serialize: true
Written by the destructor ends in r:30003 instead of r:2
unserialize FAILED: unserialize: Error at offset 81 of 82 bytes
bc. PHP warning while reading the class schemata: unserialize: Error at offset 874 of 9256 bytes
Cache entry: 9256 bytes, HMAC valid, back references: 30012, 30013, 30015, 30031, 30043, 30055, 30067, 30079
unserialize of the entry FAILED
Earlier runs showed the same with PHP 8.2.33 and 8.4.24 for the plain PHP script, and with TYPO3 14.3.7 on PHP 8.5.9 for the TYPO3 one.
Possible fixes
- Core: do not persist from
__destruct. Write a schema when it is built (per class, the wayDataMapFactoryalready writes its data maps into the same cache), persist at the end of the request or through an explicit flush, or serialize into a string before any nested serialize call can start. The last option is not sufficient on its own: a serialize call inside a destructor run by the collector shifts the numbering of the outer call even when it only writes strings, so the outer string breaks instead. The guard added for #110100 (skip the write whileTYPO3_CONF_VARSis not set) does not help, the globals are set while the outer call runs. - typo3/testing-framework: functional test instances should use a
TransientMemoryBackendfor theextbasecache (SYS.caching.cacheConfigurations.extbase.backend) by default.VariableFrontendnever serializes for a transient backend, and a test process gains nothing from persisting class schemata. Extensions currently work around it per test class. - Follow-up, to be split into an issue of its own: the core should be inspected for other
__destructmethods that serialize or write caches, they have the same side effect when the collector runs them inside a serialize call. Beyond that, a clean reset story for CLI, long-poll and testing scenarios is needed, or one place that aggregates such deferred writes, instead of relying on destructors.
Related: #110100 (the guard in the destructor), #98124 (destructor order of the cache frontend). #106107 (unserialize warning from VariableFrontend in a CLI command, closed without a cause) may be the same defect, which is not confirmed.
Reviews 1
Files 2
History 13
-
Gerrit Code Review pushed patch set 1 for main to change 96351 status
NewUnder Review <span data-processor="relativetime" data-value="2026-10-04T18:58:32+00:00" > 10/04/26, 18:58 </span> -
Gerrit Code Review pushed patch set 2 for main to change 96351 <span data-processor="relativetime" data-value="2026-10-19T18:55:48+00:00" > 10/19/26, 18:55 </span>
-
Stefan Bürk changed status
Under ReviewRejected <span data-processor="relativetime" data-value="2026-10-19T20:38:57+00:00" > 10/19/26, 20:38 </span>