Skip to content

[HttpFoundation] Clarify behavior of session access via RequestStack to avoid auto-starting sessions #20891

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

Open
wants to merge 1 commit into
base: 6.4
Choose a base branch
from

Conversation

santysisi
Copy link
Contributor

@santysisi santysisi commented Apr 16, 2025

Symfony’s documentation states that sessions are automatically started when they are accessed (read, written, or even just checked), and it recommends avoiding session access for anonymous users to prevent unnecessary session cookies.

However, calling Request::getSession() or RequestStack::getSession() can throw a SessionNotFoundException if the session has not yet been started. This behavior contradicts the suggestion to "simply avoid accessing the session," since accessing it directly may cause an exception rather than just lazily starting the session as in previous versions where the session was injected via SessionInterface.

For this reason, I believe it makes sense to remove the following line from the documentation:

"Sessions are automatically started whenever you read, write or even check for the existence of data in the session. This may hurt your application performance because all users will receive a session cookie. In order to prevent starting sessions for anonymous users, you must completely avoid accessing the session."

This guidance may no longer reflect the actual behavior, where trying to access a session that doesn't exist now results in an exception, not an automatic creation.

It’s possible that I’m misunderstanding the intended behavior. If that’s the case, I apologize and will be happy to close this pull request. I just wanted to raise the concern in case it helps clarify the documentation.

…to avoid auto-starting sessions

Symfony documentation states that sessions are automatically started when accessed (read/write/check), and recommends avoiding session access for anonymous users to prevent unnecessary session cookies.

However, obtaining the session via `Request::getSession()` or `RequestStack::getSession()` may throw a `SessionNotFoundException` if no session has been started yet — contradicting the suggestion to simply "avoid accessing the session".

This PR adds clarification/code handling to:
- Prevent unintended session creation when checking for session presence
- Avoid potential exceptions when attempting to retrieve a session too early
- Align with Symfony's performance recommendation for anonymous users

I might be misunderstanding the intended behavior, so if that's the case, I apologize and will happily close this PR. Just wanted to raise the concern in case it helps improve clarity.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants