Weprofiler module throws fatal error when the module is enabled and a user is logged in using the admin (user id 1), because there are no roles assigned to that role.

The website encountered an unexpected error. Please try again later.
Error: Call to a member function label() on null in Drupal\webprofiler\DataCollector\UserDataCollector->collect() (line 66 of modules/contrib/devel/webprofiler/src/DataCollector/UserDataCollector.php).
Drupal\webprofiler\DataCollector\UserDataCollector->collect(Object, Object, NULL) (Line: 223)
Symfony\Component\HttpKernel\Profiler\Profiler->collect(Object, Object, NULL) (Line: 128)
Symfony\Component\HttpKernel\EventListener\ProfilerListener->onKernelResponse(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 61)
Symfony\Component\EventDispatcher\Debug\WrappedListener->__invoke(Object, 'kernel.response', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.response', Object) (Line: 140)
Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher->dispatch('kernel.response', Object) (Line: 184)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 166)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 64)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 43)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 652)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

abhigupta created an issue. See original summary.

abhigupta’s picture

Issue summary: View changes
FileSize
808 bytes
lussoluca’s picture

Status: Active » Needs work

Yes you are right, only standard profile adds a role to user 1 (in Core).

Just a quick nitpick

+++ b/webprofiler/src/DataCollector/UserDataCollector.php
@@ -98,8 +98,9 @@ class UserDataCollector extends DataCollector implements DrupalDataCollectorInte
+      if ($entity = $storage->load($role)) {

Please avoid single equals sign in if condition because it can confuse readers.

lussoluca’s picture

Project: WebProfiler » Devel
Component: Code » webprofiler
lussoluca’s picture

Status: Needs work » Postponed (maintainer needs more info)

Uhm..wait,
user 1 should have at least "Authenticated User" role.
Also if code enter in foreach at line 64 the user has at least a role. In your case it seems that the user has a role that no longer exists (so the load() returns NULL).

Could you please check if this is the case?

kdebisschop’s picture

This is not only a problem with user/1 - it seems to be a problem with any user that has only the authenticated role. Adding an essentially unprivileged role makes it work.

twfahey’s picture

#2 fixed it for me, from my investigation it appears that 'administrator' loads as NULL when piped through ` $entity = $storage->load($role); `. I can tell you that this was on a clean install, however it was using a custom installation profile. I haven't tested it with a vanilla D8 install, but I don't believe I'm altering anything in regards to roles, I'm really just enabling a theme and some modules. Before I found this issue, I was ksm'ing on `$this->currentUser->getRoles()`, which tells me my user has only the 2 roles:
```
string(13) "authenticated"
string(13) "administrator"
```
When I ksm'ed inside the loop ~line 66, that's when I discovered that the authenticated role was loading, but administrator was not, it was null.

Rar9’s picture

applying patch reduced error but I now get

Warning: Invalid argument supplied for foreach() in Drupal\webprofiler\EventDispatcher\TraceableEventDispatcher->addCalledListener() (line 166 of /var/www/vhosts/XXXX/httpdocs/modules/devel/webprofiler/src/EventDispatcher/TraceableEventDispatcher.php).

malcomio’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
966 bytes

I'm also getting this error with a custom install profile.

Stepping through this in XDebug for user 1, $storage->entities contains a Role object for 'authenticated', but null for 'administrator'.

Seems likely to be a symptom of another issue somewhere else - probably related to the fact that the config yml for the administrator role is in the standard install profile.

We could at least prevent the fatal error by checking if the entity exists, and using the role ID if not.

malcomio’s picture

I think (at least in our case) that the role entity is null because the custom install profile calls standard_install(), as suggested on https://www.drupal.org/docs/8/creating-distributions/how-to-write-a-drup...

This includes assigning the administrator role to user 1, but because the config for the role is defined in the standard profile, the role entity is not created.

Might also be worth testing with the minimal install profile.

malcomio’s picture

Having investigated further, this is indeed happening because the custom install profile does not include config for the administrator role.

The wiki page mentions that it was written based on the Spark profile, which does include the role config .

Adding the config to our custom profile means that the entity exists, and the error no longer occurs (with or without the patch applied).

I think that the custom profile documentation should be updated to mention this.

Prashant.c’s picture

@abhigupta

How is this possible that User 1 does not have any role.
He will always have 2 roles "authenticated" and "administrator" and even if you remove "administrator" he will always have "authenticated" role.

Can you please list down the steps how can i replicate this ?

salvis’s picture

Status: Needs review » Postponed (maintainer needs more info)

Thank you for the research, malcomio.

This seems to be a rare issue, likely tied to buggy installation profiles. Has anyone been able to reproduce it with plain vanilla D8?

willzyx’s picture

Status: Postponed (maintainer needs more info) » Closed (cannot reproduce)

Closing for lack of activity. Feel free to reopen if the issue still exists