JIT: handler of `catch (T ex) when (...)` sees `ex == null` after Tier1 with PGO, in an async loop (.NET 10 regression)
Description
In an async method with a while loop around try { await ... }, the handler of
catch (OperationCanceledException ex) when (firstFailure is not null) sees ex as null once the state machine's
MoveNext is recompiled at Tier1. The filter does not read ex. The first ~30 calls work; after the Tier1 compile every
call fails until the process restarts.
Reproduction Steps
Repro.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Program.cs:
var nulls = 0;
for (var i = 0; i < 300; i++)
{
try
{
await RetryOnce();
}
catch (TimeoutException)
{
}
catch (NullReferenceException e)
{
if (nulls++ == 0)
Console.WriteLine($"first NullReferenceException at call {i}:\n{e}");
}
}
Console.WriteLine($"{Environment.Version}: {nulls} of 300 calls saw a null catch variable");
return nulls == 0 ? 100 : 1;
static async Task<int> RetryOnce()
{
Exception? firstFailure = null;
var attempt = 0;
while (true)
{
try
{
return await Attempt(attempt++);
}
catch (InvalidOperationException ex)
{
firstFailure = ex;
}
catch (OperationCanceledException ex) when (firstFailure is not null)
{
throw new TimeoutException(ex.GetType().Name, ex);
}
}
}
static async Task<int> Attempt(int attempt)
{
if (attempt == 0)
{
await Task.Yield();
throw new InvalidOperationException();
}
using var cts = new CancellationTokenSource(1);
await Task.Delay(Timeout.Infinite, cts.Token);
return 1;
}
dotnet build -c Release, then dotnet bin/Release/net10.0/Repro.dll.
Expected behavior
0 of 300 calls saw a null catch variable, exit code 100, as on .NET 8 and 9.
Actual behavior
first NullReferenceException at call 32:
System.NullReferenceException: Object reference not set to an instance of an object.
at Program.<<Main>$>g__RetryOnce|0_0() in ...\Program.cs:line 36
at Program.<Main>$(String[] args) in ...\Program.cs:line 6
10.0.14: 268 of 300 calls saw a null catch variable
Line 36 is throw new TimeoutException(ex.GetType().Name, ex);.
| Runtime | Result | MoveNext Tier1 compile (DOTNET_JitDisasmSummary=1) |
|---|---|---|
| 8.0.33 | 0 / 300 | not logged |
| 9.0.22 | 0 / 300 | Tier1 with Dynamic PGO |
| 10.0.12 | 268 / 300 | not logged |
| 10.0.14 | 268 / 300 | Tier1 with Synthesized PGO, right before the first failure |
Regression?
Yes: passes on 8.0.33 and 9.0.22, fails on 10.0.12 and 10.0.14.
Known Workarounds
Each of these alone makes the repro pass on 10.0.14: DOTNET_TieredPGO=0, DOTNET_TieredCompilation=0,
DOTNET_JitDisablePGO=1, DOTNET_JitNoInline=1. These don't: DOTNET_TC_OnStackReplacement=0,
DOTNET_JitObjectStackAllocation=0.
In code, on a larger variant of this repro: test the condition in the handler body and throw; instead of using a
when filter, or make the filter read ex. Debug builds don't reproduce.
Configuration
- Runtime 10.0.12 and 10.0.14 (win-x64 zip from builds.dotnet.microsoft.com), SDK 10.0.401
- Windows 11 Enterprise 10.0.26200, x64
- Release build, default runtime settings
Other information
- Without the
whileloop (two sequentialtry/awaitblocks), and in a synchronous version of the same loop, it did not reproduce in 300 calls. - Possibly related, but they look like different bugs: #131713 (NRE after tier-up from conditional escape analysis; this
one still fails with
DOTNET_JitObjectStackAllocation=0) and #134379 (loop hoisting past a store to a handler-live local; that one fails on .NET 8 too). - Found as a CI failure: Release test runs hit it after ~130 earlier calls to the method in the same process.
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.
Thank you for reporting this. This was fixed in .NET 11 by #129058, but the fix wasn't backported to 10.0.
Let me look into doing that.
In the meantime, as a possible workaround, consider rewriting the filter as a catch rethrow. It does not give the exact the same behavior but might work in your case.
catch (OperationCanceledException ex)
{
if (firstFailure is null)
{
throw;
}
throw new TimeoutException(ex.GetType().Name, ex);
}
@mihainradu, .NET 11 was released on November 10th. Is it an option for you to upgrade to .NET 11? We have a high bar for .NET 10 backports now.
This was fixed in .NET 11 by #129058
@mihainradu, .NET 11 was released on November 10th. Is it an option for you to upgrade to .NET 11? We have a high bar for .NET 10 backports now.
This was fixed in .NET 11 by #129058
Thanks @JulieLeeMSFT, I get that the bar for 10.0 is high now. I'd still push for this one, though: it's a silent miscompile of correct code, and you hit it just by moving the host from .NET 8 to .NET 10, with any package on nuget.org that has this shape: no code change, no rebuild. That's about as basic as a quality bar gets.
Moving to .NET 11 doesn't really help. It's STS, and our released products are on the .NET 10 LTS and stay there. The same goes for everyone else on the LTS: they'd live with this for the rest of 10's support.
To be concrete: a net8.0 or netstandard2.0 library built with the .NET 8 SDK and loaded as-is into a .NET 10 app loses ex the same way, while the same code works fine on 8 and 9. So:
- every .NET 10 app that loads such a package is exposed, and the package author can't fix it by rebuilding
- the shape is a plain retry loop: a catch that continues, then
catch (OperationCanceledException ex) when (!token.IsCancellationRequested) - it only shows up in Release after tier-up, so tests and Debug runs pass; we only caught ours because a Release CI run hit an NRE inside the catch
- the workarounds are process-wide (
DOTNET_JitEnableInductionVariableOpts=0set at startup, or turning off TieredPGO), so a library can't protect itself
We rewrote our own catch the way Andy suggested, but we can't do that for the libraries we load, and nobody else can either. I'd really appreciate it if this could make it into 10.0 servicing.
Thanks @mihainradu for providing justification. Approved for servicing. We will backport to .NET 10.
Thank you, @JulieLeeMSFT . Thank you, @AndyAyersMS .
Thanks @mihainradu for providing justification. Approved for servicing. We will backport to .NET 10.