Argument 1 passed to Drupal\Core\Routing\RequestContext::fromRequest() must be an instance of Symfony\Component\HttpFoundation\Request, null given

Created on 13 October 2021, over 2 years ago
Updated 11 March 2024, 4 months ago
🐛 Bug report
Status

Closed: cannot reproduce

Version

10.1

Component
Request processing 

Last updated about 10 hours ago

No maintainer
Created by

🇨🇦Canada mmaranao Calgary, AB

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 wolfborg

    Been dealing with the issue @mbhadr mentioned tonight after updating our PHP to 8.1.

    After messing around with it for awhile, it looks like the way I got it to work is by directly including the core file which defines SAVED_UPDATES, which is "core/includes/common.inc". The only reason I had to is because the code later saves a change to an entity (in this case it changes a user's roles). If I didn't do this then it wasn't required.

    Also seems like I no longer need the patch from before to make Request work. Not sure if it's because of PHP 8.1, the request_stack push, or both. But either way that's nice. It's better than having to apply a patch every time I update Drupal core.

    If anyone needs it, here's what I did:

    use Drupal\Core\DrupalKernel;
    use Symfony\Component\HttpFoundation\Request;
    $autoloader = require_once '../autoload.php';
    $request = Request::createFromGlobals();
    $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod')->boot();
    $requestStack = Drupal::service('request_stack')->push($request);
    

    NOTE: autoload.php needs to be from Drupal root dir, so point to the correct path. In my case it was just in the parent folder.

  • 🇬🇷Greece Pavel Ruban

    Just for debug purposes:
    core 9.5.3, PHP 8.1

    It seems the issue happens in different contexts. In my case I have standard themed Drupal registry form without popups, first time page opens fine after it gets caches in anon session every next request results in this error:

    The website encountered an unexpected error. Please try again later.
    TypeError: Drupal\Core\Routing\RequestContext::fromRequest(): Argument #1 ($request) must be of type Symfony\Component\HttpFoundation\Request, null given, called in /projects/matchtech/docroot/core/lib/Drupal/Core/Routing/RequestContext.php on line 28 in Drupal\Core\Routing\RequestContext->fromRequest() (line 34 of core/lib/Drupal/Core/Routing/RequestContext.php).
    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\Plugin\PluginBase->__wakeup()
    unserialize() (Line: 167)
    Drupal\Core\Cache\DatabaseBackend->prepareItem() (Line: 122)
    Drupal\Core\Cache\DatabaseBackend->getMultiple() (Line: 92)
    Drupal\Core\Cache\DatabaseBackend->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: 713)
    Drupal\Core\DrupalKernel->handle() (Line: 19)

    On sixth backtrace line `Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters() (Line: 273)` it tries to get erroring request method by solving deps for `Drupal\Core\Routing\UrlGenerator`

    In my case the base service in backtrace has this service - `oauth2ClientService`.
    So just an assumption to conclude - if on the cached page with form you have a service that depends on the op service after cache get & wakeup it will lead to fatal

  • 🇬🇷Greece Pavel Ruban

    Just to elaborate on post above how the oath service was got into cache deps for user registry form:

    I have redirect subscriber that uses third party oauth logic in redirects flow:

      myaccount.event_subscriber:
        class: 'Drupal\myaccount\EventSubscriber\EventSubscriber'
        arguments:
          - '@current_user'
          - '@?crm_api.client'
          - '@entity_type.manager'
          - '@cache.data'
          - '@state'
          - '@messenger'
          - '@path.current'
        tags:
          - {name: event_subscriber}
    

    I believe if you add a subscriber with request or url generator dep & page is cached, on cache get contrib logic will result in fatal

  • PHP 8.1.21; Drupal Core 9.5.11

    I experienced this error when visiting a cached page as an anonymous user. The cached page is a custom form with URL generator dependencies, which seems to confirm @pavel-ruban's assessment:

    I believe if you add a subscriber with request or url generator dep & page is cached, on cache get contrib logic will result in fatal

    Stack trace:

    The website encountered an unexpected error. Please try again later.
    TypeError: Drupal\Core\Routing\RequestContext::fromRequest(): Argument #1 ($request) must be of type Symfony\Component\HttpFoundation\Request, null given, called in /var/www/web/core/lib/Drupal/Core/Routing/RequestContext.php on line 28 in Drupal\Core\Routing\RequestContext->fromRequest() (line 34 of core/lib/Drupal/Core/Routing/RequestContext.php).
    Drupal\Core\Routing\RequestContext->fromRequest(NULL) (Line: 28)
    Drupal\Core\Routing\RequestContext->fromRequestStack(Object)
    call_user_func_array(Array, Array) (Line: 276)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'router.request_context') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('router.request_context', 3) (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 273)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'coffee.url_generator') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('coffee.url_generator', 1) (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'url_generator') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('url_generator', 1) (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'oauth2_client.service.grant.authorization_code') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('oauth2_client.service.grant.authorization_code', 1) (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'oauth2_client.service') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('oauth2_client.service', 1) (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'rt_mercury.mercury') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('rt_mercury.mercury', 1) (Line: 434)
    Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 237)
    Drupal\Component\DependencyInjection\Container->createService(Array, 'rt_contact_preferences.contact_preferences_form_helper') (Line: 177)
    Drupal\Component\DependencyInjection\Container->get('rt_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://riviera-travel.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)

    Applying Patch #8 has resolved this issue for me, and revealed what I believe to be related error for which I have opened a separate issue 🐛 "Call to a member function hasSession() on null" in SharedTempStoreFactory Active .

  • Here is an example of this issue happening in Drupal 10.1.6:

    # my_module.services.yml
    my_module.my_http_middleware:
        class: Drupal\my_module\StackMiddleware\MyHttpMiddleware
        arguments:
          - '@router.request_context'
        tags:
          - { name: http_middleware, priority: 10 }
    
    
    namespace Drupal\my_module\StackMiddleware;
    
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\HttpKernel\HttpKernelInterface;
    use Symfony\Component\Routing\RequestContext;
    
    class MyHttpMiddleware implements HttpKernelInterface {
    
      private HttpKernelInterface $httpKernel;
      private RequestContext $requestContext;
    
      public function __construct(
        HttpKernelInterface $httpKernel,
        RequestContext $requestContext,
      ) {
        $this->httpKernel = $httpKernel;
        $this->requestContext = $requestContext;
      }
    
      public function handle(Request $request, int $type = self::MAIN_REQUEST, bool $catch = TRUE): Response {
        return $this->httpKernel->handle($request, $type, $catch);
      }
    
    }
    

    I have not tested in later versions yet. The patch in #8 worked, but given #16 I'm curious if something else is not quite right.

  • 🇮🇳India VishalKumarSahu

    In the similar case on Drupal 10.2, I am getting an error on $account->save();

    On Windows WAMP with PHP 8.2 (With error reporting enabled):

    Fatal error: Uncaught AssertionError: Transaction $stack was not empty. Active stack: 65a57aa37f5ba6.98051663\drupal_transaction in D:\www\public\core\lib\Drupal\Core\Database\Transaction\TransactionManagerBase.php on line 99

    On Linux with PHP 8.2 (With error reporting to database as it is production):

    Error: Undefined constant "Drupal\Core\Entity\SAVED_UPDATED" in Drupal\Core\Entity\ContentEntityStorageBase->doSave() (line 703 of /www/public/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php)

  • 🇬🇧United Kingdom tce

    I'm having a similar issue. I am using REST to post content from one site to another.

    I upgraded Drupal from 9 to 10 and I believe the user I use for REST is using an older password scheme, so this is why user save is being triggered when I try to POST some data to the site. I get the error:

    Error: Undefined constant "Drupal\Core\Entity\SAVED_UPDATED" in Drupal\Core\Entity\ContentEntityStorageBase->doSave() (line 704 of /app/web/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php)

  • I have encountered the same issue on Drupal v10.1.2
    The patch #8 resolved it!

  • 🇬🇧United Kingdom lexsoft London

    I have encountered the same issue on Drupal v10.1.8 with basic_auth. Resetting the user's password after the update fixed the issue for me. Happened after updating core from 10.0.x to 10.1.x.

    Error: Undefined constant "Drupal\Core\Entity\SAVED_UPDATED" in Drupal\Core\Entity\ContentEntityStorageBase->doSave() (line 703 of my_website_stage_5.0/docroot/core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php) 
    #0 my_website_stage_5.0/docroot/core/lib/Drupal/Core/Entity/EntityStorageBase.php(486): Drupal\Core\Entity\ContentEntityStorageBase->doSave()
    #1 my_website_stage_5.0/docroot/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php(806): Drupal\Core\Entity\EntityStorageBase->save()
    #2 my_website_stage_5.0/docroot/core/lib/Drupal/Core/Entity/EntityBase.php(339): Drupal\Core\Entity\Sql\SqlContentEntityStorage->save()
    #3 my_website_stage_5.0/docroot/core/modules/user/src/UserAuth.php(57): Drupal\Core\Entity\EntityBase->save()
    #4 my_website_stage_5.0/docroot/core/modules/basic_auth/src/Authentication/Provider/BasicAuth.php(110): Drupal\user\UserAuth->authenticate()
    #5 my_website_stage_5.0/docroot/modules/contrib/shield/src/ShieldMiddleware.php(343): Drupal\basic_auth\Authentication\Provider\BasicAuth->authenticate()
    #6 my_website_stage_5.0/docroot/modules/contrib/shield/src/ShieldMiddleware.php(263): Drupal\shield\ShieldMiddleware->basicAuthRequestAuthenticate()
    #7 my_website_stage_5.0/docroot/modules/contrib/shield/src/ShieldMiddleware.php(137): Drupal\shield\ShieldMiddleware->bypass()
    #8 my_website_stage_5.0/docroot/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(48): Drupal\shield\ShieldMiddleware->handle()
    #9 my_website_stage_5.0/docroot/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(51): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
    #10 my_website_stage_5.0/docroot/core/lib/Drupal/Core/StackMiddleware/StackedHttpKernel.php(51): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
    #11 my_website_stage_5.0/docroot/core/lib/Drupal/Core/DrupalKernel.php(704): Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
    #12 my_website_stage_5.0/docroot/index.php(19): Drupal\Core\DrupalKernel->handle()
    
Production build 0.69.0 2024