Problem/Motivation
The issue that is happening is that, after updating to Drupal 10.3.0, it was noticed that the session is not being updated. So, if I set the session to 30 minutes, after the user is inactive on the site for that time, they are logged out. In Drupal 10.2.7, every time there was a request, this time was recalculated and updated.
Steps to reproduce
Create the services.yml file and change gc_maxlifetime to 120. Also, change cookie_lifetime to 120. You will be able to use the site normally, but you will be logged out after 2 minutes.
parameters:
session.storage.options:
cookie_samesite: Lax
gc_maxlifetime: 120
cookie_lifetime: 120
Proposed resolution
I think the issue is happening because it's not saving the new session.
Moreover, even forcing it to enter the if, it doesn't work.
https://git.drupalcode.org/project/drupal/-/blob/10.3.0/core/lib/Drupal/...
Issue fork drupal-3468632
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
charlliequadros commentedI created an MR. I'm not sure if it's the best solution, but it solves the problem until someone can fix it properly. I'm available to work on this ticket if anyone can help me figure out a permanent solution to the issue.
Comment #4
cilefen commentedMy expectation is the opposite—the session should end.
Comment #5
rafael maito commentedThank you, charlliequadros. This solution worked for me, adding this change the session is being updated in each request.
Hi cilefen, Is your session never ending?
Comment #6
cilefen commentedThe session should end after
gc_maxlifetime. No?Comment #7
charlliequadros commentedHi @cilefen
In Drupal version 10.2.7, the user's session was updated with each request, meaning that as long as the user interacted with the site, the session time was refreshed, preventing it from expiring during use. However, after updating to version 10.3.0, this behavior changed: the session time is no longer updated. So, if the timeout is reached, the session will be automatically closed, regardless of what the user is doing, such as during content creation or editing, for example. What's not working after the update is the session time being refreshed while the user is interacting with the site.
Comment #8
cilefen commentedAs I mentioned my expectation is the opposite of yours.
Anyway, perform a git bisect on a Git working copy of Drupal core to understand which commit changed this.
Comment #9
charlliequadros commentedHi @cilefen,
I followed your suggestion to identify when the issue was introduced.
This issue arose during the resolution of this issue. https://www.drupal.org/project/drupal/issues/3414287
I couldn’t determine which call was removed to avoid two database requests. Was it "$request->getSession()->save();" or "$this->session->start();
Comment #10
cilefen commentedComment #11
rafael maito commentedHello charlliequadros and cilefen,
After some investigation, here is my conclusion.
This change is related to the big pipe, here (https://www.drupal.org/project/drupal/issues/3414287) is the Drupal issue. So checking this, the changes between Drupal versions 10.2.7 and 10.3.0 in BigPipe were changes to implement enhance performance and security in the delivery of dynamic content on web pages. However, might be affecting how the session is managed, particularly concerning the SSESS cookie's update timing.
In Drupal 10.2.7, BigPipe handled session management by reopening the session during the rendering of placeholders and then closing it afterward. This ensured that the session's expiration time was updated as content was progressively delivered to the user. However, in Drupal 10.3.0, the changes of these pre- and post-rendering tasks may be disrupting the regular update process of the SSESS cookie.
These changes could be preventing the session from being reopened or correctly managed during placeholder rendering, leading to issues with the SSESS cookie's expiration time.
If I revert the changes made here (https://git.drupalcode.org/project/drupal/-/merge_requests/6441/diffs#6c...) in the files BigPipe.php and BigPipeResponse.php, it works as before.
Comment #12
isa.belComment #13
quietone commentedChanges are made on on 11.x (our main development branch) first, and are then back ported as needed according to our policies.
Comment #14
catchComment #15
catchThe session should never be written on every single request, we have specific logic to prevent this
See
WriteSafeSessionHandler::write().What behaviour do you get with big pipe disabled - e.g. does the session timestamp get updated there when it doesn't for big_pipe?
Testing locally with big_pipe enabled, when I first visit the site, my session timestamp gets update, then I click around a bit, and it doesn't get updated for a while, but then if I try again ~6-7 minutes later, the timestamp gets updated again. This is what I'd expect to happen.
This should be the same with or without big_pipe too - or at least that was the intention of #3414287: Avoid reading session from the database multiple times during a big pipe request.
It's possible there's a bug somewhere, but please clarify the steps to reproduce if you can - the session should be updated every few minutes, not every request.
Comment #16
jan kellermann commentedThe same topic is discussed in #2925718. But it seems, that D11 has solved the problem if session_write_interval is set correctly.
Comment #17
znerol commentedI tried to reproduce the issue with the following setup on
11.x:/contact).In order to make the manual testing reproducible, I choose the following options for PHP session gc and Symfony session metadata_update_threshold (the latter is called
session_write_intervalin Drupal and it is a setting):In
sites/default/services.yml:In
sites/default/settings.php:The effect of the PHP
gc_divisor: 1andgc_probability: 1is that gc is run on every request before any session data is read from the database. That means that any session record with a timestamp older than 30 seconds is removed at the start of ever request (except for requests answered by the page cache of course).The effect of the metadata threshold is that a session write is enforced for sessions with a timestamp older than 20 seconds.
With this setup I can confirm that every request which hits the window between the metadata threshold (
session_write_intervaland the gc max lifetime will bump the effective session lifetime by 30 seconds (gc_maxlifetime). This is expected behavior.I can confirm that a session is removed and a user is logged out when I leave a tab open for more than 30 seconds (
gc_maxlifetime) before I reload the browser (though, see the caveat at the end). That is expected behavior as well.In my opinion this issue can be closed. I believe that the behavior observed by the OP is a result of a
gc_maxlifetimevalue lower/equal thansession_write_interval. If there is no window between the two values, the session timestamp is only updated when new session data is written. In cases where a very short session lifetime is required, it is probably best to setsession_write_interval=0. With that setting, sessions will be written on every request - no matter whether the session data has changed or not.For the latter test, I discovered an interesting edge case. For the test to pass there needs to be other activity which triggers the gc in between the requests in the tab left open. If the session record is still in the database when the request starts, then the session record is removed from the database (which is expected), but it is written at the end again and as a result survives the timeout (which is unexpected).
I'm unsure whether or not this edge case needs a fix. With the probabilistic gc in use today, sites cannot rely on session records being deleted reliably after surpassing the gc max lifetime anyway.
Comment #18
znerol commentedI realized, that the OP reported something different:
In 10.2.x, the session cookie is updated if necessary on every request (not yet sure whether this is done by PHP, Symfony or Drupal).
After #3414287: Avoid reading session from the database multiple times during a big pipe request, the call to
\Symfony\Component\HttpFoundation\Session\Session::save()is too late to update the session cookie on big pipe responses. Response headers are already sent when big pipeperformPostSendTasks()is called. I can reproduce this behavior by adding acookie_lifetime: 30container parameter (and still usingsession_write_interval: 20).We could maybe fix that by adding more logic to
\Drupal\Core\StackMiddleware\Session::handle(). E.g., trigger a session save unconditionally when the session timestamp is older thansession_write_interval.Comment #19
znerol commentedThis is a very weird issue. These are my observations so far:
Before #3414287: Avoid reading session from the database multiple times during a big pipe request (
git switch --detach 4e26ae9cc3da8b8a90561716c4a53fdd6c07f8f6):After #3414287: Avoid reading session from the database multiple times during a big pipe request (
git switch --detach 522404706e440106fe7e5d382ce018b0f89637d4):At least this is now consistently working as expected by @cilefen.
Comment #20
andyf commentedI think I might be running into this. I was using a very low
gc_maxlifetimeandgc_divisorset to 1 to test the behavior of an app with a webview when the session expires, and was surprised that no amount of navigating the site from a fresh cache clear could stop me getting logged out after the time has passed.I think the unexpected behavior is that the session ends
gc_maxlifetimeafter the session is created, regardless of how much you use the site within that period (rather than ending a minimum ofgc_maxlifetimeafter the last access).Comment #21
znerol commentedMy observations suggest that the behavior is as follows:
timestampcolumn on the session record in the database is refreshed when the site is used (provided that the user hits an uncached route from time to time and provided that this happens in a time window between$settings['session_write_interval']andgc_maxlifetime#17If that is true, then I'd recommend to keep
cookie_lifetimevalues high and only reduce the parameters which govern the session garbage collection for sites which require expiration of idle sessions.Comment #22
andyf commentedI was just trying to edit my comment to say I didn't have a sufficiently low
settings['session_write_interval'], but the update wouldn't submit...Thanks @znerol for that and your other comments, they'd already pointed me in the right direction. (Although surprisingly with a setup like in #17 I'm finding I'm still logged-in after way more than 30s, will keep playing.)
Comment #23
znerol commentedYou might be running into the edge case documented near the end of #17. If there is only activity in one tab from one user, then the session is not garbage collected.
Comment #24
catchJust a note related to #19 that since #3493911: Add a CachedPlaceholderStrategy to optimize render cache hits and reduce layout shift from big pipe, big pipe previously would have been active for pretty much any request with a session, but it now won't be with warm caches. It doesn't sound like this will make any difference at all here given that big pipe is no longer accidentally mitigating the issue anyway.