dunglas / frankenphp

šŸ§Ÿ The modern PHP app server
https://frankenphp.dev
MIT License
6.92k stars 240 forks source link

Session handling not working in worker mode #85

Closed htuscher closed 1 year ago

htuscher commented 2 years ago

I'm trying to use a bigger Symfony application in worker mode. If the caches are warm, it generally works as long as no session is used. If I visit actions containing e.g. flash message

<div class="flashbags container">
    {% for type, messages in app.flashes %}
        ...
    {% endfor %}
</div>

I get this:

<b>Fatal error</b>:  Uncaught RuntimeException: Failed to start the session. in /app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:186
Stack trace:
#0 /app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php(352): Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage-&gt;start()
#1 /app/vendor/symfony/http-foundation/Session/Session.php(261): Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage-&gt;getBag('flashes')
#2 /app/vendor/symfony/http-foundation/Session/Session.php(273): Symfony\Component\HttpFoundation\Session\Session-&gt;getBag('flashes')
#3 /app/vendor/symfony/twig-bridge/AppVariable.php(177): Symfony\Component\HttpFoundation\Session\Session-&gt;getFlashBag()
withinboredom commented 2 years ago

Which session driver are you using?

htuscher commented 2 years ago
    session:
        name: session-
        handler_id: null
        cookie_secure: auto
        cookie_samesite: lax
        storage_factory_id: session.storage.factory.native
        enabled: true
        storage_id: session.storage.native
        cookie_httponly: true
        gc_probability: 1
        save_path: /app/var/cache/prod_hd0b46c9c54c1cfce0c444726ba0c1595/sessions
        metadata_update_threshold: 0
withinboredom commented 2 years ago

The following appears to work fine:

session_id(session_create_id());
var_dump(session_start());

Which seems to be about what symfony is doing.

I imagine it is related to the save handler. Does your server's user have write access to /app/var/cache/prod_hd0b46c9c54c1cfce0c444726ba0c1595/sessions? If you can write up a minimal reproduction sample that'd be fantastic.

dunglas commented 2 years ago

What version of Symfony are you using? It looks like session.storage_id doesn't exist. I tried to reproduce the issue, without success so far.

htuscher commented 2 years ago

I'm trying to use the latest version of Shopware https://www.shopware.com/en/download which is based on Symfony 5.4. It does work without the worker mode as a possible hint. I could also provide an instance with SSH access for you if you'd like.

dunglas commented 2 years ago

Do you know if Shopware is already compatible with RoadRunner and similar tools? I wonder if we aren't in the same situation as with Drupal, where a custom session handler that hasn't been designed to be reset is used.

htuscher commented 2 years ago

They are currently moving towards Symfony Flex and Runtime compatibility. I've tested the version which should already be Runtime compatible https://github.com/shopware/recipes/blob/main/shopware/core/6.4/public/index.php respectevly https://github.com/shopware/production/tree/flex They use the regular Symfony sessions. https://github.com/shopware/platform/blob/trunk/src/Core/Framework/Routing/SalesChannelRequestContextResolver.php#L94

shyim commented 2 years ago

Shopware is not running on any of the tools. We started working in that direction by removing state in classes and adding runtime support. For now I would not recommending running such a tool before the entire ecosystem has pushed to this. It could be that extensions holds state which break or show information from previous customer.

Feel free to make pull requests to Shopware to improve the support. As I know also twig globals are only applied once and some variables relating to routing are wrong.

It's not so easy as just switching the runtime šŸ˜€

dunglas commented 1 year ago

Thanks for the info @shyim! Let's close this one for now.

tdumalin commented 11 months ago

Can you explain how did you manage to fix this issue ? I'm using frankenphp with the symfony-docker template and got the exact same issue. In dev session works fine but in production mode session works at container launch, but after navigating on few pages everything breaks and session starts throwing an error.

I've tried to used different handler (null, redis, mysql) and had the same results for all 3.

