Repository navigation
Don't hide lazy loading failures behind the IsLoaded exception - #39387
Merged
AndriySvyryd merged 2 commits intoOct 23, 2026
Merged
Conversation
- When a load failed after the entity was fixed up to a reference, resetting IsLoaded threw and replaced the original exception - Only reset IsLoaded for collections and references that are still null Fixes dotnet#34167
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Add equivalent regression coverage for asynchronous lazy-loading failure after reference fixup.
1 open issue
What changed in this PR
This pull request preserves the original exception when lazy loading fails after reference-navigation fixup, preventing IsLoaded cleanup from masking the failure.
Changes:
- Applies conditional
IsLoadedreset logic to synchronous and asynchronous lazy-loading paths. - Adds regression coverage for synchronous reference lazy-loading failures after fixup.
- Async failure-after-fixup behavior still lacks direct regression coverage.
| File | Summary |
|---|---|
test/EFCore.Specification.Tests/LazyLoadProxyTestBase.cs |
Verifies that synchronous lazy-loading failures after reference fixup preserve the original exception. |
src/EFCore/Infrastructure/Internal/LazyLoader.cs |
Avoids resetting populated reference navigations in a way that hides the original lazy-loading exception. |
Review effort: Lite
AndriySvyryd
approved these changes
Oct 23, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Fixes #34167
When a lazy load failed,
LazyLoader.Load/LoadAsyncsetentry.IsLoaded = falsebefore rethrowing. If the failure came after the loaded entity had already been fixed up to a reference navigation, as with the deadlock in the issue, that setter throwsReferenceMustBeLoaded("cannot have 'IsLoaded' set to false because the referenced entity is non-null"). The new exception replaced the one that made the load fail.IsLoadedis now only reset when the setter allows it: for collections, or for references that are still null. A reference that already points to an entity is left marked as loaded, and the original exception is rethrown.Test:
LazyLoadProxyTestBase.Lazy_load_reference_failure_after_fixup_is_not_hiddenthrows fromChangeTracker.Trackedonce the parent is tracked during a lazy load, and checks that this exception is the one the caller sees. Onmainit gets theIsLoadedexception instead.Local runs:
EFCore.Tests7004 passed;EFCore.InMemory.FunctionalTests28167 passed, 264 skipped, 0 failed.