Skip to content

Conversation

roji
Copy link
Member

@roji roji commented Sep 12, 2025

Instead of closing over the current scoped logger, causing a leak.

Closes #36464

Description
When generating the shaper (code which reads back results from the database), the current logic wrongly captures the current query logger (which is a scoped service); this means that whatever logger is in use when the query is first compiled gets referenced from the shaper, and stored in our singleton query cache potentially forever (or until evicted). The logger has references ultimately leading to the EF change tracker and through it, meaning that these user objects are kept alive forever (a form of leak).

Customer impact
With specific usage, arbitrary large user objects may be kept alive forever, and never garbage-collected. In addition, the wrong query logger instance may get used (the one from when the query was first compiled, instead of the current one for the query being executed), which could lead to incorrect logging.

We have performed patch servicing for this kind of bug before (because of the leak potential).

How found
Customer report

Regression
Yes, from EF 7 to EF 8; introduced in #31100.

Testing
This is unfortunately difficult/possible to test automatically, as it's about an unwanted reference in the shaper keeping objects alive. Tested manually to ensure that the shaper no longer contains the reference.

Risk
Low. The change is simple, and we have ample code coverage for the affected codepaths.

Instead of closing over the current scoped logger, causing a leak.

Closes dotnet#36464
@roji roji requested a review from a team as a code owner September 12, 2025 09:23
@roji roji changed the title Resolve query logger from QueryContext in shaper generation [rc2] Resolve query logger from QueryContext in shaper generation Sep 12, 2025
@roji roji added the ask-mode label Sep 12, 2025
@AndriySvyryd AndriySvyryd requested a review from artl93 September 12, 2025 18:14
Copy link
Member

@artl93 artl93 left a comment

Choose a reason for hiding this comment

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

RC2 approved. Customer reported-regression.

@roji roji merged commit b7996be into dotnet:release/10.0 Sep 12, 2025
7 checks passed
@roji roji deleted the JsonLeak branch September 12, 2025 21:48
roji added a commit to roji/efcore that referenced this pull request Sep 14, 2025
…6758)

Instead of closing over the current scoped logger, causing a leak.

Closes dotnet#36464

(cherry picked from commit b7996be)
roji added a commit to roji/efcore that referenced this pull request Sep 14, 2025
…6758)

Instead of closing over the current scoped logger, causing a leak.

Closes dotnet#36464

(cherry picked from commit b7996be)
roji added a commit to roji/efcore that referenced this pull request Sep 14, 2025
…6758)

Instead of closing over the current scoped logger, causing a leak.

Closes dotnet#36464

(cherry picked from commit b7996be)
roji added a commit that referenced this pull request Sep 15, 2025
…36779)

Instead of closing over the current scoped logger, causing a leak.

Closes #36464

(cherry picked from commit b7996be)
roji added a commit that referenced this pull request Sep 15, 2025
…36778)

Instead of closing over the current scoped logger, causing a leak.

Closes #36464

(cherry picked from commit b7996be)
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.

Utf8JsonReaderManager leaks memory via reference to IDiagnosticsLogger<DbLoggerCategory.Query>

4 participants