Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DoctrineBridge] Possible cirtucal references of lazy-service (listener) #54813

Open
oleg-andreyev opened this issue May 2, 2024 · 3 comments

Comments

@oleg-andreyev
Copy link
Contributor

oleg-andreyev commented May 2, 2024

Symfony version(s) affected

6.4.2

Description

Screenshot will answer all the questions:
image

How to reproduce

To address the complex bug, follow these steps:

  1. Initiate the BugCommand.
  2. Place a breakpoint at \App\Repository\CategoryRepository::__construct.
  3. Monitor the container as it creates three instances.

image

https://github.com/oleg-andreyev/bug_app

Possible Solution

Update PhpDumper and alter load method to track loading status of a service.

protected function load($file, $lazyLoad = true): mixed
    {
        try {
            if (isset($this->loading[$file])) {
                $path = array_keys($this->loading);
                $path = array_slice($path, array_search($file, $path, true));
                $path[] = $file;
                
                throw new \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException(
                    $file,
                    $path
                );
            }
            $this->loading[$file] = true;
            if (class_exists($class = __NAMESPACE__.'\\'.$file, false)) {
                return $class::do($this, $lazyLoad);
            }
    
            if ('.' === $file[-4]) {
                $class = substr($class, 0, -4);
            } else {
                $file .= '.php';
            }
    
            $service = require $this->containerDir.\DIRECTORY_SEPARATOR.$file;
    
            return class_exists($class, false) ? $class::do($this, $lazyLoad) : $service;
        } finally {
            unset($this->loading[$file]);
        }
    }

as result container will throw:

In App_KernelDevDebugContainer.php line 153:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
  Circular reference detected for service "getCategoryRepositoryService", path: "getCategoryRepositoryService -> getDoctrineService -> getDoctrine_Orm_DefaultEntityManagerService -> getDoctrine_Dbal_DefaultConnectionService -> getDoctrine_Dbal_DefaultConnection_EventManagerService -> getDoctrine_Orm_DefaultListeners_AttachEntityListenersService -> getStofDoctrineExtensions_Listener_TreeService -> getSomeListenerService -> getProductRepositoryService -> getCategoryRepositoryService".  

Additional Context

  1. The reason it's not an infinite loop is that \Symfony\Bridge\Doctrine\ContainerAwareEventManager::initializeListeners sets $this->initialized[$eventName] = true immediately after being called, even though the event is not initialized, which seems incorrect.

  2. The container does not retain any state of whether the current service is being initialized, therefore it does not throw any exceptions, though it arguably should.

@Hanmac
Copy link
Contributor

Hanmac commented May 6, 2024

Your Test Repo https://github.com/oleg-andreyev/bug_app has some missing parts

for example, CategoryRepository does not extend Gedmo\Tree\Entity\Repository\AbstractTreeRepository

@xabbuh
Copy link
Member

xabbuh commented May 13, 2024

I am going to close here for now due to the lack of feedback. Please let us know when you have more information and we can consider to reopen.

@xabbuh xabbuh closed this as not planned Won't fix, can't repro, duplicate, stale May 13, 2024
@oleg-andreyev
Copy link
Contributor Author

oleg-andreyev commented May 14, 2024

@Hanmac @xabbuh forgot to push changes: oleg-andreyev/bug_app@f4a9b29, please re-open it.

@xabbuh xabbuh reopened this May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants