\Drupal\user\ContextProvider\CurrentUserContext::onBlockActiveContext() loads the anonymous user

Created on 28 April 2015, over 10 years ago
Updated 21 August 2025, 1 day ago

This shouldn't be necessary, and it takes over 1ms.

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

block.module

Created by

πŸ‡¬πŸ‡§United Kingdom catch

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

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 smustgrave

    Thank you for reporting this problem. We rely on issue reports like this one to resolve bugs and improve Drupal core.

    Since there has been no activity here for over 8 years we are asking if this problem persists on a currently supported version of Drupal. To help, add a comment explaining if the problem still occurs or not. Any extra detail you can provide can help others who experienced this.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
    Thanks!

  • πŸ‡¬πŸ‡§United Kingdom catch

    This one is unfortunately still valid, the code has barely changed.

  • πŸ‡¬πŸ‡§United Kingdom catch

    #28 might have been a red herring all that time ago, or have become one - while constructing an entity still gets field definitions, so does accessing any field.

    I tested with Umami, with the 'disclaimer' block visibility set to anonymous users, and with page cache uninstalled, and after truncating dynamic_page_cache (could have uninstalled dynamic_page_cache_instead).

    Drupal\block_content\Entity\BlockContent::isReusable() and Drupal\user\Entity\User::getRoles() both call Entity::get(). It's pretty likely that the current user context being in use means that at least one field will be accessed, so we might as well save loading the entity.

    Total function call difference in xhprof is -200 but I'm hoping we'll also see a cache get or some queries disappear in performance tests.

  • πŸ‡¬πŸ‡§United Kingdom catch
  • @catch opened merge request.
  • πŸ‡¬πŸ‡§United Kingdom catch

    Performance tests show a clear improvement. This means #38 is wrong, could be one of two reasons:

    1. We were already getting field definitions in other places ten years ago.

    2. We're getting field definitions now that we weren't previously.

    But given this is a clear improvement, I think we should go ahead with what we originally planned.

    πŸ“Œ Make EntityTypeManager provide an entity factory Needs work is still there to defer things even later.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Had to update CurrentUserContextTest to create the anonymous user in the database and fix assertions - it's testing for completely the wrong behaviour. The change here makes the test 'fail' but only because it's causing the anonymous user to be available whereas it was previously not found.

  • πŸ‡¬πŸ‡§United Kingdom catch
  • πŸ‡¬πŸ‡§United Kingdom catch

    Wondering if we want a follow-up for #28 / #29 to avoid loading the user entity at all, whether that's worth attempting will depend on πŸ“Œ Load user entity in Cookie AuthenticationProvider instead of using manual queries Needs work .

  • πŸ‡¬πŸ‡§United Kingdom catch

    Had another think about it and we might be able to do this as the follow-up: πŸ“Œ [12.x] Try to use property hooks to lazy instantiate entity keys Active .

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Awesome to see these old issues get revived!

Production build 0.71.5 2024