Drupal Version
11.3
Domain module version
2.0.0-beta8
Expected Behavior
No warnings
Actual Behavior
Warning generated
Warning: Undefined property: Drupal\Core\Field\EntityReferenceFieldItemList::$entity in domain_source_get() (line 117 of .../web/modules/contrib/domain/domain_source/domain_source.module
Steps to reproduce
- Install Drupal 11.3 and Domain 2.0.0.-beta8
- Create a web page "all default links" that only includes links to nodes whose field_domain_source is the default domain
- Create a web page "non-default links" that includes links to nodes whose field_domain_source is a non-default domain
- Rebuild caches
- View the "all default links" page - no warning is generated
- View the "non-default links" page - a warning is generated for the first link rendered for each non-default domain
- Refresh the "non-default links" page - no warning is generated
My guess at what is happening
When domain_source_get() gets the 'entity' property of the Node's field_domain_source, EntityReference::getTarget() is called to return the entity. Usually this works fine, but in the rendering of a response, Fibers are used. The warning appears to be generated when the current Fiber is suspended - when it resumes the 'entity' property evaluates as NULL. The Fiber appears to be suspended when EntityReference::getTarget() accesses the database rather than the cache.
Evidence for this includes ...
- No warnings are generated for links to Nodes for the default domain - there are many calls made to DomainStorage::loadDefaultDomain() before the response is rendered so the default domain is cached
- Warnings are only generated after the cache is cleared
- When debugging domain_source_get() the warning is only generated when the current Fiber is suspended and restarted
Workaround
The warnings go away if we load all the domains before the page is rendered. For example ...
function THEME_preprocess_html(&$variables) {
$domain_storage = \Drupal::entityTypeManager()->getStorage('domain');
$domains = $domain_storage->loadMultiple();
}
Issue fork domain-3565121
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
mably commentedWe need a Fibers expert here 😉
Or should we just handle the warning properly?
Is it really specific to the Domain source module?
Is the warning still present when using the 3.x branch?
Comment #5
mably commentedComment #6
mably commentedI haven't been able to reproduce the problem on a Drupal 11.3.1 instance.
So will probably close the issue unless new information is provided.
Comment #9
mably commentedThe warnings should be fixed.
Comment #11
kenwest commentedThanks for your prompt response @mably
Unfortunately your patch for MR 296 doesn't fix my warnings, and my workaround still fixes the problem in my context after that patch is applied.
I have no answers to your questions ATM so I'll need to do some digging. The Fibers stuff is weird and may just be an artifact of my IDE.
Comment #12
mably commentedLooks like we fixed the wrong warnings then.
What is the full stack trace of the remaining warning?
Comment #13
kenwest commentedHi @mably, if you can't reproduce the problem then perhaps this is a feature of my environment? Perhaps I should dig further before you expend more effort on this.
BTW I'll be offline for the next 2 weeks.
Here is the stack trace
Comment #14
kenwest commentedAs an aside, I noticed the latest release of Paragraphs has a Fiber-related bug fix.
The fix they made to line 42 of Paragraph's modules/paragraphs_library/src/LibraryItemAccessControlHandler.php looks very similar to the code in domain_source_get().
I've asked the developer over there if our symptoms are like theirs.
Comment #15
kenwest commentedUsing the hint from the Paragraphs issue, I've created a patch that prevents the warning being generated. I don't know if I'll have enough time to update the merge requests before I go away, so I'm just pasting the raw diffs here.
This is the patch to 2.0.0-beta8 ...
This is the patch to Merge Requests 295 and 296 ...
Comment #16
berdirSee #3518668: Use Fibers for rendering views rows for a core issue with some context, it's a hard to fix php bug that we're running into. Suggested fixes look ok at a glance
Comment #17
mably commentedThanks for the share @berdir, will have a look at it.
I will implement the suggested fix.
Comment #23
mably commentedComment #26
mably commentedComment #28
mably commentedLet's remember to check back on this issue when PHP 8.6 is required as the minimum version for Drupal 😉
Comment #29
idebr commentedA workaround was implemented in Drupal Core, see #3565937: Workaround PHP bug with fibers and __get()
Comment #30
mably commented