"Call to a member function hasSession() on null" in SharedTempStoreFactory

Created on 7 November 2023, about 1 year ago
Updated 17 November 2023, about 1 year ago

Problem/Motivation

An error occurred in the Drupal site when the system attempted to call the `hasSession()` method on a null object within the `SharedTempStoreFactory` service. This issue seems to manifest particularly on cached pages when handling session data for anonymous users, where the request object is not properly instantiated.

The website encountered an unexpected error. Please try again later.
Error: Call to a member function hasSession() on null in Drupal\Core\TempStore\SharedTempStoreFactory->get() (line 103 of core/lib/Drupal/Core/TempStore/SharedTempStoreFactory.php).
Drupal\Core\TempStore\SharedTempStoreFactory->get('mercury') (Line: 185)
Drupal\mercury\Mercury->__construct(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) (Line: 259)
Drupal\Component\DependencyInjection\Container->createService(Array, 'mercury.mercury') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('mercury.mercury', 1) (Line: 434)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 237)
Drupal\Component\DependencyInjection\Container->createService(Array, 'contact_preferences.contact_preferences_form_helper') (Line: 177)
Drupal\Component\DependencyInjection\Container->get('contact_preferences.contact_preferences_form_helper') (Line: 89)
Drupal\Core\Form\FormBase->__wakeup()
Memcached->getMulti(Array, 1) (Line: 71)
Drupal\memcache\Driver\MemcachedDriver->getMulti(Array) (Line: 144)
Drupal\memcache\MemcacheBackend->getMultiple(Array, ) (Line: 136)
Drupal\memcache\MemcacheBackend->get('http://website.docksal.site/brochures:', ) (Line: 306)
Drupal\page_cache\StackMiddleware\PageCache->get(Object) (Line: 124)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 718)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Steps to reproduce

This issue appears to occur for anonymous users with no existing session, visiting a cached page that relies on request or url generator dependencies.
In my use case, this error was encountered after resolving an issue where a cached page with a URL generator dependency fails to retrieve a valid request ( https://www.drupal.org/project/drupal/issues/3243387 πŸ› Argument 1 passed to Drupal\Core\Routing\RequestContext::fromRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given Closed: cannot reproduce ) due to a missing request stack. Upon applying the patch, I was presented with this new error.
This issue appears to have a similar cause - with no request stack, there is no request, and thus `hasSession()` is called on NULL.

Proposed resolution

A patch has been created to address this issue by adding a check to ensure that the request object exists before attempting to access its `hasSession()` method. This precautionary step should avoid encountering a null reference when the request stack lacks an instantiated request object, which might happen in certain asynchronous operations or CLI contexts.

Remaining tasks

The proposed patch needs to be reviewed and tested by the community to confirm that it resolves the issue without introducing new problems. Further testing, especially in environments where the error was previously encountered, would be beneficial.

User interface changes

There are no user interface changes associated with this patch as it deals with backend session handling.

API changes

There are no API changes as the proposed solution maintains the existing API behavior, merely adding a null check before method invocation.

Data model changes

No data model changes are needed for this patch as it does not alter the structure or representation of data within the system.

Release notes snippet

Fixed an issue where an attempt to call `hasSession()` on a null object in `SharedTempStoreFactory` could lead to an unexpected site error, particularly for anonymous sessions. The patch ensures that there is a valid request object before the method is called.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
CacheΒ  β†’

Last updated about 21 hours ago

Created by

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024