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.
Comment | File | Size | Author |
---|
Issue fork drupal-2701575
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
tusharbodke CreditAttribution: tusharbodke as a volunteer and at Faichi Solutions Pvt Ltd commentedComment #4
znerol CreditAttribution: znerol commentedThis looks like a cached
Drupal\rest\ResourceResponse
instance which contains a serialized URL or URL generator.Comment #5
Wim Leershttps://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.Comment #7
Wim LeersComment #8
mpark CreditAttribution: mpark commentedHey 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}.
Comment #9
Wim Leers@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.
Comment #10
ecdani CreditAttribution: ecdani commentedThis 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
Comment #11
phjouecdani, 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.
Comment #12
b.khouyI'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:
Here is the error message and trace:
Proposed resolution
I've created the attached patch to solve temporarily the problem
Comment #13
andregp CreditAttribution: andregp at CI&T commentedPatch #12 needs review.
Comment #14
ranjith_kumar_k_u CreditAttribution: ranjith_kumar_k_u at Zyxware Technologies commentedRe-rolled #12 for 9.4.
Comment #15
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis 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.
Comment #16
klausiUnit test case attached without the fix to demonstrate the fail. Bug also exists in Drupal 10.
Comment #17
smustgrave CreditAttribution: smustgrave at Mobomo commentedReading 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?
Comment #19
klausiAh 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.
Comment #20
smustgrave CreditAttribution: smustgrave at Mobomo commentedThanks. 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.
Comment #21
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #22
larowlanLeft 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.
Comment #24
jan kellermann CreditAttribution: jan kellermann at werk21 commentedWe 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.