symfony: Cannot modify readonly property Doctrine\ORM\EntityManager::$conn
Symfony version(s) affected
7.0.5
Description
Running Symfony 7.0 on frankenphp, I have upgraded to Doctrine ORM 3 and DBAL 4, and I intermittently get Cannot modify readonly property Doctrine\ORM\EntityManager::$conn" , which at first sight is pretty strange, because the constructor fails setting the initial value of $conn. Most probably, due to LazyGhostTrait.
Example in an HTTP request:
Uncaught PHP Exception Error: "Cannot modify readonly property Doctrine\ORM\EntityManager::$conn" at EntityManager.php line 114 {"exception":"[object] (Error(code: 0): Cannot modify readonly property Doctrine\\ORM\\EntityManager::$conn at /app/site/vendor/doctrine/orm/src/EntityManager.php:114)
[stacktrace]
#0 /app/site/var/cache/prod/ContainerCBsis9p/App_KernelProdContainer.php(391): Doctrine\\ORM\\EntityManager->__construct()
#1 /app/site/var/cache/prod/ContainerCBsis9p/App_KernelProdContainer.php(349): ContainerCBsis9p\\App_KernelProdContainer::getDoctrine_Orm_DefaultEntityManagerService()
#2 /app/site/vendor/symfony/var-exporter/Internal/LazyObjectState.php(61): ContainerCBsis9p\\App_KernelProdContainer::ContainerCBsis9p\\{closure}()
#3 /app/site/vendor/symfony/var-exporter/LazyGhostTrait.php(118): Symfony\\Component\\VarExporter\\Internal\\LazyObjectState->initialize()
#4 /app/site/vendor/doctrine/orm/src/EntityManager.php(580): ContainerCBsis9p\\EntityManagerGhost6410e2e->__get()
#5 /app/site/vendor/doctrine/orm/src/Query.php(661): Doctrine\\ORM\\EntityManager->hasFilters()
#6 /app/site/vendor/doctrine/orm/src/Query.php(221): Doctrine\\ORM\\Query->getQueryCacheId()
#7 /app/site/vendor/doctrine/orm/src/Query.php(245): Doctrine\\ORM\\Query->parse()
#8 /app/site/vendor/doctrine/orm/src/AbstractQuery.php(935): Doctrine\\ORM\\Query->_doExecute()
#9 /app/site/vendor/doctrine/orm/src/AbstractQuery.php(891): Doctrine\\ORM\\AbstractQuery->executeIgnoreQueryCache()
#10 /app/site/vendor/doctrine/orm/src/AbstractQuery.php(738): Doctrine\\ORM\\AbstractQuery->execute()
#11 /app/site/src/Repository/Doctrine/PerformanceDoctrineRepository.php(67): Doctrine\\ORM\\AbstractQuery->getOneOrNullResult()
#12 /app/site/src/Service/PerformanceService.php(49): App\\Repository\\Doctrine\\PerformanceDoctrineRepository->findById()
#13 /app/site/src/Controller/PerformancePageController.php(47): App\\Service\\PerformanceService->findById()
#14 /app/site/vendor/symfony/http-kernel/HttpKernel.php(178): App\\Controller\\PerformancePageController->__invoke()
#15 /app/site/vendor/symfony/http-kernel/HttpKernel.php(76): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw()
#16 /app/site/vendor/symfony/http-kernel/Kernel.php(185): Symfony\\Component\\HttpKernel\\HttpKernel->handle()
#17 /app/site/vendor/runtime/frankenphp-symfony/src/Runner.php(38): Symfony\\Component\\HttpKernel\\Kernel->handle()
#18 [internal function]: Runtime\\FrankenPhpSymfony\\Runner->Runtime\\FrankenPhpSymfony\\{closure}()
#19 /app/site/vendor/runtime/frankenphp-symfony/src/Runner.php(45): frankenphp_handle_request()
#20 /app/site/vendor/autoload_runtime.php(29): Runtime\\FrankenPhpSymfony\\Runner->run()
#21 /app/site/public/index.php(5): require_once('...')
#22 {main}
Example in a MessageHandler:
Fatal Error: Uncaught Error: Cannot modify readonly property Doctrine\ORM\EntityManager::$conn in /app/site/vendor/doctrine/orm/src/EntityManager.php:114
Stack trace:
#0 /app/site/var/cache/prod/ContainerCBsis9p/App_KernelProdContainer.php(391): Doctrine\ORM\EntityManager->__construct()
#1 /app/site/var/cache/prod/ContainerCBsis9p/App_KernelProdContainer.php(349): ContainerCBsis9p\App_KernelProdContainer::getDoctrine_Orm_DefaultEntityManagerService()
#2 /app/site/vendor/symfony/var-exporter/Internal/LazyObjectState.php(61): ContainerCBsis9p\App_KernelProdContainer::ContainerCBsis9p\{closure}()
#3 /app/site/vendor/symfony/var-exporter/LazyGhostTrait.php(118): Symfony\Component\VarExporter\Internal\LazyObjectState->initialize()
#4 /app/site/vendor/doctrine/orm/src/EntityManager.php(529): ContainerCBsis9p\EntityManagerGhost6410e2e->__get()
#5 /app/site/vendor/doctrine/doctrine-bundle/Registry.php(80): Doctrine\ORM\EntityManager->isOpen()
#6 /app/site/vendor/doctrine/doctrine-bundle/Registry.php(66): Doctrine\Bundle\DoctrineBundle\Registry->resetOrClearManager()
#7 /app/site/vendor/symfony/http-kernel/DependencyInjection/ServicesResetter.php(57): Doctrine\Bundle\DoctrineBundle\Registry->reset()
#8 /app/site/vendor/symfony/http-kernel/Kernel.php(111): Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter->reset()
#9 /app/site/src/Kernel.php(32): Symfony\Component\HttpKernel\Kernel->boot()
#10 /app/site/vendor/symfony/http-kernel/Kernel.php(180): App\Kernel->boot()
#11 /app/site/vendor/runtime/frankenphp-symfony/src/Runner.php(38): Symfony\Component\HttpKernel\Kernel->handle()
#12 [internal function]: Runtime\FrankenPhpSymfony\Runner->Runtime\FrankenPhpSymfony\{closure}()
#13 /app/site/vendor/runtime/frankenphp-symfony/src/Runner.php(45): frankenphp_handle_request()
#14 /app/site/vendor/autoload_runtime.php(29): Runtime\FrankenPhpSymfony\Runner->run()
#15 /app/site/public/index.php(5): require_once('...')
#16 {main}
thrown {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Error: Uncaught Error: Cannot modify readonly property Doctrine\\ORM\\EntityManager::$conn in /app/site/vendor/doctrine/orm/src/EntityManager.php:114
Stack trace:
#0 /app/site/var/cache/prod/ContainerCBsis9p/App_KernelProdContainer.php(391): Doctrine\\ORM\\EntityManager->__construct()
#1 /app/site/var/cache/prod/ContainerCBsis9p/App_KernelProdContainer.php(349): ContainerCBsis9p\\App_KernelProdContainer::getDoctrine_Orm_DefaultEntityManagerService()
#2 /app/site/vendor/symfony/var-exporter/Internal/LazyObjectState.php(61): ContainerCBsis9p\\App_KernelProdContainer::ContainerCBsis9p\\{closure}()
#3 /app/site/vendor/symfony/var-exporter/LazyGhostTrait.php(118): Symfony\\Component\\VarExporter\\Internal\\LazyObjectState->initialize()
#4 /app/site/vendor/doctrine/orm/src/EntityManager.php(529): ContainerCBsis9p\\EntityManagerGhost6410e2e->__get()
#5 /app/site/vendor/doctrine/doctrine-bundle/Registry.php(80): Doctrine\\ORM\\EntityManager->isOpen()
#6 /app/site/vendor/doctrine/doctrine-bundle/Registry.php(66): Doctrine\\Bundle\\DoctrineBundle\\Registry->resetOrClearManager()
#7 /app/site/vendor/symfony/http-kernel/DependencyInjection/ServicesResetter.php(57): Doctrine\\Bundle\\DoctrineBundle\\Registry->reset()
#8 /app/site/vendor/symfony/http-kernel/Kernel.php(111): Symfony\\Component\\HttpKernel\\DependencyInjection\\ServicesResetter->reset()
#9 /app/site/src/Kernel.php(32): Symfony\\Component\\HttpKernel\\Kernel->boot()
#10 /app/site/vendor/symfony/http-kernel/Kernel.php(180): App\\Kernel->boot()
#11 /app/site/vendor/runtime/frankenphp-symfony/src/Runner.php(38): Symfony\\Component\\HttpKernel\\Kernel->handle()
#12 [internal function]: Runtime\\FrankenPhpSymfony\\Runner->Runtime\\FrankenPhpSymfony\\{closure}()
#13 /app/site/vendor/runtime/frankenphp-symfony/src/Runner.php(45): frankenphp_handle_request()
#14 /app/site/vendor/autoload_runtime.php(29): Runtime\\FrankenPhpSymfony\\Runner->run()
#15 /app/site/public/index.php(5): require_once('...')
#16 {main}
thrown at /app/site/vendor/doctrine/orm/src/EntityManager.php:114)
[stacktrace]
#0 {main}
How to reproduce
I have no idea how to reproduce it. It happens once in a while, seemingly random. E.g. a failed HTTP request will succeed afterwards, probably related to the usage of frankenphp.
UPDATE: It seems to happen in the request right after a request that closes the EntityManager, e.g. due to Doctrine\\DBAL\\Exception\\UniqueConstraintViolationException
Possible Solution
No response
Additional Context
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 37 (20 by maintainers)
https://github.com/doctrine/orm/releases/tag/3.2.0 is released, this can be closed 🙂
I submitted https://github.com/doctrine/orm/pull/11472 to remove readonly from the EM.
For completion’s sake - with ORM 3.2.0 the reproducer runs as it should, so issue indeed fundamentally solved for now.
I agree with the above comment: the entity manager is not ready to use readonly properties, because by doing so it breaks legit use cases that need to reset it without providing any viable alternative.
Note that to be accurate, by “Lazy”, @curry684 means “lazy ghosts”. Those are capable of lazy-loading a readonly property, but they’re not capable of resetting them. The other lazy strategy (virtual proxies) is capable of that reset even for readonly properties, but it comes with side effects embedded, which might be more nasty (possible significant perf impact and possible identity problems).
Removing those readonly modifiers would definitely solve the issue (with the same reasoning as why
@finalis used and not the realfinalmodifier).Thanks!
Both comments above are relevant to the issue but not related to the root cause. The error indeed always occurs when any error at all has caused the EntityManager to close. In my application it happens when Azure Graph API gives a transient error (it gives a 503 Service Unavailable once every few days) or a sync job fails with an external API that throws random errors at infrequent intervals like 10 times per week.
When an exception is thrown and not caught during a Doctrine operation the EntityManager is rightfully closed - the state of the entities is no longer reliable or predictable. The message is, also correctly, queued for retry in default configuration.
What happens is that the message is immediately retried but Messenger cannot reopen the EntityManager due to the readonly property. This causes a lower level exception, crashing the
messenger:consumecommand and restarting the entire worker process, which gets a fresh EntityManager inherently.So the true issue is still that Messenger is unable to reset the EntityManager. Avoiding exceptions in your message handlers prevents running into this issue but should not be necessary.
A lazy and reset-able proxy for the EM could be built on top of the
EntityManagerDecoratorclass. That EM would not pass a$em instanceof EntityManageranymore, but that’s kind of why we have an interface for the EM.Here is the actual smallest possible reproducer: https://github.com/curry684/emconn-reproducer
@derrabus actually I doubt that this would work. The virtual proxy strategy is essentially the best possible implementation of what you’re saying, but still, there is one major issue (if we ignore the perf overhead): identity. The EM passes itself to many of its dependencies, and these dependencies will get the real instance instead of the proxy in many situations. This can lead to terrible situations where two entity manager objects coexist and nobody controls which one is at hand. This happened before and will happen with the decoration strategy. Ghost objects are free from this issue. Yet, readonly props are blocking. Their benefit doesn’t outweigh the benefit of being able to use ghost objects. Removing them looks like the only solution to me, at least short term, until there is a native way to reset the EM. (on my side, I’m fine with ghost objects doing the job, that’s less work for Doctrine maintainers.)
Ok I did some debugging, got totally stumped and had a good talk with @nicolas-grekas on the subject. There’s a few different things at play here leading to a confusing picture:
readonlyproperties are not fit to be Lazy Ghosts.The actual crash occurs at https://github.com/doctrine/DoctrineBundle/blob/2.12.x/src/Registry.php#L80, but is just an ugly symptom: the
isOpencall forces the Lazy object to reinitialize, which it cannot do because ofreadonlyproperties. The check is however required for normal functionality when manually resetting an entity manager that may or may not have been closed before. So cannot fix it there due to catch-22.The best fix for now is likely on the ORM side to revert (most of) this PR: https://github.com/doctrine/orm/pull/10440. The EM is currently simply not capable of having
readonlymembers, even though the change is correct from an architectural perspective.On the other hand the Lazy code should throw a better error here if we can, so the issue is also relevant to Symfony.
/ping @greg0ire, would love to hear your insights here!
edit: clarified ‘lazy ghost’, thnx