Skip to content

Don't hide lazy loading failures behind the IsLoaded exception - #39387

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
AsadIqbal-Dev:fix/lazy-load-hidden-exception
Oct 23, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
AsadIqbal-Dev:fix/lazy-load-hidden-exception

Conversation

@AsadIqbal-Dev

@AsadIqbal-Dev AsadIqbal-Dev commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor
  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Fixes #34167

When a lazy load failed, LazyLoader.Load/LoadAsync set entry.IsLoaded = false before 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 throws ReferenceMustBeLoaded ("cannot have 'IsLoaded' set to false because the referenced entity is non-null"). The new exception replaced the one that made the load fail.

IsLoaded is 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_hidden throws from ChangeTracker.Tracked once the parent is tracked during a lazy load, and checks that this exception is the one the caller sees. On main it gets the IsLoaded exception instead.

Local runs: EFCore.Tests 7004 passed; EFCore.InMemory.FunctionalTests 28167 passed, 264 skipped, 0 failed.

- 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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 IsLoaded reset 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 AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd enabled auto-merge (squash) October 23, 2026 00:28
@AndriySvyryd
AndriySvyryd merged commit dc5f6f3 into dotnet:main Oct 23, 2026
18 of 19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hidden Exception in LazyLoader.Load(..) Due to exception in InternalEntry.SetIsLoaded(..)

3 participants