Skip to content

Doctrines event subscriber out of memory. #28010

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

Closed
ln-e opened this issue Jul 19, 2018 · 4 comments
Closed

Doctrines event subscriber out of memory. #28010

ln-e opened this issue Jul 19, 2018 · 4 comments

Comments

@ln-e
Copy link

ln-e commented Jul 19, 2018

Symfony version(s) affected: 4.*

Description

  1. Create Doctrine's EventSubscriber.
  2. Inject into in some service.
  3. Inject into that service EntityManagerInterface
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in var/cache/dev/ContainerLFEgQjr/getDoctrine_Dbal_DefaultConnectionService.php on line 1

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in var/cache/dev/ContainerLFEgQjr/getDoctrine_Dbal_DefaultConnectionService.php on line 1
PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in vendor/symfony/debug/Exception/OutOfMemoryException.php on line 1

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in vendor/symfony/debug/Exception/OutOfMemoryException.php on line 1

It worked in symfony 3.3 but it didn't work after update to symfony 4.1

How to reproduce
https://github.com/ln-e/symfony_out_of_memory

Possible Solution

Additional context

@dmaicher
Copy link
Contributor

dmaicher commented Jul 23, 2018

Can confirm the issue. Until its resolved you could temporarily mark your EventSubscriber lazy:

diff --git a/composer.json b/composer.json
index 49746b5..4dd4ca4 100644
--- a/composer.json
+++ b/composer.json
@@ -15,6 +15,7 @@
         "symfony/monolog-bundle": "^3.1",
         "symfony/orm-pack": "*",
         "symfony/process": "^4.1",
+        "symfony/proxy-manager-bridge": "^4.1",
         "symfony/security-bundle": "^4.1",
         "symfony/serializer-pack": "*",
         "symfony/swiftmailer-bundle": "^3.1",
diff --git a/config/services.yaml b/config/services.yaml
index f98bd4e..2075d09 100644
--- a/config/services.yaml
+++ b/config/services.yaml
@@ -33,3 +33,6 @@ services:
 
     # add more service definitions when explicit configuration is needed
     # please note that last definitions always *replace* previous ones
+
+    App\Event\EntitySubscriber:
+        lazy: true

edit: ah it actually seems like just enabling lazy services in general (adding the proxy manager bridge) is already enough.

@nicolas-grekas
Copy link
Member

Thanks for the reproducer.

The cause of the issue is a circular reference when proxy-manager-bridge is not installed: on 3.3, this is detected at runtime and leads to [Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException] Circular reference detected for service "doctrine.orm.default_entity_manager", path: "doctrine.orm.default_entity_manager -> doctrine.dbal.default_connection". (thus, not sure what you meant by "It worked in symfony 3.3": it doesn't "work" - but it fails properly yes :))

On 3.4 & up, this circular reference is undetected and turns to an infinite loop.

@ln-e
Copy link
Author

ln-e commented Jul 24, 2018

Oh, I see. In my 3.3 project I have an installed doctrine/doctrine-migrations-bundle. And It is somehow solve my problem just like symfony/proxy-manager-bridge do it for 4.1 version.

I have try on new install sf3.3. If there is migrations-bundle - no error, otherwise Circular reference detected.

Anyway installing symfony/proxy-manager-bridge solves problem in 4.1, thanks.

@stof
Copy link
Member

stof commented Jul 25, 2018

that's because doctrine/doctrine-migrations-bundle requires the ocramius/proxy-manager library so this one got installed, and your 3.3 project was probably using symfony/symfony rather than component packages, so the code of the bridge was also available. So this leads to a situation where you have the proxy manager bridge able to handle lazy services.

nicolas-grekas added a commit that referenced this issue Aug 8, 2018
…ptions or infinite loops at runtime (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Fix false-positive circular ref leading to wrong exceptions or infinite loops at runtime

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28010, #27865
| License       | MIT
| Doc PR        | -

When circular loops involve references in properties, method calls or configurators, it is possible to properly instantiate the related services.

The current logic is broken: `ContainerBuilder` considers some of these loops as self-referencing circular references, leading to a runtime exception, and in similar situations, `PhpDumper` generates code that turns to infinite loops at runtime 💥. These badly handled situations happen with inlined definitions.

This PR fixes both classes by making them track which references are really part of the constructors' chain, including inline definitions.

It also fixes dumping infinite loops when dumping circular loops involving lazy services while proxy-manager-bridge is not installed.

Commits
-------

e843bb8 [DI] Fix false-positive circular ref leading to wrong exceptions or infinite loops at runtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants