#111494ObjectStorage::removeAll() leaves half of the objects when given its own storage
Description
Problem
Calling ObjectStorage::removeAll() with the storage itself removes only every other object, so the
relation is left half filled instead of empty. Measured on TYPO3 13.4.37, and read from main on
2026-11-18, where the method is unchanged:
| objects before | remaining after one call |
| 2 | 1 |
| 3 | 1 |
| 5 | 2 |
| 8 | 4 |
| 10 | 5 |
Calling it again halves what is left, so code that “empties” a relation this way ends up with stale
objects that are written back to the database. PHP’s native SplObjectStorage::removeAll(), which this
class stands in for, removes all objects in the same situation.
The core itself never calls the method — every removeAll() call site in typo3/sysext is
@QueryBuilder::getRestrictions()->removeAll()@ or CategoryCollection::removeAll() — and the unit test
suite only ever passes a different storage, which is why this has not surfaced.
Steps to reproduce
Run this script against a Composer project with typo3/cms-extbase 13.4, or a checkout of main. No
TYPO3 bootstrap is needed, only the autoloader:
require __DIR__ . '/vendor/autoload.php';
$storage = new \TYPO3\CMS\Extbase\Persistence\ObjectStorage();
for ($i = 0; $i < 10; $i++) {
$storage->attach(new \stdClass());
}
$storage->removeAll($storage);
echo count($storage);
- Expected:
0 - Actual:
5 - Counter-proof that the method is fine for a foreign storage: build two storages holding the same ten objects and call
$a->removeAll($b)—count($a)is0.
Cause
ObjectStorage implements \Iterator over its own $storage array, using the internal array pointer
of that array: rewind() is reset(), current() is current(), next() is next().
removeAll() walks the storage it is given and calls detach() for every object the loop yields.
@detach()@ is offsetUnset(), an unset() of the entry the pointer currently stands on — and the
@next()@ the foreach then performs steps past the element that has shifted into that position. When
the given storage is the same object, every removal skips one object, so exactly half of them survive.
LazyObjectStorage::removeAll() delegates to this method and behaves the same.
Suggested fix
Iterate a snapshot instead of the live pointer; toArray() already exists on the class:
--- a/typo3/sysext/extbase/Classes/Persistence/ObjectStorage.php
+++ b/typo3/sysext/extbase/Classes/Persistence/ObjectStorage.php
@@ public function removeAll(ObjectStorage $storage): void
{
- foreach ($storage as $object) {
+ foreach ($storage->toArray() as $object) {
$this->detach($object);
}
}
LazyObjectStorage inherits the fix. The existing removeAll tests in
@typo3/sysext/extbase/Tests/Unit/Persistence/ObjectStorageTest.php@ all pass a second storage and keep
passing; what is missing is the case that hands the storage to itself:
#[Test]
public function removeAllRemovesObjectsWhenGivenItself(): void
{
/** @var ObjectStorage<Entity> $storage */
$storage = new ObjectStorage();
$storage->attach(new Entity());
$storage->attach(new Entity());
self::assertCount(2, $storage);
$storage->removeAll($storage);
self::assertCount(0, $storage);
}Reviews 1
History 23
-
Sven Proffe changed description <span data-processor="relativetime" data-value="2026-11-18T17:22:44+00:00" > 11/18/26, 17:22 </span>
-
Gerrit Code Review pushed patch set 1 for main to change 97806 status
NewUnder Review <span data-processor="relativetime" data-value="2026-11-18T21:03:59+00:00" > 11/18/26, 21:03 </span> -
Oliver Bartsch commented <span data-processor="relativetime" data-value="2026-11-18T21:04:28+00:00" > 11/18/26, 21:04 </span>
Hey,
thanks for the detailed report, your analysis is correct. ObjectStorage iterates using the internal pointer of its own
$storagearray. WhenremoveAll()gets the same storage, eachdetach()unsets the element the pointer stands on, PHP moves the pointer forward to the next element, and thenext()offoreachthen skips that element. That is why every second object survives, exactly as in your table.I pushed a patch that makes
removeAll()iterate over a snapshot of the given storage taken withtoArray(), so detaching objects no longer interferes with the iteration.LazyObjectStorageand lazy-loaded storages get the fix through the same method. A new unit test removes 2, 3 and 10 objects from their own storage and checks that it ends up empty. Calls with a different storage behave as before. Until the fix is released,$storage->removeAll(clone $storage)works as a workaround.Thanks, Oli