Skip to content
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

Workers sharing logged users #118

Open
FluffyDiscord opened this issue Jun 21, 2023 · 8 comments
Open

Workers sharing logged users #118

FluffyDiscord opened this issue Jun 21, 2023 · 8 comments

Comments

@FluffyDiscord
Copy link
Contributor

FluffyDiscord commented Jun 21, 2023

In production, the Symfony somehow loads different logged user than myself. This is major bug. I suspect it's because of sessions not being closed/refresh between requests. I remember this bundle having middleware that would deal with that, but it was removed recently and I am not sure why.

I run RR with 16 workers, so worker 1 has logged user A, worker 2 has user B, and when user B get worker 1, it instead loads the worker 1 user and voila, I am in a different user account.

What do I do?

@Baldinof
Copy link
Owner

Normally sessions should work out of the box now with Symfony.

What version of Symfony are you using?

Can you reproduce on a fresh Symfony install?

@FluffyDiscord
Copy link
Contributor Author

Using symfony 6.3 I have hard time reproducing it on production too, but it does happen as clients reported this multiple times and today that happened to me too

@Baldinof
Copy link
Owner

Are you using a custom session handler?

@FluffyDiscord
Copy link
Contributor Author

FluffyDiscord commented Jun 22, 2023

this is the whole framework.yaml

# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
    http_method_override: false
    trusted_proxies: '127.0.0.1/8,REMOTE_ADDR,SERVER_NAME'
    trusted_headers: [ 'x-forwarded-for', 'x-forwarded-host', 'x-forwarded-proto', 'x-forwarded-port', 'x-forwarded-prefix' ]
    secret: '%env(APP_SECRET)%'
    handle_all_throwables: true
    #csrf_protection: true
    #http_method_override: true

    # Enables session support. Note that the session will ONLY be started if you read or write from it.
    # Remove or comment this section to explicitly disable session support.
    session:
        storage_factory_id: session.storage.factory.native
        handler_id: session.handler.native_file
        save_path: '%kernel.project_dir%/sessions/%kernel.environment%'
        cookie_secure: auto
        cookie_samesite: lax
        cookie_lifetime: 31536000 # one year in seconds

    #esi: true
    #fragments: true
    php_errors:
        log: true

@webspec2012
Copy link

Maybe this is also related to my problem?

#114

@Baldinof
Copy link
Owner

I'll try to give it some time, it's strange 😬

@Baldinof
Copy link
Owner

I tried to had a look with no luck.

I tried on a fresh project, using in-memory or doctrine as user storage and everything was working fine (with one or many workers).

Are you able to provide a reproducer repository? I would gladely help as it seems pretty serious but I'm not able to reproduce it on a fresh project.

@FluffyDiscord
Copy link
Contributor Author

FluffyDiscord commented Jun 26, 2023

I have added the following middleware, and so far it seems fine. Since the ServicesResetter does not register all services that can be reset (my own, internal symfony's, bundles, eg.) combining it with iterating over the private container services seems to finally work as intended.

<?php

namespace App\RoadRunner\Middleware;

use Baldinof\RoadRunnerBundle\Http\MiddlewareInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Contracts\Service\ResetInterface;

readonly class ServiceReSetterMiddleware implements MiddlewareInterface
{
    public function __construct(
        #[Autowire("@service_container")]
        private ContainerInterface $container,

        #[Autowire("@services_resetter")]
        private ServicesResetter   $servicesResetter,
    )
    {
    }

    public function process(Request $request, HttpKernelInterface $next): \Iterator
    {
        yield $next->handle($request);

        $this->servicesResetter->reset();

        $ref = new \ReflectionClass($this->container);
        $prop = $ref->getProperty("privates");
        $services = $prop->getValue($this->container);

        foreach ($services as $service) {
            if ($service instanceof ResetInterface) {
                $service->reset();
            }
        }
    }
}

I would live to add reproducer repo, but I cant myself easily reproduce it, it was really random.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants