Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

kraz
Copy link
Contributor

@kraz kraz commented Jul 22, 2025

Fixes #1896
related to symfony/symfony/#61005

@kraz kraz force-pushed the fix-entity-manager-closed-on-worker-mode-and-php-8-4 branch 4 times, most recently from 78a0944 to 086f7f7 Compare July 22, 2025 17:07
@greg0ire
Copy link
Member

Maybe instead of checking with 3 different ways whether the service is lazy, we could obtain that information from the service definition?

@kraz kraz force-pushed the fix-entity-manager-closed-on-worker-mode-and-php-8-4 branch 2 times, most recently from fbf1e6a to 8bbaf52 Compare July 23, 2025 07:04
@kraz
Copy link
Contributor Author

kraz commented Jul 23, 2025

Unfortunately there is no way to detect if object itself is natively lazy. It's not possible to detect it at service container compile time by reading the container definition either. But, we can use the version of the Symfony DI bundle to determine if the lazy services are represented as lazy native objects, because since version 7.3 that's exactly the case. Prior version 7.3 the lazy services are LazyObjectInterface.

I reconsidered the usage of $config->isNativeLazyObjectsEnabled() because it's not meant for that purpose.

After analyzing the changes between 7.2 and 7.3 for:

symfony/dependency-injection 7.2-7.3 diff

I think it's best to check if the following method exists in order to determine if symfony version is >= 7.3:

Symfony\Component\DependencyInjection\ContainerBuilder->findTaggedResourceIds

Then:

For PHP < 8.4 or symfony < 7.3 the logic remains as is:

      if ((! $manager instanceof LazyLoadingInterface && ! $manager instanceof LazyObjectInterface) || $manager->isOpen()) {
          $manager->clear();

          return;
      }

      $this->resetManager($managerName);

Else:

    $r = new ReflectionClass($manager);
    if ($r->isUninitializedLazyObject($manager)) {
        return;
    }

    if ($manager->isOpen()) {
        $manager->clear();

        return;
    }

    $this->resetManager($managerName);

@kraz kraz force-pushed the fix-entity-manager-closed-on-worker-mode-and-php-8-4 branch from 5a369d8 to a162d8e Compare July 23, 2025 10:46
@greg0ire
Copy link
Member

Unfortunately there is no way to detect if object itself is natively lazy.

I'm confused… isn't that exactly what https://www.php.net/manual/de/reflectionclass.isuninitializedlazyobject.php is supposed to achieve?

@ostrolucky
Copy link
Member

IIRC it only checks if it's lazy uninitialized object. If it's initialized then you are out of luck.

@kraz
Copy link
Contributor Author

kraz commented Jul 24, 2025

Yes, that's correct.

After initialization, the object is indistinguishable from an object that was never lazy.

-- PHP RFC: Lazy Objects

Copy link

@aleho aleho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on a bigger project with multiple entity managers and FrankenPHP.

Consecutive requests to the same worker now don't result in "The EntityManager is closed." exceptions any more.

@ostrolucky ostrolucky merged commit 5a305c5 into doctrine:2.15.x Jul 30, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Entity manager is closed on worker mode and PHP 8.4

4 participants