symfony: Upgrade to 5.2.4. gives Undefined index: _service_doctrine.orm.default_listeners.attach_entity_listeners

Symfony version(s) affected: 5.2.4

Description Undefined index: _service_doctrine.orm.default_listeners.attach_entity_listeners

How to reproduce Downgrading to 5.2.3. the error disappears

Additional context

[2021-03-04T20:30:49.646533+01:00] request.CRITICAL: Exception thrown when handling an exception (ErrorException: Notice: Undefined index: _service_doctrine.orm.default_listeners.attach_entity_listeners at /app/vendor/symfony/doctrine-bridge/ContainerAwareEventManager.php line 64) {"exception":"[object] (ErrorException(code: 0): Notice: Undefined index: _service_doctrine.orm.default_listeners.attach_entity_listeners at /app/vendor/symfony/doctrine-bridge/ContainerAwareEventManager.php:64)"} []

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 24 (8 by maintainers)

Commits related to this issue

Most upvoted comments

We regularly merge lower branches up into all still maintained branches. So this will also land in 5.2.

The same happens on 4.4.20 as well.

could you please provide a reproducer ?

@jderusse Here is a reproducer:

https://github.com/michanismus/eventsmanager-reproducer

Error appears when an listener added by subscriber triggers an loadClassMetadata. Then there is a nested call for the event.

https://github.com/symfony/doctrine-bridge/blob/9e2c53f3e8f8a6ccecd80de5c2c8b71beeca7fc8/ContainerAwareEventManager.php#L180

To fix that the line 180 has to be replaced with:

// $this->listeners[$event] += $listeners;
foreach ($listeners as $listener) {
    $this->addEventListener($event, $listener);
}

Edit:

composer install
bin/console doc:mig:mig

Request the TestController route /.

@jderusse

Here is the bugfix, sorry, I do not have the time to create a PR. Replace the function initializeSubscribers. You simple merged uninitialized listeners with initialized without a check.

    private function initializeSubscribers()
    {
        $this->initializedSubscribers = true;

        $eventListeners = $this->listeners;
        // reset eventListener to respect priority: EventSubscribers have a higher priority
        $this->listeners = [];
        foreach ($this->subscribers as $id => $subscriber) {
            if (\is_string($subscriber)) {
                parent::addEventSubscriber($this->subscribers[$id] = $this->container->get($subscriber));
            }
        }
        foreach ($eventListeners as $event => $listeners) {
            if (!isset($this->listeners[$event])) {
                $this->listeners[$event] = [];
            }
            foreach ($listeners as $hash => $listener) {
                if (\is_string($listener)) {
                    $listener = $this->container->get($listener);
                    $hash = $this->getHash($listener);

                    $this->methods[$event][$hash] = $this->getMethod($listener, $event);
                }

                $this->listeners[$event][$hash] = $listener;
            }
        }
        $this->subscribers = [];
    }