Repository navigation
Description
Description
Publishing this async cleanup with trimming leaves a monitor acquired after the task completes. An empty filtered catch immediately precedes a finally containing a lock. The untrimmed build releases the monitor; the trimmed build does not. The precise optimizer trigger has not been bisected.
Observed on Linux x64, .NET SDK 10.0.401, Microsoft.NET.ILLink.Tasks 10.0.12. No third-party packages.
Reproduction
Repro.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>
</Project>Program.cs:
using System;
using System.Threading;
using System.Threading.Tasks;
internal static class Program
{
private static readonly object Gate = new();
private static readonly CancellationTokenSource Stop = new();
private static int _completed;
private static async Task WorkAsync()
{
await Task.Yield();
try { await Task.Delay(1, Stop.Token).ConfigureAwait(false); }
catch (OperationCanceledException) when (Stop.IsCancellationRequested) { }
finally { lock (Gate) { _completed++; } }
}
private static async Task<int> Main()
{
await WorkAsync();
var acquired = false;
var thread = new Thread(() =>
{
acquired = Monitor.TryEnter(Gate, TimeSpan.FromSeconds(1));
if (acquired) { Monitor.Exit(Gate); }
});
thread.Start();
thread.Join();
Console.WriteLine($"completed={_completed}, monitor available={acquired}");
return acquired ? 0 : 1;
}
}From a directory using SDK 10.0.401:
dotnet publish Repro.csproj -c Release -r linux-x64 --self-contained -p:PublishTrimmed=false -o out-untrimmed
./out-untrimmed/Repro
dotnet publish Repro.csproj -c Release -r linux-x64 --self-contained -p:PublishTrimmed=true -o out-trimmed
./out-trimmed/ReproExpected for both: completed=1, monitor available=True, exit 0.
Actual untrimmed: completed=1, monitor available=True, exit 0.
Actual trimmed: completed=1, monitor available=False, exit 1.
The fresh thread prevents reentrant acquisition from hiding the leak. Decoding the linked IL in the application where this was found showed Monitor.Enter retained but the nested Monitor.Exit and its exception handler removed from the affected async MoveNext methods. Moving the locking cleanup into a synchronous method called by the async finally preserves the exit and avoids the hang. Other async methods with locking finally blocks retain balanced monitors; this is not a claim that every async finally is affected.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Oct 24, 2026
Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Oct 24, 2026
Tagging subscribers to this area: @agocke, @dotnet/illink
See info in area-owners.md if you want to be subscribed.
Runic Desktop: start and answer calls when published trimmed