php-1          | {"message":"Warning: session_start(): User session functions are not defined","context":{"exception":{"class":"ErrorException","message":"Warning: session_start(): User session functions are not defined","code":0,"file":"/app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:185"}},"level":300,"level_name":"WARNING","channel":"php","datetime":"2023-11-29T09:24:58.197360+01:00","extra":{}}
php-1          | {"message":"Warning: session_start(): Failed to initialize storage module: user (path: )","context":{"exception":{"class":"ErrorException","message":"Warning: session_start(): Failed to initialize storage module: user (path: )","code":0,"file":"/app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:185"}},"level":300,"level_name":"WARNING","channel":"php","datetime":"2023-11-29T09:24:58.197418+01:00","extra":{}}
php-1          | {"message":"Uncaught PHP Exception RuntimeException: \"Failed to start the session.\" at /app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php line 186","context":{"exception":{"class":"RuntimeException","message":"Failed to start the session.","code":0,"file":"/app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:186"}},"level":500,"level_name":"CRITICAL","channel":"request","datetime":"2023-11-29T09:24:58.197581+01:00","extra":{}}
php-1          | {"message":"Warning: session_start(): User session functions are not defined","context":{"exception":{"class":"ErrorException","message":"Warning: session_start(): User session functions are not defined","code":0,"file":"/app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:185"}},"level":300,"level_name":"WARNING","channel":"php","datetime":"2023-11-29T09:24:58.199112+01:00","extra":{}}
php-1          | {"message":"Warning: session_start(): Failed to initialize storage module: user (path: )","context":{"exception":{"class":"ErrorException","message":"Warning: session_start(): Failed to initialize storage module: user (path: )","code":0,"file":"/app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:185"}},"level":300,"level_name":"WARNING","channel":"php","datetime":"2023-11-29T09:24:58.199248+01:00","extra":{}}
php-1          | {"message":"Exception thrown when handling an exception (RuntimeException: Failed to start the session. at /app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php line 186)","context":{"exception":{"class":"RuntimeException","message":"Failed to start the session.","code":0,"file":"/app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:186"}},"level":500,"level_name":"CRITICAL","channel":"request","datetime":"2023-11-29T09:24:58.199566+01:00","extra":{}}
php-1          | {"level":"error","ts":1701246298.1998334,"msg":"PHP Fatal error:  Uncaught RuntimeException: Failed to start the session. in /app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php:186
Stack trace:
#0 /app/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php(352): Symfony\\Component\\HttpFoundation\\Session\\Storage\\NativeSessionStorage->start()
#1 /app/vendor/symfony/http-foundation/Session/Session.php(261): Symfony\\Component\\HttpFoundation\\Session\\Storage\\NativeSessionStorage->getBag()
#2 /app/vendor/symfony/http-foundation/Session/Session.php(283): Symfony\\Component\\HttpFoundation\\Session\\Session->getBag()
#3 /app/vendor/symfony/http-foundation/Session/Session.php(77): Symfony\\Component\\HttpFoundation\\Session\\Session->getAttributeBag()\n#4 /app/vendor/sonata-project/page-bundle/src/CmsManager/CmsManagerSelector.php(81): Symfony\\Component\\HttpFoundation\\Session\\Session->get()
#5 /app/vendor/sonata-project/page-bundle/src/CmsManager/CmsManagerSelector.php(67): Sonata\\PageBundle\\CmsManager\\CmsManagerSelector->isEditor()
#6 /app/vendor/sonata-project/page-bundle/src/Route/CmsPageRouter.php(141): Sonata\\PageBundle\\CmsManager\\CmsManagerSelector->retrieve()
#7 /app/vendor/symfony-cmf/routing/src/ChainRouter.php(192): Sonata\\PageBundle\\Route\\CmsPageRouter->match()
#8 /app/vendor/symfony-cmf/routing/src/ChainRouter.php(158): Symfony\\Cmf\\Component\\Routing\\ChainRouter->doMatch()
#9 /app/vendor/symfony/http-kernel/EventListener/RouterListener.php(111): Symfony\\Cmf\\Component\\Routing\\ChainRouter->matchRequest()
#10 /app/vendor/symfony/event-dispatcher/EventDispatcher.php(230): Symfony\\Component\\HttpKernel\\EventListener\\RouterListener->onKernelRequest()
#11 /app/vendor/symfony/event-dispatcher/EventDispatcher.php(59): Symfony\\Component\\EventDispatcher\\EventDispatcher->callListeners()
#12 /app/vendor/symfony/http-kernel/HttpKernel.php(139): Symfony\\Component\\EventDispatcher\\EventDispatcher->dispatch()
#13 /app/vendor/symfony/http-kernel/HttpKernel.php(75): Symfony\\Component\\HttpKernel\\HttpKernel->handleRaw()
#14 /app/vendor/symfony/http-kernel/Kernel.php(202): Symfony\\Component\\HttpKernel\\HttpKernel->handle()
#15 /app/vendor/runtime/frankenphp-symfony/src/Runner.php(35): Symfony\\Component\\HttpKernel\\Kernel->handle()
#16 [internal function]: Runtime\\FrankenPhpSymfony\\Runner->Runtime\\FrankenPhpSymfony\\{closure}()
#17 /app/vendor/runtime/frankenphp-symfony/src/Runner.php(30): frankenphp_handle_request()
#18 /app/vendor/autoload_runtime.php(35): Runtime\\FrankenPhpSymfony\\Runner->run()
#19 /app/public/index.php(5): require_once('...')

I can't debug because when i tried to edit the files on the container, it doesn't change anything, If i well understood the behavior of frankenphp, the app is running on the memory, Is there any way to edit the project and "update" the memory with the new edited files ?