-
Notifications
You must be signed in to change notification settings - Fork 539
LINQ: Fixes memory leak from Expression.Compile() in all call sites #5588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kirankumarkolli
merged 19 commits into
master
from
users/kirankk/copilot-5487-fix-linq-memory-leak
Mar 29, 2026
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6c3f73a
LINQ: Fixes memory leak from Expression.Compile() in SubtreeEvaluator
kirankumarkolli b6f882f
Move benchmark test to Performance.Tests project
kirankumarkolli 8ec3864
Making lambda runtime selection instead of comile time
kirankumarkolli f4055b6
Merge branch 'master' into users/kirankk/copilot-5487-fix-linq-memory…
kirankumarkolli a866fdf
Benchmark: use SubtreeEvaluator directly instead of duplicating fix
kirankumarkolli 6110c1a
Merge branch 'master' into users/kirankk/copilot-5487-fix-linq-memory…
kirankumarkolli d1445f4
LINQ: Apply Compile(preferInterpretation) fix to all Expression.Compi…
kirankumarkolli 6d34f4a
Merge PR #5703 into #5588: consolidate all Expression.Compile() fixes
kirankumarkolli c063cf1
Removing not needed files
kirankumarkolli aa12c80
Add ENV variable feature flag for LINQ expression interpretation mode
kirankumarkolli 698cce0
Move feature flag check to CompileLambda for runtime toggling
kirankumarkolli e5390e6
Cache ENV variable read at static init for zero per-call overhead
kirankumarkolli 2e0c232
Address review: add generic CompileLambda<T> and memory benchmark
kirankumarkolli 72b8a02
Address review: add generic CompileLambda<T>, E2E query generation be…
kirankumarkolli 67aeab1
Simplify: remove ExpressionCompileHelper, inline Compile(preferInterp…
kirankumarkolli 90d39db
Merge branch 'master' into users/kirankk/copilot-5487-fix-linq-memory…
kirankumarkolli 469230c
Add guard test to prevent bare .Compile() in LINQ source files
kirankumarkolli e4de241
Removing dead code
kirankumarkolli bd23026
Adding NativeMemoryProfiler
kirankumarkolli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
51 changes: 51 additions & 0 deletions
51
...e.Cosmos/tests/Microsoft.Azure.Cosmos.Performance.Tests/Linq/SubtreeEvaluatorBenchmark.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // ---------------------------------------------------------------- | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // ---------------------------------------------------------------- | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Linq | ||
| { | ||
| using System; | ||
| using System.Linq.Expressions; | ||
| using BenchmarkDotNet.Attributes; | ||
| using BenchmarkDotNet.Diagnostics.Windows.Configs; | ||
|
|
||
| /// <summary> | ||
| /// Benchmark comparing Expression.Compile() vs Compile(preferInterpretation: true) | ||
| /// in the context of CosmosDB LINQ-to-SQL query generation. | ||
| /// Validates fix for GitHub Issue #5487: Unbounded JIT/IL growth from Expression.Compile(). | ||
| /// | ||
| /// [MemoryDiagnoser] reports managed GC allocations (Gen0/Gen1/Allocated columns). | ||
| /// [NativeMemoryProfiler] uses ETW to track native memory allocations and leaks per method, | ||
| /// adding "Allocated native memory" and "Native memory leak" columns to the results table. | ||
| /// Note: NativeMemoryProfiler requires Windows and elevated (admin) privileges. | ||
| /// </summary> | ||
| [ShortRunJob] | ||
| [MemoryDiagnoser] | ||
| // [NativeMemoryProfiler] // Enable this line to include native memory profiling, requires Windows and admin privileges. | ||
| public class SubtreeEvaluatorBenchmark | ||
|
kirankumarkolli marked this conversation as resolved.
|
||
| { | ||
| private LambdaExpression lambda; | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| string status = "active"; | ||
| Expression<Func<bool>> expr = () => status == "active"; | ||
| this.lambda = Expression.Lambda(expr.Body); | ||
| } | ||
|
|
||
| [Benchmark(Baseline = true)] | ||
| public object Compile() | ||
| { | ||
| Delegate fn = this.lambda.Compile(); | ||
| return fn.DynamicInvoke(null); | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public object CompileWithInterpretation() | ||
| { | ||
| Delegate fn = this.lambda.Compile(preferInterpretation: true); | ||
| return fn.DynamicInvoke(null); | ||
| } | ||
| } | ||
| } | ||
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
75 changes: 75 additions & 0 deletions
75
Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Linq/LinqCompileGuardTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| //------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| //------------------------------------------------------------ | ||
|
|
||
| namespace Microsoft.Azure.Cosmos.Linq | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text.RegularExpressions; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
|
||
| /// <summary> | ||
| /// Guards against introducing bare Expression.Compile() calls in the LINQ provider. | ||
| /// All Compile() calls must use Compile(preferInterpretation: true) to avoid native | ||
| /// memory leaks from DynamicMethod IL emission. | ||
| /// See: https://github.com/Azure/azure-cosmos-dotnet-v3/issues/5487 | ||
| /// </summary> | ||
| [TestClass] | ||
| public class LinqCompileGuardTests | ||
| { | ||
| // Matches .Compile() with no arguments — the problematic pattern | ||
| private static readonly Regex BareCompilePattern = new Regex( | ||
| @"\.Compile\(\s*\)", | ||
| RegexOptions.Compiled); | ||
|
|
||
| // Matches .Compile(preferInterpretation: true) — the correct pattern | ||
| private static readonly Regex InterpretedCompilePattern = new Regex( | ||
| @"\.Compile\(\s*preferInterpretation\s*:\s*true\s*\)", | ||
| RegexOptions.Compiled); | ||
|
|
||
| [TestMethod] | ||
| public void LinqSourceFiles_ShouldNotUseBareCompile() | ||
| { | ||
| string linqDirectory = Path.GetFullPath( | ||
| Path.Combine( | ||
| Directory.GetCurrentDirectory(), | ||
| "..", "..", "..", "..", "..", | ||
| "src", "Linq")); | ||
|
|
||
| Assert.IsTrue( | ||
| Directory.Exists(linqDirectory), | ||
| $"LINQ source directory not found at: {linqDirectory}"); | ||
|
|
||
| string[] sourceFiles = Directory.GetFiles(linqDirectory, "*.cs", SearchOption.AllDirectories); | ||
| Assert.IsTrue(sourceFiles.Length > 0, "No source files found in LINQ directory."); | ||
|
|
||
| List<string> violations = new List<string>(); | ||
|
|
||
| foreach (string file in sourceFiles) | ||
| { | ||
| string[] lines = File.ReadAllLines(file); | ||
| string fileName = Path.GetFileName(file); | ||
|
|
||
| for (int i = 0; i < lines.Length; i++) | ||
| { | ||
| string line = lines[i]; | ||
|
|
||
| if (BareCompilePattern.IsMatch(line) && !InterpretedCompilePattern.IsMatch(line)) | ||
| { | ||
| violations.Add($" {fileName}:{i + 1} => {line.Trim()}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Assert.AreEqual( | ||
| 0, | ||
| violations.Count, | ||
| $"Found bare .Compile() calls without preferInterpretation: true. " + | ||
| $"Use .Compile(preferInterpretation: true) to avoid native memory leaks " + | ||
| $"(see issue #5487):\n{string.Join("\n", violations)}"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.