Skip to content

ILLink removes Monitor.Exit from async finally after empty filtered catch (.NET 10) #136414

Open

Description

@TechJosh

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/Repro

Expected 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.

Activity

  1. added
    untriaged
    on Oct 24, 2026
  2. dotnet-policy-service commented on Oct 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-threading-tasks
    See info in area-owners.md if you want to be subscribed.

  3. added
    area-Tools-ILLink
    on Oct 24, 2026
  4. dotnet-policy-service commented on Oct 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @agocke, @dotnet/illink
    See info in area-owners.md if you want to be subscribed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Tools-ILLinkuntriaged

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions