Skip to content

[DependencyInjection] WeakReference on 6.3 cause error #50439

Closed
@HypeMC

Description

@HypeMC

Symfony version(s) affected

6.3.x

Description

After upgrading my application to version 6.3.0-RC1 I started getting the following error on my CI pipeline:

Error: Attempt to modify property "privates" on null

After some research I found out the problem was introduced in #48469 .

How to reproduce

Here's an example test:

use App\Factory\PostFactory;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Zenstruck\Foundry\Test\Factories;
use Zenstruck\Foundry\Test\ResetDatabase;

class ExampleTest extends KernelTestCase
{
    use Factories;
    use ResetDatabase;

    public function testSomething(): void
    {
        gc_collect_cycles();

        $posts = PostFactory::createMany(5);

        self::assertCount(5, $posts);
    }
}

It would fail on the CI pipeline, but would pass locally. After a while I've realized I was able to reproduce the problem locally by adding gc_collect_cycles(); (this was not required for the test to fail on the CI).

I was also able to reproduce the problem with the following test:

use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
use Symfony\Component\DependencyInjection\Reference;

class ContainerTest extends KernelTestCase
{
    public function testWeakReferenceWithRewindableGenerator()
    {
        $container = new ContainerBuilder();
        $container->register('bar', \stdClass::class);
        $container->register('baz', \stdClass::class);
        $container->register('foo', \stdClass::class)
            ->setPublic(true)
            ->setProperty('iterator', new IteratorArgument([
                'bar' => new Reference('bar'),
                'baz' => new Reference('baz'),
            ]))
        ;
        $container->compile();

        $dumper = new PhpDumper($container);
        eval('?>'.$dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_WeakReferenceWithRewindableGenerator']));

        $container = new \Symfony_DI_PhpDumper_Test_WeakReferenceWithRewindableGenerator();

        $foo = $container->get('foo');
        self::assertInstanceOf(RewindableGenerator::class, $foo->iterator);

        unset($container);

        $array = iterator_to_array($foo->iterator); // Error: Attempt to modify property "privates" on null
        self::assertCount(2, $array);
        self::assertArrayHasKey('bar', $array);
        self::assertInstanceOf(\stdClass::class, $array['bar']);
        self::assertArrayHasKey('baz', $array);
        self::assertInstanceOf(\stdClass::class, $array['baz']);
    }
}

reproducer.zip

Possible Solution

No response

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