Problem/Motivation

Drupal suddenly stopped to work. I was working on a theme when Drupal started to throw a \Drupal\Component\Plugin\Exception\ContextException on every page view.

The error message and a backtrace are attached. Due to recursion in objects the output of debug_print_backtrace() could not be retrieved.

Steps to reproduce

I don't know. The last thing I remember was clearing all caches.

Proposed resolution

The function Drupal\Core\ParamConverter\EntityConverter->convert contains the code

    $context_id = '@user.current_user_context:current_user';
    if (isset($contexts[$context_id])) {
      $account = $contexts[$context_id]->getContextValue();
      unset($account->_skipProtectedUserFieldConstraint);
      unset($contexts[$context_id]);
    }

which fails. Obviously the result is thrown away for some reason. So there is no need to fail at this point.
The following code seems to work for me:

    $context_id = '@user.current_user_context:current_user';
    if (isset($contexts[$context_id])) {
      try {
        $account = $contexts[$context_id]->getContextValue();
        unset($account->_skipProtectedUserFieldConstraint);
      } catch (\Drupal\Component\Plugin\Exception\ContextException $e) {
      }
      unset($contexts[$context_id]);
    }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

keinstein created an issue. See original summary.

cilefen’s picture

keinstein’s picture

@cilefen What do you mean with the links?

I don't see why change in templates/layout/page.html.twig or playing around with scss and css files should trigger such a bug.

A framework that is so sensitive to changes is buggy in my opinion. Or in other words: If the theme is buggy the framework should help the themers to track down the error and not blur it.

Btw.: How do the links relate to https://www.drupal.org/node/2934192 and the comment in the following source code?

// @todo At the moment we do not need the current user context, which is
//   triggering some test failures. We can remove these lines once
//   https://www.drupal.org/node/2934192 is fixed.
cilefen’s picture

Title: Unhandled exception in EntityConverter. » The 'entity:user' context is required and not present

Those links discuss the same error. I thought they may help.

webdrips’s picture

The patch worked for me.

The problem occurred during a migration (I believe it was during migrate-import --all).

I couldn't view the site as an anonymous user.

rollins’s picture

Status: Active » Reviewed & tested by the community

patch resolved the bug, now an anonymous user can see the website
I am using Drupal Core 9.5.9

cilefen’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

The patch did not pass code quality tests. Also it lacks regression test coverage.

karishmaamin’s picture

Status: Needs work » Needs review
StatusFileSize
new1.22 KB

Tried to fix custom code error with the patch

smustgrave’s picture

Version: 9.5.x-dev » 11.x-dev
Status: Needs review » Needs work

Previously tagged for tests that still need to happen.

Also should target the 11.x branch as that's the current development branch. Changes can be backported later.

dpi’s picture

Is this the same issue as #3056234: User context missing when using toUrl in some circumstances? likey caused by a missing anonymous user row from a bad database dump

I suppose this could also happen if a migration occurred and no user-0 was retained

joachim’s picture

+++ b/core/lib/Drupal/Core/ParamConverter/EntityConverter.php
@@ -146,8 +147,12 @@ public function convert($value, $definition, $name, array $defaults) {
+      catch (ContextException $e) {
+      }

Silently catching an exception doesn't seem like a good fix to me.

At the very least this requires a comment to explain a) why this might happen and b) why it's ok to silently ignore it.

Syone’s picture

Hello, I have the same issue after updating the database with the update.php script. Has anyone find out how to fix this?

Syone’s picture

I finally find out what happened, this is what @dpi described.
I've made a database dump and then reload it in my local MySQL database.
The insert query try to create a user with a uid = 0 (the anonymous user) but in MySQL when the mode NO_AUTO_VALUE_ON_ZERO is not enable, it will use the auto increment sequence instead of inserting 0 as uid.

dpi’s picture

Status: Needs work » Closed (duplicate)
Related issues: +#3357354: The 'entity:user' context is required and not present

