-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Broken entity manager service #30091
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
Comments
Note that this is not a BC break, it's just a bug :) |
A lot of WTF indeed... |
Symfony 4.2.4 was just released and I can confirm the this horrible bug still exists. Getting two different EntityManagers when using constructor injection in two different services (in my case a controller and an event listener). I wonder why not more people report here, I guess this breaks a lot of applications... |
I seem to be suffering from the same bug on a project using Symfony 4.2.4. In my case '@doctrine.orm.entity_manager' is injected into two services. For this comment I will call them Service A and Service B. Service A is injected into Service B. Within Service B, Service A is used to create, persist and return an entity without also flushing. Within Service A, just before it returns the entity, $this->entityManager->contains($entity) returns true, and spl_object_hash($this->entityManager) returns "0000000015d8e5160000000059204dbc". Within Service B, after Service A returns $entity, $this->entityManager->contains($entity) returns false, and spl_object_hash($this->entityManager) returns "0000000015d8e61d0000000059204dbc", which differs by one character from the hash of the entity manager instance in Service A. Edit: Edit: |
Any progress on this one? The bugs this causes are very difficult to relate to this issue, I guess thats why it is so silent here. |
This does not seem to be the case for me. I was using symfony/symfony at 4.2.2 which solved this for me, using the symfony/* packages individually at 4.2.4 and only symfony/dependency-injection at 4.2.2 reintroduced the problem. |
Hello there, I still experiment strange behaviours. I'm on symfony 3.4.24. I tried to downgrade DI component to 3.4.22 and 3.4.21 without any success. Is there another workaround ? Thank you, Fabien |
seems like its introduced in #30046 |
Error still exists for me in dependency-injection 4.2.6. |
Please provide us some clear reproducing cases. We have 2 conflicting reports here. @wiseguy1394 says that locking to 4.2.2 solves the issue, while @ampaze says that locking at 4.2.2 has the issue. This won't help us fix anything (and it might mean that you even have 2 different issues). |
I have reproducer provided here and here, but they all involve complex applications and I didn't manage to reduce the case to a minimal service graph. All involve the Doctrine |
Here is a case that is reproduce-able:
Notes: Using step debugging I was able to set a breakpoint in the constructor of the service, and record the spl_object_id() of the EM being injected. And I set a second breakpoint in the controller, and recorded a second, different ID for the EM. I have committed my test project for this at https://github.com/kingchills/di-test |
I think I have to revoke my report, don't have time to reinvestigate. |
Locking at 4.2.2 solves this issue for me |
I have also found that locking |
Hello, I have a service I investigated and I found a condition that has been added in the build container since v3.4.21:
Here the v3.4.18 version (it works):
This function is called more than once in the 2 versions. I think (in v3.4.21): when it's called the second time, it recreates the entitymanager then overrides In the v3.4.18, it recreates and overrides all times the Excuse me if I'm not clear, it's difficult to explain. ;) |
@nicolas-grekas Do you have some news about this bug ? |
Just tried symfony/* 4.3-beta2. Still getting two different entity manager instances... |
Found a fix that works well (at least in my applications). In every service, that needs an EntityManager flush to database, I inject the doctrine use Symfony\Bridge\Doctrine\RegistryInterface;
/**
* @var RegistryInterface
*/
protected $doctrine;
public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}
public function generate()
{
$entity = new Entity()
$em = $this->doctrine->getManager();
$em->persist($entity);
$em->flush();
} |
@virtualize I do the same thing: inject the |
@Ioni14 thanks for the hint. Using |
@virtualize You work with v4.2.8 ? |
@MWMathieu yes I do now. Works flawlessly. I removed my composer lock on symfony/dependency-injection at 4.2.2 |
Adding |
Injecting RegistryInterface instead of EntityManagerInterface is just a workaround. In bigger applications it is not possible to refactor all occurences of the EntityManagerInterface. |
Injecting RegistryInterface instead of EntityManagerInterface is less effort than I thought. It only needs one Factory Class:
and some config in services.yaml
But I think it's still a workaround. |
Minimal repro I could get if someone wants to look at it : https://github.com/Lctrs/symfony-issue-30091. |
From what I can see in the dumped container, it's a circular reference issue. We have an initialization order like this : So when the container instantiates the first Another workaround could be to convert all doctrine subscribers to doctrine listeners. It's the recommended ways to do it according to the documentation because doctrine listeners can be lazy loaded, so the issue would not occur with them. |
@Lctrs yes that is also what I mentioned here for a related issue: The question is: why is it not detected as a circular dependency? 😄 |
Because it's not :) |
I should have a failing test case shortly... |
Is there any workaround for this issue while waiting for a fix? |
you could try to make your listeners/services (where the EM is injected) lazy? See #30091 (comment) |
if lazy is the answer now... can we close? |
The issue is not only linked to Listeners. I have it on regular services as well, just using two different services in one controller is enough to have the issue. |
I just moved my project from 4.2.2 to 4.3.2 and ran into this issue too. I'll wait for the fix with the second solution! Even if I don't understand this component's purpose. |
I ran into a Segfault due to PHP running in a loop. I had a |
Somehow the issue disappeared in my own app, both in 4.2.10 and 4.3.2. Maybe it's fixed on Doctrine's side. |
Issue still occurs in my reproducer with 4.2.10 and 4.3.2. |
Any progress on this? Can confirm this still occurs with the latest versions (as of right now) and causes really strange and hard to debug issues. |
…rryan, nicolas-grekas) This PR was merged into the 3.4 branch. Discussion ---------- [DI] Fix dumping Doctrine-like service graphs (bis) | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #30091, #29637 | License | MIT | Doc PR | - Dumping the container while accounting for circular references is hard :) Commits ------- a37f3e0 [DI] Fix dumping Doctrine-like service graphs (bis) ee49144 Failing test case for complex near-circular situation + lazy
Fix is now merged, please report back if you still encounter the issue (and open a new issue with a reproducer please if that happens.) |
Is this also in 4.3.3 now? Or when is it going to be in 4.3 branch? |
Fix will be part of 4.3.4 / 3.4.31 |
I would like to provide you with additionnal feedback: I have been able to reproduce this bug several times on my project. From what I have seen, this may occur when the EntityManager is injected both in a Doctrine EventSubscriber and in other "regular" services. |
Hello there, bug seems fixed with symfony 3.4.31! 👍 Thank you! |
Symfony version(s) affected: 4.2.3
Somehow there are now 2 entity manager instances in my application, causing all sort of WTFs :)
See https://github.com/ro0NL/symfony-issue-30091
Before:
Both entity managers are instance 233
After
Now the instances differ. The only differenence is sf/di 4.2.2 vs 4.2.3. Respectively the first and last commit.
The text was updated successfully, but these errors were encountered: