C# Pose Shim from Task

220 views Asked by At

I've created a shim:

using Pose;

Shim consoleShim = Shim.Replace(() => Debug.WriteLine(Is.A<string>())).With(
    delegate (string s) { Debug.WriteLine($"Hijacked: {s}"); });

The following runs as expected and prints "Hijacked: Test Debug":

PoseContext.Isolate(() =>
{
    Debug.WriteLine("Test Debug");
}, consoleShim);

However, the following fails with a stack overflow error

PoseContext.Isolate(() =>
{
    _ = Task.Run(() =>
    {
        Debug.WriteLine("In different task");
    });
}, consoleShim);
Stack overflow.
   at System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](Int32)
   at System.Collections.Generic.Dictionary`2[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Reflection.Emit.Label, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].TryInsert(Int32, System.Reflection.Emit.Label, System.Collections.Generic.InsertionBehavior)
   at Pose.Extensions.DictionaryExtensions.TryAdd[[System.Int32, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Reflection.Emit.Label, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.Collections.Generic.Dictionary`2<Int32,System.Reflection.Emit.Label>, Int32, System.Reflection.Emit.Label)
   at Pose.IL.MethodRewriter.Rewrite()
   at DynamicClass.stub_System.Collections.Generic.List`1[System.String]_LastIndexOf(System.Collections.Generic.List`1<System.String>, System.String, Int32, Int32, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.Collections.Generic.List`1[System.String]_LastIndexOf(System.Collections.Generic.List`1<System.String>, System.String)
   at DynamicClass.stub_virt_System.Collections.Generic.List`1[System.String]_LastIndexOf(System.Collections.Generic.List`1<System.String>, System.String, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.SR_InternalGetResourceString(System.String)
   at DynamicClass.stub_System.SR_InternalGetResourceString(System.String, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.SR_GetResourceString(System.String)
   at DynamicClass.stub_System.SR_GetResourceString(System.String, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.stub_System.SR_get_Arg_InvalidOperationException(System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.InvalidOperationException_.ctor(System.InvalidOperationException)
   at DynamicClass.stub_ctor_System.InvalidOperationException_.ctor(System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.Runtime.CompilerServices.RuntimeHelpers_IsBitwiseEquatable()
   at DynamicClass.stub_System.Runtime.CompilerServices.RuntimeHelpers_IsBitwiseEquatable(System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.Array_LastIndexOf(System.String[], System.String, Int32, Int32)
   at DynamicClass.stub_System.Array_LastIndexOf(System.String[], System.String, Int32, Int32, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.Collections.Generic.List`1[System.String]_LastIndexOf(System.Collections.Generic.List`1<System.String>, System.String, Int32, Int32)
   at DynamicClass.stub_System.Collections.Generic.List`1[System.String]_LastIndexOf(System.Collections.Generic.List`1<System.String>, System.String, Int32, Int32, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.Collections.Generic.List`1[System.String]_LastIndexOf(System.Collections.Generic.List`1<System.String>, System.String)
   at DynamicClass.stub_virt_System.Collections.Generic.List`1[System.String]_LastIndexOf(System.Collections.Generic.List`1<System.String>, System.String, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.SR_InternalGetResourceString(System.String)
   at DynamicClass.stub_System.SR_InternalGetResourceString(System.String, System.RuntimeMethodHandle, System.RuntimeTypeHandle)
   at DynamicClass.dynamic_System.SR_GetResourceString(System.String)...

Is this a limitation of the Pose library or is there a workaround?

0

There are 0 answers