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.

Issue fork drupal-2701575

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

davidwbarratt created an issue. See original summary.

tusharbodke’s picture

Assigned: Unassigned » tusharbodke

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

znerol’s picture

Component: page_cache.module » rest.module
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)

This looks like a cached Drupal\rest\ResourceResponse instance which contains a serialized URL or URL generator.

Wim Leers’s picture

Category: Bug report » Support request
Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)

https://example.com/entity/colossal_menu/main/tree
This is not something you can do with Drupal core.

So this must be a custom module with a custom @RestResource plugin. Therefore there must be a bug in that module.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)
mpark’s picture

Hey guys, I have to reopen this, because suddenly after upgrade to 8.6.4 I have this issues as well.

It appeared after the bot visited robots.txt

Type php
Date Wednesday, December 12, 2018 - 03:21
User Anonymous (not verified)
Location https://xxxx/robots.txt
Referrer
Message TypeError: Argument 2 passed to Drupal\page_cache\StackMiddleware\PageCache::storeResponse() must be an instance of Symfony\Component\HttpFoundation\Response, null given, called in /home/xxx/xxx/core/modules/page_cache/src/StackMiddleware/PageCache.php on line 188 in Drupal\page_cache\StackMiddleware\PageCache->storeResponse() (line 205 of /home/xxxxxx/core/modules/page_cache/src/StackMiddleware/PageCache.php) #0 /home/xxxx/xxxx/core/modules/page_cache/src/StackMiddleware/PageCache.php(188): Drupal\page_cache\StackMiddleware\PageCache->storeResponse(Object(Symfony\Component\HttpFoundation\Request), NULL) #1 /home/xxxx/xxxx/core/modules/page_cache/src/StackMiddleware/PageCache.php(121): Drupal\page_cache\StackMiddleware\PageCache->fetch(Object(Symfony\Component\HttpFoundation\Request), 1, true) #2 /home/xxxx/xxxx/core/modules/page_cache/src/StackMiddleware/PageCache.php(75): Drupal\page_cache\StackMiddleware\PageCache->lookup(Object(Symfony\Component\HttpFoundation\Request), 1, true) #3 /home/xxxx/xxxx/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #4 /home/xxxx/xxxx/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(52): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #5 /home/xxxx/xxxx/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #6 /home/xxxx/xxxx/core/lib/Drupal/Core/DrupalKernel.php(669): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #7 /home/xxxx/xxxx/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #8 {main}.

Wim Leers’s picture

Issue tags: +API-First Initiative

@mpark Please create a new issue — this issue is >2 years old and was closed! Please include steps to reproduce, and it may be worth linking to this issue as being similar. Note that this issue was specifically a problem with a custom REST resource plugin, which it sounds like is different than your use case.

ecdani’s picture

This error occurs to me putting a custom ajax callback to a contact form in hook_form_alter() of this type:

$form['actions']['submit']['#ajax'] = [
'callback' => [$this, 'contactFormsAjaxCallback'],
'wrapper' => 'ajax_unique_id_' . $form_id,
'effect' => 'fade',
'event' => 'message',
];

I solved it usign this:

$form['actions']['submit']['#ajax'] = [
'callback' => [static::class, 'contactFormsAjaxCallback'],
'wrapper' => 'ajax_unique_id_' . $form_id,
'effect' => 'fade',
'event' => 'message',
];

Im using Drupal 8.8.1

phjou’s picture

ecdani, I had the same error

change:

'callback' => [$this, 'contactFormsAjaxCallback'],

by

'callback' => '::contactFormsAjaxCallback',

Kind of strange but it works, I don't know where I got that first syntax.

b.khouy’s picture

Version: 8.2.x-dev » 9.2.x-dev
Status: Closed (cannot reproduce) » Active
FileSize
641 bytes

I've got the same error recently

Problem/Motivation

I have an external php script in which I had to bootstrap Drupal (to get benefit from some existing Drupal services), and also do some other stuff, in my script I have to render a twig template using renderInline() twig service method:
external-script.php file:

use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;

// Bootstrap to initialize container.
$autoloader = require __DIR__ . '/autoload.php';
require_once __DIR__ . '/core/includes/bootstrap.inc';
require_once __DIR__ . '/core/includes/common.inc';

$request = Request::createFromGlobals();
Settings::initialize(dirname(dirname(__DIR__)), DrupalKernel::findSitePath($request), $autoloader);
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod')->boot();
$container = $kernel->getContainer();

// Do something here...

// Get twig service from container;
$twig = $container->get('twig');
$template_file = __DIR__ . '/templates/my-custom-template.html.twig';
$template = file_get_contents($template_file);
$variables = [
    'content' => $content,
];

// Call renderInline method.
$content = $twig->renderInline($template, $variables)->__toString();

header('Content-Type: application/json; charset=utf-8');
echo json_encode(['content' => trim(preg_replace('/\t+/', '', $content))]);

Here is the error message and trace:

TypeError: 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 28 in /core/lib/Drupal/Core/Routing/RequestContext.php on line 34 #0 /core/lib/Drupal/Core/Routing/RequestContext.php(28): Drupal\Core\Routing\RequestContext->fromRequest(NULL)
#1 [internal function]: Drupal\Core\Routing\RequestContext->fromRequestStack(Object(Drupal\Core\Http\RequestStack))
#2 /core/lib/Drupal/Component/DependencyInjection/Container.php(279): call_user_func_array(Array, Array)
#3 /core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService(Array, 'router.request_...')
#4 /core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get('router.request_...', 3)
#5 /core/lib/Drupal/Component/DependencyInjection/Container.php(276): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)
#6 /core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService(Array, 'coffee.url_gene...')
#7 /core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get('coffee.url_gene...', 1)
#8 /core/lib/Drupal/Component/DependencyInjection/Container.php(240): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)
#9 /core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService(Array, 'url_generator')
#10 /core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get('url_generator', 1)
#11 /core/lib/Drupal/Component/DependencyInjection/Container.php(240): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)
#12 /core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService(Array, 'twig.extension')
#13 /core/lib/Drupal/Component/DependencyInjection/Container.php(437): Drupal\Component\DependencyInjection\Container->get('twig.extension', 1)
#14 /core/lib/Drupal/Component/DependencyInjection/Container.php(276): Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array)
#15 /core/lib/Drupal/Component/DependencyInjection/Container.php(176): Drupal\Component\DependencyInjection\Container->createService(Array, 'twig')
#16 /external-script.php(21): Drupal\Component\DependencyInjection\Container->get('twig')
#18 {main}

Proposed resolution

I've created the attached patch to solve temporarily the problem

andregp’s picture

Status: Active » Needs review

Patch #12 needs review.

ranjith_kumar_k_u’s picture

smustgrave’s picture

Category: Support request » Bug report
Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

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.

klausi’s picture

Version: 9.2.x-dev » 10.1.x-dev
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
972 bytes

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

smustgrave’s picture

Status: Needs review » Needs work

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’s picture

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.

smustgrave’s picture

Assigned: tusharbodke » Unassigned
Status: Needs review » Reviewed & tested by the community

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.

smustgrave’s picture

Title: Argument 1 passed to Drupal\Core\Routing\RequestContext::fromRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given » RequestContext throws error when current request is empty
larowlan’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Bug Smash Initiative

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.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jan kellermann’s picture

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)