Skip to content

10.x Trimming removes a nested finally from an async method #137735

Open

Description

@blowdart

Description

A trimmed Release publish omits the inner finally below. An untrimmed publish releases the semaphore; the trimmed executable leaves it locked.

Reproduction Steps

Create a .NET 10 console project and replace Program.cs with:

using SemaphoreSlim gate = new(1, 1);
await SaveAsync();
Console.WriteLine($"gate count: {gate.CurrentCount}");
bool acquired = await gate.WaitAsync(TimeSpan.FromSeconds(1));
Console.WriteLine(acquired ? "acquired" : "timed out");
return acquired ? 0 : 1;

async Task SaveAsync()
{
    await gate.WaitAsync();
    try
    {
        await Task.Yield();
    }
    finally
    {
        try
        {
            Console.WriteLine("cleanup");
        }
        finally
        {
            gate.Release();
            Console.WriteLine("released");
        }
    }
}

Publish and run both versions on Windows:

dotnet publish -c Release -r win-x64 --self-contained true -p:PublishTrimmed=false -o .\untrimmed
.\untrimmed\TrimRepro.exe

dotnet publish -c Release -r win-x64 --self-contained true -p:PublishTrimmed=true -o .\trimmed
.\trimmed\TrimRepro.exe

Expected behavior

Both print released, gate count: 1, and acquired, then exit 0.

Actual behavior

The trimmed executable prints cleanup, gate count: 0, and timed out, then exits 1.

Regression?

No response

Known Workarounds

Moving the nested cleanup into a synchronous helper worked around the issue in the original application.

Configuration

Reproduced on Windows x64 with SDKs 10.0.404 and 11.0.100, targeting .NET 10 with Microsoft.NET.ILLink.Tasks 10.0.15.

Other information

The issue repro does not fail on .NET 11. Both trimmed and untrimmed publishes released the semaphore and exited successfully.

Tested with SDK 11.0.100, runtime 11.0.0, and ILLink 11.0.0 on Windows x64.

Potentially same as #136414 ?

Activity

  1. added
    area-Tools-ILLink
    on Nov 17, 2026
  2. added
    untriaged
    on Nov 17, 2026
  3. dotnet-policy-service commented on Nov 17, 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.

  4. MihuBot commented on Nov 17, 2026

    @MihuBot
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