Skip to content

Conversation

roji
Copy link
Member

@roji roji commented Sep 14, 2025

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

Closes #36464, backports #36758 to 9.0

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; quirk added.

…6758)

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

Closes dotnet#36464

(cherry picked from commit b7996be)
@roji roji requested a review from a team as a code owner September 14, 2025 07:37
@roji roji changed the base branch from release/10.0 to release/9.0-staging September 14, 2025 07:41
@roji roji merged commit efb7602 into dotnet:release/9.0-staging Sep 15, 2025
6 checks passed
@roji roji deleted the JsonLeak9 branch September 15, 2025 07:07
This was referenced Oct 14, 2025
This was referenced Oct 15, 2025
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>

2 participants