RequestContext throws error when current request is empty

Created on 7 April 2016, about 8 years ago
Updated 3 July 2023, 12 months ago

Problem/Motivation

Enabling Page Cache produces this error on certain paths on a cache hit:

Argument 1 passed to Drupal\Core\Routing\RequestContext::fromRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given, called in core/lib/Drupal/Core/Routing/RequestContext.php on line 36 and defined in Drupal\Core\Routing\RequestContext->fromRequest() (line 42 of core/lib/Drupal/Core/Routing/RequestContext.php).

Here's the backtrace:

Drupal\Core\Routing\RequestContext->fromRequest(NULL) (Line: 36)
Drupal\Core\Routing\RequestContext->fromRequestStack(Object)
call_user_func_array(Array, Array) (Line: 329)
Drupal\Component\DependencyInjection\Container->createService(Array, 'router.request_context') (Line: 181)
Drupal\Component\DependencyInjection\Container->get('router.request_context', 3) (Line: 487)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Object) (Line: 326)
Drupal\Component\DependencyInjection\Container->createService(Array, 'private__b7bcd0b1968d44ae44212040b5b5ae5a1185e77f') (Line: 502)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Object) (Line: 233)
Drupal\Component\DependencyInjection\Container->createService(Array, 'url_generator') (Line: 181)
Drupal\Component\DependencyInjection\Container->get('url_generator') (Line: 54)
Drupal\Core\Url->__wakeup()
unserialize('O:28:"Drupal\rest\ResourceResponse":8:{s:15:"*responseData";a:7:{i:0;O:36:"Drupal\C
Drupal\Core\Cache\DatabaseBackend->prepareItem(Object, ) (Line: 98)
Drupal\Core\Cache\DatabaseBackend->getMultiple(Array, ) (Line: 68)
Drupal\Core\Cache\DatabaseBackend->get('https://example.com/entity/colossal_menu/main/tree?_format=json:json', ) (Line: 285)
Drupal\page_cache\StackMiddleware\PageCache->get(Object) (Line: 121)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 79)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 55)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 34)
Drupal\gc_api\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 637)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

The root cause is that our RequestContext class does not take into account that the RequestStack can be empty and return NULL for getCurrentRequest(), although NULL is a valid case there.

As the stacktrace shows this can happen for example when a Url object is unserialized from cache, which calls down to RequestContext.

Proposed resolution

Fix RequestContext so that it takes potential NULL values from RequestStack into account and is more robust.

Remaining tasks

Review the pull request.

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component
RESTΒ  β†’

Last updated 22 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States davidwbarratt

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request β†’ as a guide.

    This will require a test case.

  • Status changed to Needs review over 1 year ago
  • πŸ‡¦πŸ‡ΉAustria klausi πŸ‡¦πŸ‡Ή Vienna

    Unit test case attached without the fix to demonstrate the fail. Bug also exists in Drupal 10.

  • Status changed to Needs work over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Reading the issue summary

    Figure out what is causing the error and resolve it.

    Was it determined what was causing it? What was causing the empty request?

  • @klausi opened merge request.
  • Status changed to Needs review over 1 year ago
  • πŸ‡¦πŸ‡ΉAustria klausi πŸ‡¦πŸ‡Ή Vienna

    Ah sorry, did not fully update the issue summary. Done now.

    As shown in the stacktrace the error is caused when a Url object is unserialized and calls down to RequestContext. That can happen in a middleware where no current request was pushed onto the RequestStack yet.

  • Issue was unassigned.
  • Status changed to RTBC over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thanks. Removing the assigned person as it's been 7 years.

    #16 does show the failure so will move this along to see what the committers say.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave
  • Status changed to Needs work about 1 year ago
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    Left a comment on the MR

    Can we reproduce this without a unit test, the test is slightly contrived - in reality most Drupal code will be running in the context of a request. It's the first thing we create in index.php.

  • πŸ‡©πŸ‡ͺGermany jan kellermann

    We have this problem when page_cache is deserializing a form because somewhere on the way the RequestStack is empty.
    We use open social and memcache, so several parts of code can cause this bug. The form is the enroll-action-form embedded in a block.

    The MR 3646 mitigated this problem.

    TypeError: Drupal\Core\Routing\RequestContext::fromRequest(): Argument #1 ($request) must be of type Symfony\Component\HttpFoundation\Request, null given, [...]
    Drupal\Core\Routing\RequestContext->fromRequest() (Line: 28)
    Drupal\Core\Routing\RequestContext->fromRequestStack()
    call_user_func_array() (Line: 276)
    Drupal\Component\DependencyInjection\Container->createService() (Line: 177)
    Drupal\Component\DependencyInjection\Container->get() (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters() (Line: 273)
    Drupal\Component\DependencyInjection\Container->createService() (Line: 449)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters() (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService() (Line: 177)
    Drupal\Component\DependencyInjection\Container->get() (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters() (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService() (Line: 177)
    Drupal\Component\DependencyInjection\Container->get() (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters() (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService() (Line: 177)
    Drupal\Component\DependencyInjection\Container->get() (Line: 89)
    Drupal\Core\Form\FormBase->__wakeup()
    Memcached->getMulti() (Line: 71)
    Drupal\memcache\Driver\MemcachedDriver->getMulti() (Line: 144)
    Drupal\memcache\MemcacheBackend->getMultiple() (Line: 136)
    Drupal\memcache\MemcacheBackend->get() (Line: 306)
    Drupal\page_cache\StackMiddleware\PageCache->get() (Line: 124)
    Drupal\page_cache\StackMiddleware\PageCache->lookup() (Line: 82)
    Drupal\page_cache\StackMiddleware\PageCache->handle() (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle() (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle() (Line: 23)
    Stack\StackedHttpKernel->handle() (Line: 718)
    Drupal\Core\DrupalKernel->handle() (Line: 19)
    
Production build 0.69.0 2024