Skip to content

Runtime async: await and .Result reach different overrides when a [PreserveBaseOverrides] MethodImpl names the grand-base method #137038

Open

Description

@pcshrosbree

TL;DR: With runtime async, await mi.M() and mi.M().Result run 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() is async Task M().
  • In version 1 of a library, Mi : B does not override M. In version 2, Mi overrides it covariantly with async Task<int> M(), and Roslyn gives Mi.M a [PreserveBaseOverrides] MethodImpl naming B::M.
  • DA : Mi, in another assembly compiled against version 1, overrides M with a return type of Task<int> or a type derived from it. The only M it could see was B.M, so Roslyn gives DA.M a [PreserveBaseOverrides] MethodImpl naming B::M as well.

At run time, with version 2 loaded and Mi mi holding a DA:

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.dll

The 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 from src/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.dll in 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, await through Mi::M on a DA1 object fails with value=10 trace=Mi.M; on both builds with default settings and with each of TieredCompilation=0, TC_QuickJitForLoops=0, JitStress=2, TieredPGO=0, ReadyToRun=0, GCStress=0xC, GCStress=0x3, and GCStress=0xC with ReadyToRun=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 slot i that the type overrides with a [PreserveBaseOverrides] MethodImpl, the fixup rewrites every slot j that still holds the parent's MethodDesc for slot i. For B.M's ordinary slot, the parent's MethodDesc is Mi.M, so Mi.M's own ordinary slot is rewritten. That is why .Result works.
  • For B.M's async variant slot, which returns void, the parent's MethodDesc is Mi.M's return-dropping thunk (methodtablebuilder.cpp, lines 3770-3771). Mi.M's int-returning async variant is a different MethodDesc in a different slot, so the fixup never rewrites it. An await through Mi::M uses that slot.
  • No derived override exists for Mi.M's int-returning async variant either. DB does get an int-returning async variant, but its MethodImpl names B::M, so the variant's declaration lookup finds B's void variant. Because the signatures differ, the code in lines 6389-6407 skips DB's int-returning variant, which then overrides nothing. DA gets only the covariant forwarding thunk. It takes its return type from the MethodImpl's declaration B::M, so it returns void (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

  1. added
    untriaged
    on Nov 5, 2026
  2. dotnet-policy-service commented on Nov 5, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-threading-tasks
    See info in area-owners.md if you want to be subscribed.

  3. pcshrosbree commented on Nov 5, 2026

    @pcshrosbree
    ContributorAuthor

    @VSadov, which direction would you prefer for the DA case 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.M has three slots of its own: the ordinary method, its int async variant and its void return-dropping thunk. For both DA and DB, PropagateCovariantReturnMethodImplSlots rewrites the ordinary slot and the thunk's slot, but not the int variant's slot, so await mi.M() still runs Mi.M. One correction to the issue: the int async variants of Mi.M and DB.M are 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 return void or both return a value. Then ChangesImplementationOfVirtualSlot has to count that inherited slot as changed, so its vtable chunk is not shared with the parent. With a prototype of both changes, DB passes all three call forms.

    The covariant shape (DA) has no int variant to put in that slot. Since #132425, DA.M has one async variant: a covariant forwarding thunk. The thunk takes its return type from the MethodImpl's declaration, B::M (here), so it returns void. I see two ways to give DA.M an int variant:

    • (A) Take the element type from DA.M's own return type, MyTaskInt : Task<int>. The signature of DA.M does not contain Task<int>. Finding it means walking MyTaskInt'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, an int forwarding thunk and a void return-dropping thunk: the same two return shapes DB.M has.
    • (B) Take the element type from the signature of 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 finding Mi.M earlier. A class or struct element type would also need translating from Mi's module into DA's.

    A third option is to land the Task<int> fix first and leave DA open here.

    I'll put up a PR once the direction is clear.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-TypeSystem-coreclrruntime-asyncuntriaged

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions