Skip to content

ControlledExecution.Run - abort delivered at the first instruction of a try region skips its catch/finally #138208

Description

@makemefeelgr8

Description

When ControlledExecution.Run aborts a thread, and the abort is delivered while that thread is at the first instruction of a try region, that region's catch and finally handlers don't run. The ThreadAbortException gets dispatched as if it had been raised just before the try.

More often than not, JIT places a loop head at the start of a try region, like: try { while (...) { Call(); } }. So a tight loop that calls a method hits this in roughly 20% of aborts. A lock statement around the loop never releases the lock, and using/finally cleanup never happens.

The bug is in the "new" managed exception handling. With DOTNET_LegacyExceptionHandling=1 on net9 the same repro never skips the handler.

Reproduction Steps

using System;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;

Console.WriteLine(RuntimeInformation.FrameworkDescription + " " + RuntimeInformation.RuntimeIdentifier);

const int Iterations = 100;
int skipped = 0;
for (int i = 0; i < Iterations; i++)
{
    Repro.FinallyRan = false;
    using var cts = new CancellationTokenSource();
    using var started = new ManualResetEventSlim();
    var thread = new Thread(() =>
    {
        try
        {
            ControlledExecution.Run(() => { started.Set(); Repro.Loop(); }, cts.Token);
        }
        catch (OperationCanceledException) { }
    });
    thread.Start();
    started.Wait();
    Thread.Sleep(10);
    cts.Cancel();
    thread.Join();
    if (!Repro.FinallyRan) skipped++;
}

Console.WriteLine($"finally skipped in {skipped} of {Iterations} aborts");
return skipped == 0 ? 0 : 1;

static class Repro
{
    public static volatile bool FinallyRan;

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Work() { }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static void Loop()
    {
        try
        {
            while (true)
            {
                Work();
            }
        }
        finally
        {
            FinallyRan = true;
        }
    }
}

Expected behavior

"finally skipped in 0 of 100 aborts" in console. ControlledExecution.Run runs the finally blocks. The docs state the abort is an exception whose catch and finally blocks are called.

Actual behavior

Runtime Configuration finally skipped
.NET 11.0.0 win-x64 default 16, 18, 18 of 100
.NET 11.0.0 win-x64 DOTNET_TieredCompilation=0 26 of 100
.NET 10.0.15 win-x64 default 16, 18 of 100
.NET 9.0.23 win-x64 default 19, 25 of 100
.NET 9.0.23 win-x64 DOTNET_LegacyExceptionHandling=1 0, 0 of 100

Regression?

Yes. It works with DOTNET_LegacyExceptionHandling=1 on .NET 9.

Known Workarounds

None

Configuration

  • dotnet 11.0.0, dotnet 10.0.15, dotnet 9.0.23. All of them win-x64, Release builds
  • Windows 11 Pro 25H2 x64

Other information

A possible fix seems rather simple. Like, don't adjust the control PC when the first managed frame was reached through a resumable frame that holds an interrupted context (RedirectedThreadFrame). Or just mark that frame so hasFaulted/isIPadjusted is set.

Exceptions thrown from GC-suspension redirects, like a pending abort raised by HandleThreadAbort after PulseGCMode, take the same path, so this issue is not limited to ControlledExecution. The issue seems to be pretty widespread, actually. This looks like a root cause of at least those 3, and perhaps a lot more:

Activity

  1. added
    untriaged
    on Nov 24, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 24, 2026
  3. dotnet-policy-service commented on Nov 24, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  4. added
    JIT-EH
    and removed
    untriaged
    on Nov 25, 2026
  5. added this to the 12.0.0 milestone on Nov 25, 2026
  6. AndyAyersMS commented on Nov 25, 2026

    @AndyAyersMS
    Member

    I can take a look, but this is likely a runtime issue. FYI @janvorli

  7. janvorli commented on Nov 25, 2026

    @janvorli
    Member

    @AndyAyersMS this seems to really be runtime issue, I am taking the issue.

  8. added a commit that references this issue on Nov 26, 2026
    7b1c31a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

JIT-EHarea-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions