Repository navigation
Description
Description
System.Runtime.ControlledExecution.Run aborts a given thread only once on Linux and macOS. When the same thread calls Run a second time with a token that cancels while the delegate is running, the abort never lands: the thread stays in the delegate's loop until the process exits. The same program on Windows aborts every time.
Two shapes show it:
- A dedicated thread calling
Runthree times in sequence, each with a fresh 500 ms token. Linux and macOS:#1 ABORTED, then the thread never leaves the secondRun. Windows:#1 #2 #3 ABORTED. - Sequential
Task.Run(body)calls, each body one cancelledRun, awaited one at a time. Linux and macOS: the second body never aborts (the pool hands it the thread the first one ran on). Windows: all four abort.
A fresh dedicated Thread per Run aborts every time on all three OSes (four in a row in one process). The first abort in a process lands on every OS in every variant tried: default JIT settings, DOTNET_TC_QuickJitForLoops=0, DOTNET_TieredCompilation=0, on a dedicated thread and on a pool thread.
Context: a desktop authoring tool runs user-written scripts under a hard timeout, the REPL-style use ControlledExecution is documented for. On Linux and macOS the first runaway script on the UI thread is stopped and the second one hangs the application for good.
Reproduction Steps
Program.cs (top-level statements), run with dotnet run -c Release:
using System;
using System.Threading;
var results = new System.Collections.Generic.List<string>();
var th = new Thread(() =>
{
for (int i = 1; i <= 3; i++)
{
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));
string outcome = "?";
try
{
#pragma warning disable SYSLIB0046
System.Runtime.ControlledExecution.Run(() => { while (true) { } }, cts.Token);
#pragma warning restore SYSLIB0046
outcome = "RETURNED";
}
catch (OperationCanceledException) { outcome = "ABORTED"; }
catch (Exception e) { outcome = "OTHER:" + e.GetType().Name; }
lock (results) results.Add($"#{i}:{outcome}");
}
}) { IsBackground = true };
th.Start();
bool done = th.Join(TimeSpan.FromSeconds(8));
lock (results) Console.WriteLine(
$"{System.Runtime.InteropServices.RuntimeInformation.OSDescription.Split(' ')[0]} " +
$"{System.Runtime.InteropServices.RuntimeInformation.OSArchitecture} same-thread: " +
$"{string.Join(" ", results)}{(done ? "" : " | then STILL-RUNNING")}");
Environment.Exit(0);<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>disable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
</Project>The thread-pool shape: the same Run as body, a fresh 500 ms token each time, and in place of the thread for (int i = 1; i <= 4; i++) { bool done = Task.Run(body).Wait(TimeSpan.FromSeconds(5)); ...; if (!done) break; }.
Expected behavior
Each Run whose token is cancelled while its delegate runs throws OperationCanceledException on the calling thread, as it does on Windows.
Actual behavior
Same thread, three sequential runs (3 of 3 process launches per OS):
Ubuntu X64 same-thread: #1:ABORTED | then STILL-RUNNING
macOS Arm64 same-thread: #1:ABORTED | then STILL-RUNNING
Microsoft X64 same-thread: #1:ABORTED #2:ABORTED #3:ABORTED
Thread pool, four sequential runs in one process (3 of 3 per OS):
Ubuntu X64 pool: #1:ABORTED #2:STILL-RUNNING
macOS Arm64 pool: #1:ABORTED #2:STILL-RUNNING
Microsoft X64 pool: #1:ABORTED #2:ABORTED #3:ABORTED #4:ABORTED
A fresh dedicated thread per run, four in one process: #1:ABORTED #2:ABORTED #3:ABORTED #4:ABORTED on all three.
Regression?
Unknown. Not tried on earlier runtimes.
Known Workarounds
Run each abortable delegate on a fresh dedicated thread, or in a separate process.
Configuration
- .NET 10.0.14, Ubuntu 24.04 LTS x64 (WSL2, kernel 6.18.54.1)
- .NET 10.0.7, macOS 26.6.2 on Apple Silicon (arm64)
- .NET 10.0.6, Windows 10 22H2 x64 (build 19045): aborts every time
- Release builds, default runtime configuration.
Other information
#96519 is about ControlledExecution not reaching work inside a nested Task and about resiliency in general. This is narrower: one thread, a plain while (true) { } directly in the delegate, no nesting; it works once per thread and never again, and only off Windows.
Activity
Fix ControlledExecution.Run abort behavior on Linux and macOS (dotnet…