From f2dbc6c30037e17e339d0b045a6a762c396af380 Mon Sep 17 00:00:00 2001 From: Reuben Bond Date: Sun, 22 Nov 2026 17:06:23 -0800 Subject: [PATCH] fix: keep ConfigureAwait(options) validation frame in controlled awaitables The controlled ConfigureAwait(ConfigureAwaitOptions) awaitable constructors are [StackTraceHidden] and validate through the BCL's ConfigureAwait. Once the JIT optimizes them (Tier1 in a long sequential run, or TieredCompilation=0), it inlines ConfigureAwait into the hidden frame, so the BCL's frame disappears from the trace of invalid options. NoInlining | NoOptimization keeps the BCL call a real frame. The awaiter conformance probe disables JIT optimization so the BCL oracle has the frames of a non-inlined call in every run, and CI runs the awaiter conformance class sequentially without tiered compilation. Fixes #79 --- .github/workflows/ci.yml | 8 ++++++++ docs/compatibility.md | 3 ++- .../CompilerServices/ControlledConfiguredTaskAwaitable.cs | 8 ++++++-- .../ControlledAwaiterConformanceTests.cs | 5 +++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99aed30..bdcc3ac 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -50,6 +50,14 @@ jobs: DOTNET_TieredCompilation: '0' DOTNET_ReadyToRun: '0' run: dotnet run --project tests/Clockwork.Conformance.Tests/Clockwork.Conformance.Tests.csproj --configuration Debug --no-build -- --timeout 300s --filter-class Orleans.Testing.Simulation.Conformance.Tests.ChannelConformanceTests + - name: Test awaiter conformance sequentially without tiered compilation + # Fully optimized Release runtime code is what a long sequential run reaches through tiering: the controlled + # ConfigureAwait(options) awaitables must keep the BCL's validation frame instead of inlining it into a hidden + # frame (#79). + env: + DOTNET_TieredCompilation: '0' + DOTNET_ReadyToRun: '0' + run: dotnet run --project tests/Clockwork.Conformance.Tests/Clockwork.Conformance.Tests.csproj --configuration Release --no-build -- --timeout 300s --parallel none --filter-class Orleans.Testing.Simulation.Conformance.Tests.ControlledAwaiterConformanceTests - name: Package smoke tests # Exercises all seven real packed NuGet artifacts end-to-end in an isolated feed, # verifies evaluated/restored/packed dependency boundaries and the source-build map, diff --git a/docs/compatibility.md b/docs/compatibility.md index b803604..ae99877 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -591,7 +591,8 @@ through which an async `Main` returns (`task.GetAwaiter().GetResult()`), waits t `Task.ConfigureAwait(ConfigureAwaitOptions)` and `Task.ConfigureAwait(ConfigureAwaitOptions)` build the same controlled awaitables: `SuppressThrowing` completes the await normally for a faulted or canceled `Task`, `ForceYielding` always yields through the coordinator, and invalid options (including -`SuppressThrowing` on `Task`) throw the BCL's `ArgumentOutOfRangeException`. As in the BCL, +`SuppressThrowing` on `Task`) throw the BCL's `ArgumentOutOfRangeException` from the BCL's +`ConfigureAwait` frame, as a call the JIT does not inline does. As in the BCL, `GetAwaiter()` or `ConfigureAwait` on a null task throws `NullReferenceException` and the controlled awaiters keep their frames out of a rethrown exception's stack trace. A synchronous `GetAwaiter().GetResult()` on an incomplete task or value task pumps the coordinator until it completes diff --git a/src/Orleans.Testing.Simulation/Runtime/Shims/System/Runtime/CompilerServices/ControlledConfiguredTaskAwaitable.cs b/src/Orleans.Testing.Simulation/Runtime/Shims/System/Runtime/CompilerServices/ControlledConfiguredTaskAwaitable.cs index b3f87b0..9b68c11 100644 --- a/src/Orleans.Testing.Simulation/Runtime/Shims/System/Runtime/CompilerServices/ControlledConfiguredTaskAwaitable.cs +++ b/src/Orleans.Testing.Simulation/Runtime/Shims/System/Runtime/CompilerServices/ControlledConfiguredTaskAwaitable.cs @@ -39,12 +39,14 @@ public ControlledConfiguredTaskAwaitable(Task task, bool continueOnCapturedConte /// /// is invalid for . [StackTraceHidden] + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] public ControlledConfiguredTaskAwaitable(Task task, ConfigureAwaitOptions options) { SimulationRuntimeDispatch.RequireActiveSimulation(ApiName); // The BCL call dereferences the task and validates the options, so a null task and invalid options - // throw the BCL's own exceptions. + // throw the BCL's own exceptions. This frame is hidden, so the JIT must not inline the BCL call into + // it: invalid options are thrown from the BCL's ConfigureAwait frame, as from a non-inlined call. _ = task.ConfigureAwait(options); _task = task; _options = options; @@ -146,12 +148,14 @@ public ControlledConfiguredTaskAwaitable(Task task, bool continueOnCapt /// is invalid for , including . /// [StackTraceHidden] + [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)] public ControlledConfiguredTaskAwaitable(Task task, ConfigureAwaitOptions options) { SimulationRuntimeDispatch.RequireActiveSimulation(ApiName); // The BCL call dereferences the task and validates the options, so a null task and invalid options - // throw the BCL's own exceptions. + // throw the BCL's own exceptions. This frame is hidden, so the JIT must not inline the BCL call into + // it: invalid options are thrown from the BCL's ConfigureAwait frame, as from a non-inlined call. _ = task.ConfigureAwait(options); _task = task; _options = options; diff --git a/tests/Clockwork.Conformance.Tests/ControlledAwaiterConformanceTests.cs b/tests/Clockwork.Conformance.Tests/ControlledAwaiterConformanceTests.cs index f890db1..0667980 100644 --- a/tests/Clockwork.Conformance.Tests/ControlledAwaiterConformanceTests.cs +++ b/tests/Clockwork.Conformance.Tests/ControlledAwaiterConformanceTests.cs @@ -15,8 +15,13 @@ public sealed class ControlledAwaiterConformanceTests : IDisposable { private static readonly DateTimeOffset Start = new(2024, 1, 1, 0, 0, 0, TimeSpan.Zero); + // An optimizing JIT may inline the BCL's ConfigureAwait into its caller, removing that frame from the BCL's + // trace depending on the caller's tier. The probe disables JIT optimization so the BCL trace has the frames + // of a non-inlined call in every run, as the controlled awaitable produces. private const string Source = """ + using System.Diagnostics; using System.Threading.Tasks; + [assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations)] namespace Conf { public static class AwaiterProbe { public static async Task Fail() {