Closing this as duplicate #3056234: User context missing when using toUrl in some circumstances. I also see the patch in both issues are very similar.

Reopen if the differences can be described.

herved’s picture

I have now stumbled on this for 2 projects in behat tests.
FWIW in my case it's not the missing anonymous user issue (it's there) #3357354: The 'entity:user' context is required and not present
Here is my stacktrace:

Context.php:72, Drupal\Core\Plugin\Context\Context->getContextValue()
EntityConverter.php:149, Drupal\Core\ParamConverter\EntityConverter->convert()
ParamConverterManager.php:100, Drupal\Core\ParamConverter\ParamConverterManager->convert()
ParamConversionEnhancer.php:45, Drupal\Core\Routing\Enhancer\ParamConversionEnhancer->enhance()
Router.php:270, Drupal\Core\Routing\Router->applyRouteEnhancers()
Router.php:150, Drupal\Core\Routing\Router->matchRequest()
AccessAwareRouter.php:90, Drupal\Core\Routing\AccessAwareRouter->matchRequest()
AccessAwareRouter.php:144, Drupal\Core\Routing\AccessAwareRouter->match()
LanguageNegotiationUserAdmin.php:138, Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin->isAdminPath()
LanguageNegotiationUserAdmin.php:104, Drupal\user\Plugin\LanguageNegotiation\LanguageNegotiationUserAdmin->getLangcode()
LanguageNegotiator.php:197, Drupal\language\LanguageNegotiator->negotiateLanguage()
LanguageNegotiator.php:137, Drupal\language\LanguageNegotiator->initializeType()
ConfigurableLanguageManager.php:218, Drupal\language\ConfigurableLanguageManager->getCurrentLanguage()
LanguageRequestSubscriber.php:92, Drupal\language\EventSubscriber\LanguageRequestSubscriber->setLanguageOverrides()
LanguageRequestSubscriber.php:74, Drupal\language\EventSubscriber\LanguageRequestSubscriber->onKernelRequestLanguage()
ContainerAwareEventDispatcher.php:111, call_user_func:{/home/herve/Documents/commission/ecdc/ecdc-monorepo/web/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php:111}()
ContainerAwareEventDispatcher.php:111, Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch()
HttpKernel.php:142, Symfony\Component\HttpKernel\HttpKernel->handleRaw()
HttpKernel.php:74, Symfony\Component\HttpKernel\HttpKernel->handle()
Session.php:58, Drupal\Core\StackMiddleware\Session->handle()
KernelPreHandle.php:48, Drupal\Core\StackMiddleware\KernelPreHandle->handle()
ReverseProxyMiddleware.php:48, Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle()
NegotiationMiddleware.php:51, Drupal\Core\StackMiddleware\NegotiationMiddleware->handle()
StackedHttpKernel.php:51, Drupal\Core\StackMiddleware\StackedHttpKernel->handle()
DrupalKernel.php:704, Drupal\Core\DrupalKernel->handle()
index.php:19, {main}()

This issue seems to happen somewhat randomly for me.
After some investigation, this is happening exclusively on image styles routes.
I believe the random factor is partially due to the fact that I use selenium, and the browser caches images (so disabling selenium's browser cache helps), but also a race condition as follows: behat ends the scenario, deletes the user, and at the same time, the homepage gets refreshed and lazy loads the images, creating new requests.
Most probably the user still exists at the beginning of these image styles requests, but behat (still running in the background) deletes the user and when the image style requests arrives in \Drupal\user\ContextProvider\CurrentUserContext::getRuntimeContexts and attempts to load the user, it returns NULL (this I confirmed), causing in turn The 'entity:user' context is required and not present exception to be thrown from \Drupal\Core\Plugin\Context\Context::getContextValue

I will see if #3056234: User context missing when using toUrl in some circumstances helps solving the issue.
PS: I know this issue is closed but perhaps this info could be useful to someone else, who knows.

Edit: I ended up applying #3158130: Many calls to ContextRepository::getAvailableContexts() due to entity upcasting which is the proper fix I believe.