在基于 Linux 的 docker 映像中运行以下代码会导致 CLR 崩溃。
static void Main(string[] args)
{
System.Console.WriteLine("Starting program");
using (NLua.Lua luaState = new NLua.Lua())
{
System.Action invoke = () => throw new System.Exception("message");
luaState["invoke"] = invoke;
try
{
System.Console.WriteLine("Invoking delegate");
luaState.DoString(@"invoke()");
}
catch (System.Exception ex)
{
// We never get here
System.Console.WriteLine("Exception caught");
throw;
}
}
}
难道我做错了什么?
Dockerfile 是 Visual Studio 的默认建议,进程是从 Visual Studio 启动的。
在 Docker 之外运行程序,即。在 Windows 主机上,按预期工作。(命中 catch 块并且不会导致 CLR 上的致命情况)
将调用包装在 apcall
中并不能解决问题。
可以通过注册函数而不是发送委托来避免这个问题。如果委托没有抛出异常,它将按预期调用。
图片中的控制台日志;
Starting program
Invoking delegate
Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.Exception: message
at LuaTest.Program.<>c.<Main>b__0_0() in C:\Source\Tests\LuaTest\Program.cs:line 10
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at NLua.MetaFunctions.CallDelegateInternal(Lua luaState)
at NLua.MetaFunctions.CallDelegate(IntPtr state)
at KeraLua.NativeMethods.lua_pcallk(IntPtr luaState, Int32 nargs, Int32 nresults, Int32 errorfunc, IntPtr ctx, IntPtr k)
at KeraLua.Lua.PCall(Int32 arguments, Int32 results, Int32 errorFunctionIndex)
at NLua.Lua.DoString(String chunk, String chunkName)
at LuaTest.Program.Main(String[] args) in C:\Source\Tests\LuaTest\Program.cs:line 15
Fatal error. Internal CLR error. (0x80131506)
at KeraLua.NativeMethods.lua_pcallk(IntPtr, Int32, Int32, Int32, IntPtr, IntPtr)
at KeraLua.NativeMethods.lua_pcallk(IntPtr, Int32, Int32, Int32, IntPtr, IntPtr)
at KeraLua.Lua.PCall(Int32, Int32, Int32)
at NLua.Lua.DoString(System.String, System.String)
at LuaTest.Program.Main(System.String[])