-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] AbstractSessionListener saves session for stateless request #50958
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
Comments
You may have misunderstood stateless routes: they won't prevent the session being used, but will warn you if so: https://symfony.com/doc/current/routing.html#stateless-routes |
No, I understand that. But I'm not using the session, neither is Symfony, as you can see from the stack trace. So why does it need to be saved and how else can I prevent that? Why does the warning/exception come after instead of before the save? |
From https://symfony.com/doc/current/session.html
The stack trace shows that |
I didn't read or write to the session. This is the whole controller (for Sentry JS tunnel). It dispatches async message through SQS messenger. #[Route(
path: '/api/sentry',
name: RouteName::API_SENTRY_TUNNEL,
methods: ['POST'],
stateless: true,
)]
#[IsGranted('PUBLIC_ACCESS')]
public function __invoke(Request $request): Response
{
$this->messageBus->dispatch(new SendSentryEvent($request->getContent()));
return new Response();
} Maybe |
Pretty sure this is the case if your route is under a stateful firewall (because the token will be fetched from the session). |
I had a suspicion it could be related to the firewall... That solved it. I added an additional stateless firewall for my /api routes: 'api' => [
'stateless' => true,
'pattern' => '^/api',
], That wasn't enough though. I have one event subscriber that still called the session, because I added another check there, and now it's working without the "session used on stateless route" warning: if ($this->security->getFirewallConfig($request)?->isStateless() ?? false) {
return;
} Thanks for the help @MatTheCat! |
Uh oh!
There was an error while loading. Please reload this page.
Symfony version(s) affected
6.3.1
Description
I've been seeing some
session_write_close(): Failed to write session data with "Symfony\Component\HttpFoundation\Session\Storage\Handler\RedisSessionHandler" handler
warnings in my Sentry for a specific route.This is probably caused by Redis sessions being non-locking (fingers crossed for #4976).
So I set this route to
stateless: true
, but it still happens.This route receives an AJAX request during a "normal" pageview. It does not itself use the session.
But as it is an AJAX request, the request includes the session cookie, which is probably why AbstractSessionListener.php#L95 and AbstractSessionListener.php#L108 evaluate to true.
I think this case might have been missed when
_stateless
was introduced in #35732.How to reproduce
I wrote this test case for SessionListenerTest, which I believe should pass.
Currently, it fails with
Symfony\Component\HttpFoundation\Session\Session::save() was not expected to be called.
.Possible Solution
Don't save the session if the request is stateless.
The whole check for stateless request could be moved to the top of the listener.
I don't understand why the session should be saved in a stateless request, but there is a test case for it in SessionListenerTest.php#L785.
Maybe the exception/warning for using the session in a stateless request should be thrown without actually saving the session?
Additional Context
The text was upda 8000 ted successfully, but these errors were encountered: