Generated with the help of an LLM.
Problem/Motivation
Since updating from 11.3.19 to 11.4.11 we see a recurring error in production that did not exist prior to the update:
Error: Call to a member function getSession() on null in
Drupal\Core\Session\SessionHandler::doWrite()
(line 74 of core/lib/Drupal/Core/Session/SessionHandler.php)
#3 .../core/lib/Drupal/Core/Session/SessionHandler.php(80): ...->{closure}()
#2 .../core/lib/Drupal/Core/Session/WriteSafeSessionHandler.php(76): write()
#1 [internal function]: session_write_close()
#0 {main}
Cause: #2613044: Requests are pushed onto the request stack twice, popped once made DrupalKernel::terminate() pop the main request off the request stack. Before 11.4 that request stayed on the stack until the process ended. When PHP's shutdown saves a session that is still open, SessionHandler::doWrite() asks the stack for the current request so it can stamp uid and hostname on the row, and never checks for null.
For us this predominantly occurs following a 403 from our OIDC module's failed-login callback which leaves an open, changed session.
Impact: Nothing is user-visible, but the late session data is silently lost (before 11.4 these writes succeeded), the fatal is logged every time, and it stops any later shutdown functions from running.
Steps to reproduce
Add a throwaway http_middleware with priority between Session (50) and KernelPreHandle (100) that changes the session after the inner kernel returns:
public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = TRUE): Response {
$response = $this->httpKernel->handle($request, $type, $catch);
if ($request->cookies->count() && $request->hasSession()) {
$request->getSession()->set('probe', time());
}
return $response;
}
Visit any page while logged in. On 11.4.x PHP's shutdown save produces the exact fatal above. On 11.3.x the same write succeeds.
Proposed resolution
Move the Session stack middleware's save() into the finally leg of a try/finally around the inner kernel call, so the session is saved even when the kernel throws. See comment #6.
$result = NULL; try { $result = $this->httpKernel->handle($request, $type, $catch); } finally { if ($type === self::MAIN_REQUEST && PHP_SAPI !== 'cli' && !$result instanceof ResponseKeepSessionOpenInterface) { $request->getSession()->save(); } } return $result;
| Comment | File | Size | Author |
|---|---|---|---|
| #7 | session-save-in-finally-3634400-7.patch | 973 bytes | codebaboon |
| bt5477-upstream-issue-full-analysis.txt | 9.78 KB | codebaboon |
Issue fork drupal-3634400
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 #2
cilefen commentedComment #3
cilefen commentedComment #4
codebaboon commentedA a follow-up here are steps to reproduce through the standard OpenID Connect callback, with no custom code involved.
On a site running 11.4.x, with a configured OIDC provider (I used FusionAuth):
This scenario, while contrived, simulates something similar to a delayed login exchange where the code expires before being handed off. The module takes its failure path for the stale state, the request ends in an error response, and the session is left open and modified. PHP's shutdown session_write_close() then runs against the empty request stack and fatals with the exact stack trace in the summary:
Comment #5
nicxvan commentedFixes are made on main and backported as necessary.
Comment #6
ghost of drupal pastno need for a slop title
Suggested fixes:
Hardwiring a session close into DrupalKernel is a very bad idea. Do not hardwire things into the poor kernel.
Comment #7
codebaboon commentedre #6: thanks, you were right. I moved the save into a try/finally in the Session middleware as you suggested, and it resolves the error.
In
drupal/html/core/lib/Drupal/Core/StackMiddleware/Session.php:See attached patch, I've updated the summary Proposed Resolution to match this suggestion.
Comment #8
nicxvan commentedThank you, please create an issue fork here so that tests can run and we can review it properly.
Comment #9
cilefen commentedComment #10
ghost of drupal pastAlso please add the documentation on the session middleware.
Comment #12
codebaboon commentedI have opened MR !17865 against main. This merge request includes the session middleware fix, documentation asked for by ghost of drupal past in #10, and also a test that will fail if this fix is not in place.
A backport to `11.4.x` would be appreciated, as we hit this in production and have needed to introduce a composer patch in the interim.
Generated with the help of an LLM.