Repository navigation
Description
TL;DR: With runtime async,
await mi.M()andmi.M().Resultrun different overrides on the same object. From reading the code, I suspect the type loader's slot fixup for async variants. I reproduced the failure on #132425's head and its main parent. I'd like to work on a fix; does the sketch under Other information look right?
Description
From reading the source, I suspect the type loader's slot fixup for async variants rather than the JIT's compilation of runtime-async methods. The fixup is in ClassLoader::PropagateCovariantReturnMethodImplSlots (class.cpp). I have not instrumented the loader. The IL-only version below fails the same way with tiering, JIT stress, TieredPGO and ReadyToRun settings changed (see Configuration).
The shape comes from ordinary C# versioning:
B.M()isasync Task M().- In version 1 of a library,
Mi : Bdoes not overrideM. In version 2,Mioverrides it covariantly withasync Task<int> M(), and Roslyn givesMi.Ma[PreserveBaseOverrides]MethodImpl namingB::M. DA : Mi, in another assembly compiled against version 1, overridesMwith a return type ofTask<int>or a type derived from it. The onlyMit could see wasB.M, so Roslyn givesDA.Ma[PreserveBaseOverrides]MethodImpl namingB::Mas well.
At run time, with version 2 loaded and Mi mi holding a DA:
mi.M().ResultrunsDA.M.await ((B)mi).M()runsDA.M. For an override returning a type derived fromTask<int>, Fix covariant returns scenarios in task-returning methods #132425 made this await reachDA.M.await mi.M()runsMi.Mand returnsMi.M's value.
So a runtime-async caller and an ordinary caller of the same virtual method, on the same object, run different bodies.
Reproduction Steps
Library, version 2 (libv2.cs). For version 1 (libv1.cs), replace the Mi declaration below with public class Mi : B { }.
using System.Threading.Tasks;
public static class T
{
public static string Trace;
public static void Add(string s) => Trace += s;
}
public class B
{
public virtual async Task M() { T.Add("B.M;"); }
}
public class Mi : B
{
public override async Task<int> M() { T.Add("Mi.M;"); return 10; }
}Derived assembly (derived.cs), compiled against version 1. DB is a control whose override returns Task<int> itself. It writes the same trace string as DA.
using System;
using System.Threading.Tasks;
public class MyTaskInt : Task<int>
{
public MyTaskInt(Func<int> f) : base(f) => RunSynchronously();
}
public class DA : Mi
{
public override MyTaskInt M() => new MyTaskInt(() => { T.Add("DA.M;"); return 42; });
}
public class DB : Mi
{
public override Task<int> M() { T.Add("DA.M;"); return Task.FromResult(42); }
}Program (app.cs), compiled against version 2 and the derived assembly:
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
public static class App
{
[MethodImpl(MethodImplOptions.NoInlining)] static async Task<int> AwaitMi(Mi m) => await m.M();
[MethodImpl(MethodImplOptions.NoInlining)] static async Task AwaitB(B b) => await b.M();
static int failures;
static void Check(string name, int got, int expect)
{
bool ok = got == expect && T.Trace == "DA.M;";
if (!ok) failures++;
Console.WriteLine("SCENARIO " + name + " " + (ok ? "PASS" : "FAIL") + " value=" + got + " trace=" + T.Trace);
T.Trace = null;
}
static void Run(string label, Mi m)
{
Check(label + "-Mi.M()-Result", m.M().Result, 42);
Check(label + "-await-Mi.M()", AwaitMi(m).GetAwaiter().GetResult(), 42);
AwaitB(m).GetAwaiter().GetResult();
Check(label + "-await-B.M()", 0, 0);
}
public static int Main()
{
bool always = DateTime.Now.Year > 0;
Run("DA-MyTaskInt", always ? new DA() : new Mi());
Run("DB-TaskInt-control", always ? new DB() : new Mi());
return failures == 0 ? 100 : 101;
}
}I ran these steps from a script. Here csc stands for the repository SDK's csc.dll run with dotnet exec, with the options -nologo -nostdlib -noconfig -langversion:preview -features:runtime-async=on -optimize+ and references to System.Private.CoreLib.dll, System.Runtime.dll and System.Console.dll from Core_Root.
mkdir -p v1 v2 run
csc -target:library -out:v1/Lib.dll libv1.cs
csc -target:library -out:v2/Lib.dll libv2.cs
csc -target:library -out:v1/Derived.dll -r:v1/Lib.dll derived.cs
csc -target:exe -out:run/App.dll -r:v2/Lib.dll -r:v1/Derived.dll app.cs
cp v2/Lib.dll v1/Derived.dll run/
cd run && "$CORE_ROOT/corerun" App.dllThe same thing happens without a second assembly. I also wrote an IL-only version with B, Mi and DA1 in one assembly. There, DA1.M returns MyTaskInt and has a [PreserveBaseOverrides] MethodImpl naming B::M. await through Mi::M runs Mi.M on both #132425's head and its main parent, in every configuration I tried (listed under Configuration).
Expected behavior
All three calls shown above should run the derived override on a DA or DB object. That is what the comment above PropagateCovariantReturnMethodImplSlots (class.cpp, lines 1487-1515) says [PreserveBaseOverrides] ensures. Each line should end PASS ... trace=DA.M;, and the program should exit with 100.
Actual behavior
Output on a Checked build of #132425's head; the program exits with 101:
SCENARIO DA-MyTaskInt-Mi.M()-Result PASS value=42 trace=DA.M;
SCENARIO DA-MyTaskInt-await-Mi.M() FAIL value=10 trace=Mi.M;
SCENARIO DA-MyTaskInt-await-B.M() PASS value=0 trace=DA.M;
SCENARIO DB-TaskInt-control-Mi.M()-Result PASS value=42 trace=DA.M;
SCENARIO DB-TaskInt-control-await-Mi.M() FAIL value=10 trace=Mi.M;
SCENARIO DB-TaskInt-control-await-B.M() PASS value=0 trace=DA.M;
On a Checked build of its main parent, the output is the same except that DA-MyTaskInt-await-B.M() also fails, with trace=Mi.M;.
Regression?
It is not caused by #132425: the Mi-typed await fails on its main parent too, for both DA and DB. #132425 fixed the B-typed await for the covariant DA case, but not the Mi-typed one. I have not tried a released runtime.
Known Workarounds
In the IL-only version, a DA2 whose MethodImpl names Mi::M instead of B::M passes on #132425's head. I expect that is what C# emits when the derived assembly is compiled against version 2, so recompiling it should avoid the bug. I have not run that C# variant.
Configuration
- Checked linux-x64 runtimes, built with
./build.sh clr+libs -rc Checked -lc Release, with Core_Root fromsrc/tests/build.sh -Checked x64. One build is Fix covariant returns scenarios in task-returning methods #132425's head,4557409c1bdeced5ea44af0efae38e39702aa1af. The other is its main parent,b0643c8d83462ab9c5ca0b43ed4ef8fac4a00038. - Compiler: the Roslyn
csc.dllin the repository's SDK,12.0.100-alpha.1.26480.102. - I ran the C# program once per build, with
DOTNET_*variables removed from its environment. In the IL-only version,awaitthroughMi::Mon aDA1object fails withvalue=10 trace=Mi.M;on both builds with default settings and with each ofTieredCompilation=0,TC_QuickJitForLoops=0,JitStress=2,TieredPGO=0,ReadyToRun=0,GCStress=0xC,GCStress=0x3, andGCStress=0xCwithReadyToRun=0. - I have not tried another OS or architecture.
Other information
The likely cause is below. I found it by reading the code; I have not instrumented it. The links are at #132425's merge commit, 681cd3ab4a8.
class.cpp, lines 1581-1595: for each parent slotithat the type overrides with a[PreserveBaseOverrides]MethodImpl, the fixup rewrites every slotjthat still holds the parent's MethodDesc for sloti. ForB.M's ordinary slot, the parent's MethodDesc isMi.M, soMi.M's own ordinary slot is rewritten. That is why.Resultworks.- For
B.M's async variant slot, which returnsvoid, the parent's MethodDesc isMi.M's return-dropping thunk (methodtablebuilder.cpp, lines 3770-3771).Mi.M'sint-returning async variant is a different MethodDesc in a different slot, so the fixup never rewrites it. AnawaitthroughMi::Muses that slot. - No derived override exists for
Mi.M'sint-returning async variant either.DBdoes get anint-returning async variant, but its MethodImpl namesB::M, so the variant's declaration lookup findsB'svoidvariant. Because the signatures differ, the code in lines 6389-6407 skipsDB'sint-returning variant, which then overrides nothing.DAgets only the covariant forwarding thunk. It takes its return type from the MethodImpl's declarationB::M, so it returnsvoid(lines 3667-3673).
Fix sketch, not tried. When the fixup propagates an override into an ordinary slot, it could also rewrite the parent's async variant of that ordinary method. It would use the derived type's variant with the same return shape. The derived type then needs that variant. For DB, its existing int variant would override Mi's. For DA, the loader would add an int-returning forwarding thunk besides the void one.
I prepared this issue with AI assistance (Anthropic Claude, reviewed by OpenAI Codex) and reviewed it before filing.
Activity
dotnet-policy-service commentedon Nov 5, 2026
Tagging subscribers to this area: @dotnet/area-system-threading-tasks
See info in area-owners.md if you want to be subscribed.
pcshrosbree commentedon Nov 5, 2026
@VSadov, which direction would you prefer for the
DAcase below?I confirmed the cause with a temporary vtable dump on a Checked linux-x64 build of main at a69ba15. The dump used the issue's shapes in one IL assembly.
Mi.Mhas three slots of its own: the ordinary method, itsintasync variant and itsvoidreturn-dropping thunk. For bothDAandDB,PropagateCovariantReturnMethodImplSlotsrewrites the ordinary slot and the thunk's slot, but not theintvariant's slot, soawait mi.M()still runsMi.M. One correction to the issue: theintasync variants ofMi.MandDB.Mare not MethodImpls.The
Task<int>shape (DB) can be fixed in two parts. The slot loop moves slots from the parent's method to the override. It can also point the slot that holds the parent's async variant at the override's async variant, when both returnvoidor both return a value. ThenChangesImplementationOfVirtualSlothas to count that inherited slot as changed, so its vtable chunk is not shared with the parent. With a prototype of both changes,DBpasses all three call forms.The covariant shape (
DA) has nointvariant to put in that slot. Since #132425,DA.Mhas one async variant: a covariant forwarding thunk. The thunk takes its return type from the MethodImpl's declaration,B::M(here), so it returnsvoid. I see two ways to giveDA.Manintvariant:DA.M's own return type,MyTaskInt : Task<int>. The signature ofDA.Mdoes not containTask<int>. Finding it means walkingMyTaskInt's base types while the method table is being built. I expect that walk to need a type load. The method would also get two variants, anintforwarding thunk and avoidreturn-dropping thunk: the same two return shapesDB.Mhas.Mi.M, which is the parent's implementation of the declared slot. The variant's signature is built before the MethodImpl declarations are resolved, so this means findingMi.Mearlier. A class or struct element type would also need translating fromMi's module intoDA's.A third option is to land the
Task<int>fix first and leaveDAopen here.I'll put up a PR once the direction is clear.