Repository navigation
Description
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:
- ThreadAbortException raised again after a catch can skip a finally and reaches different handlers in tier-0 and optimized code #135637
- [Interpreter] A thread abort raised again after a catch skips the catch of the enclosing try #135871
- [wasm] Thread abort is raised inside finally and catch handlers of R2R code, so ControlledExecution.Run lets ThreadAbortException escape #135914
Activity
dotnet-policy-service commentedon Nov 24, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
AndyAyersMS commentedon Nov 25, 2026
I can take a look, but this is likely a runtime issue. FYI @janvorli
janvorli commentedon Nov 25, 2026
@AndyAyersMS this seems to really be runtime issue, I am taking the issue.
Fix ThreadAbort at first instruction of try block (dotnet#138306)