Symfony app crashes during XDebug session at EntityManager proxy, but manual evaluation works

I’m experiencing a strange debugging issue with my Symfony web application where XDebug causes a crash, but manually evaluating the same code works fine.

Problem Description

When debugging my Symfony application with XDebug in PHPStorm, the application crashes when execution reaches var/cache/dev/ContainerJCxBVLf/EntityManager_9a5be93.php at line 225 (specifically at the getRepository() call).

However, when I manually evaluate $this->getRepository(); in the PHPStorm Debug Evaluator at the exact same breakpoint in SymfonyBridgeDoctrineSecurityUserEntityUserProvider->refreshUser(), it executes successfully and the application continues normally.

Stack Trace

EntityManager_9a5be93.php:225, ContainerJCxBVLfEntityManager_9a5be93->getRepository()
EntityUserProvider.php:139, SymfonyBridgeDoctrineSecurityUserEntityUserProvider->getRepository()
EntityUserProvider.php:84, SymfonyBridgeDoctrineSecurityUserEntityUserProvider->refreshUser()
ContextListener.php:216, SymfonyComponentSecurityHttpFirewallContextListener->refreshUser()
ContextListener.php:131, SymfonyComponentSecurityHttpFirewallContextListener->authenticate()
WrappedLazyListener.php:49, SymfonyBundleSecurityBundleDebugWrappedLazyListener->authenticate()
AbstractListener.php:26, SymfonyComponentSecurityHttpFirewallAbstractListener->__invoke()
TraceableFirewallListener.php:62, SymfonyBundleSecurityBundleDebugTraceableFirewallListener->callListeners()
Firewall.php:86, SymfonyComponentSecurityHttpFirewall->onKernelRequest()
WrappedListener.php:117, SymfonyComponentEventDispatcherDebugWrappedListener->__invoke()
EventDispatcher.php:230, SymfonyComponentEventDispatcherEventDispatcher->callListeners()
EventDispatcher.php:59, SymfonyComponentEventDispatcherEventDispatcher->dispatch()
TraceableEventDispatcher.php:151, SymfonyComponentEventDispatcherDebugTraceableEventDispatcher->dispatch()
HttpKernel.php:133, SymfonyComponentHttpKernelHttpKernel->handleRaw()
HttpKernel.php:79, SymfonyComponentHttpKernelHttpKernel->handle()
Kernel.php:195, SymfonyComponentHttpKernelKernel->handle()
index.php:78, {main}()

Suspicious Code in Generated Proxy

I noticed that the generated cache file EntityManager_9a5be93.php appears to have a potential issue. The variable $valueHolder68094 is used without being defined in the local scope:

<?php
namespace ContainerJCxBVLf;
include_once dirname(__DIR__, 4).'/vendor/doctrine/persistence/src/Persistence/ObjectManager.php';
include_once dirname(__DIR__, 4).'/vendor/doctrine/orm/src/EntityManagerInterface.php';
include_once dirname(__DIR__, 4).'/vendor/doctrine/orm/src/EntityManager.php';

class EntityManager_9a5be93 extends DoctrineORMEntityManager implements ProxyManagerProxyVirtualProxyInterface
{
    /**
     * @var DoctrineORMEntityManager|null wrapped object, if the proxy is initialized
     */
    private $valueHolder68094 = null;

    /**
     * @var Closure|null initializer responsible for generating the wrapped object
     */
    private $initializerd5058 = null;

    /**
     * @var bool[] map of public properties of the parent class
     */
    private static $publicProperties034fb = [

    ];

    // ...

    public function getRepository($entityName)
    {
        $this->initializerd5058 && ($this->initializerd5058->__invoke($valueHolder68094, $this, 'getRepository', array('entityName' => $entityName), $this->initializerd5058) || 1) && $this->valueHolder68094 = $valueHolder68094;

        return $this->valueHolder68094->getRepository($entityName);
    }

    // ...
}

Notice that in each method, $valueHolder68094 is passed to the initializer closure by reference, but it’s not defined in the method’s local scope.

Environment

  • PHP Version: 8.1.33
  • XDebug Version: 3.4.5
  • Symfony Components: v5.2.x
    • symfony/cache: v5.2.0
    • doctrine/orm: 2.20.6
    • doctrine/doctrine-bundle: 2.7.0

What I’ve tried

  • Clearing the Symfony cache (the whole cache folder)
  • Manually evaluating the code in the Debug Evaluator (which works)

Question

Why does the Symfony application crash during XDebug debugging at the EntityManager proxy’s getRepository() call, but executing the same method manually in the Debug Evaluator works fine? Is this a known issue with proxy generation, XDebug compatibility, or a configuration problem?