Repository navigation
Closed
dotnet/coreclr
#24847Description
In some cases abort occurs during CLR shutdown. With some probability, It can be reproduced using following sample:
using System;
using System.Runtime.InteropServices;
using System.Threading;
namespace shutdown_test
{
class Program
{
public delegate void Callback();
static Callback cbStaticCallback = StaticCallback;
[DllImport("libupcalltest.so")]
public static extern void register_callback(Callback cb);
[DllImport("libupcalltest.so")]
public static extern void upcall();
private static void StaticCallback()
{
Thread.Sleep(2);
}
private static void DoWork()
{
upcall();
}
static void Main(string[] args)
{
register_callback(cbStaticCallback);
for (int i = 0; i < 10; i++)
{
var th = new Thread(DoWork);
th.IsBackground = true;
th.Start();
}
Thread.Sleep(100);
}
}
}upcalltest.c:
#include <stdio.h>
#include <string.h>
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h>
#include <sys/syscall.h> /* For SYS_xxx definitions */
typedef void (*callback)();
callback gCB = (callback)0x0;
extern void register_callback(callback cb)
{
printf("register_callback: %ld\n", syscall(SYS_gettid));
gCB = cb;
}
extern void upcall()
{
printf("upcall: %ld\n", syscall(SYS_gettid));
while (1)
{
gCB();
}
}Result:
$ while [ 1 ] ; do ./corerun shutdown_test.dll ; done
...
upcall: 2441
upcall: 2440
upcall: 2435
upcall: 2443
Aborted (core dumped)
$ gdb ./corerun -c core
...
Core was generated by `./corerun /home/kbaladurin/Desktop/dotnet/coredumps/lldb/dotnet/shutdown_test/b'.
Program terminated with signal SIGABRT, Aborted.
#0 0xf7764be9 in __kernel_vsyscall ()
[Current thread is 1 (Thread 0xed8fcb40 (LWP 3422))]
(gdb) bt
#0 0xf7764be9 in __kernel_vsyscall ()
dotnet/coreclr#1 0xf739cea9 in raise () from /lib/i386-linux-gnu/libc.so.6
dotnet/coreclr#2 0xf739e407 in abort () from /lib/i386-linux-gnu/libc.so.6
dotnet/coreclr#3 0xf6eaf37b in PROCAbort () at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/thread/process.cpp:3068
dotnet/coreclr#4 0xf6ea9991 in PROCEndProcess (hProcess=0xffffff01, uExitCode=2148734214, bTerminateUnconditionally=1) at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/thread/process.cpp:1394
dotnet/coreclr#5 0xf6ea9b01 in TerminateProcess (hProcess=0xffffff01, uExitCode=2148734214) at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/thread/process.cpp:1310
dotnet/coreclr#6 0xf65cb0a7 in SafeExitProcess (exitCode=2148734214, fAbort=1, sca=SCA_ExitProcessWhenShutdownComplete) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/eepolicy.cpp:519
dotnet/coreclr#7 0xf65cd255 in EEPolicy::HandleFatalError (exitCode=2148734214, address=4135478230, pszMessage=0x0, pExceptionInfo=0xed8fabe0, errorSource=0x0, argExceptionString=0x0)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/eepolicy.cpp:1545
dotnet/coreclr#8 0xf67e6416 in UnhandledExceptionHandlerUnix (version=1, action=_UA_SEARCH_PHASE, exceptionClass=5138137972254386944, exception=0xf0509908, context=0xed8fb040)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/exceptionhandling.cpp:5863
dotnet/coreclr#9 0xf753d675 in _Unwind_RaiseException () from /lib/i386-linux-gnu/libgcc_s.so.1
dotnet/coreclr#10 0xf760ab63 in __cxa_throw () from /usr/lib/i386-linux-gnu/libstdc++.so.6
dotnet/coreclr#11 0xf6db392f in RtlpRaiseException (ExceptionRecord=0xf0509874, ContextRecord=0xf05095a8) at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/exception/seh-unwind.cpp:452
dotnet/coreclr#12 0xf6db38a8 in RaiseException (dwExceptionCode=3221356545, dwExceptionFlags=1, nNumberOfArguments=1, lpArguments=0xed8fb308)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/exception/seh-unwind.cpp:519
dotnet/coreclr#13 0xf637def5 in COMPlusThrowBoot (hr=-2147023829) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/excep.cpp:198
dotnet/coreclr#14 0xf65b7bd7 in UMThunkStubRareDisableWorker (pThread=0x9339af0, pUMEntryThunk=0xf5a5d5e0) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/dllimportcallback.cpp:1037
dotnet/coreclr#15 0xf67f854c in UMThunkStub () at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/i386/umthunkstub.S:143
dotnet/coreclr#16 0xf500c625 in upcall () at upcall.c:23
dotnet/coreclr#17 0xf4696b5b in ?? ()
dotnet/coreclr#18 0xf4696b02 in ?? ()
dotnet/coreclr#19 0xf4696ad3 in ?? ()
dotnet/coreclr#20 0xf4696a09 in ?? ()
dotnet/coreclr#21 0xf469680f in ?? ()
dotnet/coreclr#22 0xf46965eb in ?? ()
dotnet/coreclr#23 0xf67f7211 in CallDescrWorkerInternal () at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/i386/asmhelpers.S:304
dotnet/coreclr#24 0xf657271d in CallDescrWorker (pCallDescrData=0xed8fbb38) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/callhelpers.cpp:135
dotnet/runtime#3858 0xf65724d0 in CallDescrWorkerWithHandler (pCallDescrData=0xed8fbb38, fCriticalCall=0) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/callhelpers.cpp:78
dotnet/runtime#3859 0xf6574103 in MethodDescCallSite::CallTargetWorker (this=0xed8fbcc0, pArguments=0xed8fbd30, pReturnValue=0x0, cbReturnValue=0)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/callhelpers.cpp:620
dotnet/coreclr#27 0xf6354349 in MethodDescCallSite::Call (this=0xed8fbcc0, pArguments=0xed8fbd30) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/callhelpers.h:433
dotnet/runtime#3860 0xf658e14d in ThreadNative::KickOffThread_Worker (ptr=0xed8fc200) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/comsynchronizable.cpp:258
dotnet/runtime#3861 0xf64f5d6c in ManagedThreadBase_DispatchInner (pCallState=0xed8fc058) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:8850
dotnet/runtime#3862 0xf64fc60d in ManagedThreadBase_DispatchMiddle (pCallState=0xed8fc058) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:8901
dotnet/runtime#3863 0xf64fc38f in ManagedThreadBase_DispatchOuter(ManagedThreadCallState*)::$_8::operator()(ManagedThreadBase_DispatchOuter(ManagedThreadCallState*)::TryArgs*) const::{lambda(Param*)#1}::operator()(Param*) const (this=0xed8fbf70, pParam=0xed8fbff8) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:9139
dotnet/coreclr#32 0xf64fc12e in ManagedThreadBase_DispatchOuter(ManagedThreadCallState*)::$_8::operator()(ManagedThreadBase_DispatchOuter(ManagedThreadCallState*)::TryArgs*) const (this=0xed8fbfe0, pArgs=0xed8fbfe8)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:9141
dotnet/runtime#3864 0xf64f58db in ManagedThreadBase_DispatchOuter (pCallState=0xed8fc058) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:9178
dotnet/runtime#3865 0xf64f5abb in ManagedThreadBase_FullTransitionWithAD (pAppDomain=..., pTarget=0xf658db80 <ThreadNative::KickOffThread_Worker(void*)>, args=0xed8fc200, filterType=ManagedThread)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:9199
dotnet/coreclr#35 0xf64f5a17 in ManagedThreadBase::KickOff (pAppDomain=..., pTarget=0xf658db80 <ThreadNative::KickOffThread_Worker(void*)>, args=0xed8fc200)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:9234
dotnet/coreclr#36 0xf658ee5e in ThreadNative::KickOffThread (pass=0x92fa228) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/comsynchronizable.cpp:377
dotnet/runtime#3866 0xf64e33ad in Thread::intermediateThreadProc (arg=0x93279b8) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:2254
dotnet/coreclr#38 0xf6eb7f2b in CorUnix::CPalThread::ThreadEntry (pvParam=0x933a9a0) at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/thread/thread.cpp:1682
dotnet/runtime#3867 0xf7717295 in start_thread () from /lib/i386-linux-gnu/libpthread.so.0
dotnet/runtime#3868 0xf74580ae in clone () from /lib/i386-linux-gnu/libc.so.6
(gdb) thread 5
[Switching to thread 5 (Thread 0xf7370940 (LWP 3405))]
#0 0xf7764be9 in __kernel_vsyscall ()
(gdb) bt
#0 0xf7764be9 in __kernel_vsyscall ()
dotnet/coreclr#1 0xf771ca6c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib/i386-linux-gnu/libpthread.so.0
dotnet/coreclr#2 0xf6e8689e in CorUnix::CPalSynchronizationManager::ThreadNativeWait (ptnwdNativeWaitData=0x9296568, dwTimeout=4294967295, ptwrWakeupReason=0xffb8df64, pdwSignaledObject=0xffb8df60)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/synchmgr/synchmanager.cpp:479
dotnet/coreclr#3 0xf6e85680 in CorUnix::CPalSynchronizationManager::BlockThread (this=0x9296c30, pthrCurrent=0x92963d8, dwTimeout=4294967295, fAlertable=true, fIsSleep=false, ptwrWakeupReason=0xffb8e2a8,
pdwSignaledObject=0xffb8e2d4) at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/synchmgr/synchmanager.cpp:302
dotnet/coreclr#4 0xf6e9b8c5 in CorUnix::InternalWaitForMultipleObjectsEx (pThread=0x92963d8, nCount=1, lpHandles=0x92f0148, bWaitAll=0, dwMilliseconds=4294967295, bAlertable=1, bPrioritize=0)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/synchmgr/wait.cpp:636
dotnet/coreclr#5 0xf6e9cc6b in WaitForMultipleObjectsEx (nCount=1, lpHandles=0x92f0148, bWaitAll=0, dwMilliseconds=4294967295, bAlertable=1)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/pal/src/synchmgr/wait.cpp:202
dotnet/coreclr#6 0xf64e5e5d in WaitForMultipleObjectsEx_SO_TOLERANT (nCount=1, lpHandles=0x92f0148, bWaitAll=0, dwMilliseconds=4294967295, bAlertable=1)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:3462
dotnet/coreclr#7 0xf64e5f29 in Thread::DoAppropriateAptStateWait (this=0x92deee8, numWaiters=1, pHandles=0x92f0148, bWaitAll=0, timeout=4294967295, mode=WaitMode_Alertable)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:3496
dotnet/coreclr#8 0xf64e681a in Thread::DoAppropriateWaitWorker (this=0x92deee8, countHandles=1, handles=0x92f0148, waitAll=0, millis=4294967295, mode=WaitMode_Alertable)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:3633
dotnet/coreclr#9 0xf64e582f in Thread::DoAppropriateWait(int, void**, int, unsigned int, WaitMode, PendingSync*)::$_0::operator()(Thread::DoAppropriateWait(int, void**, int, unsigned int, WaitMode, PendingSync*)::__EEParam*) const (this=0xffb8e6f8, __pEEParam=0xffb8e700) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:3307
dotnet/coreclr#10 0xf64d9a0e in Thread::DoAppropriateWait (this=0x92deee8, countHandles=1, handles=0x92f0148, waitAll=0, millis=4294967295, mode=WaitMode_Alertable, syncState=0x0)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/threads.cpp:3322
dotnet/coreclr#11 0xf6762e45 in CLREventBase::WaitEx (this=0x92f0148, dwMilliseconds=4294967295, mode=WaitMode_Alertable, syncState=0x0) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/synch.cpp:475
dotnet/coreclr#12 0xf6762c73 in CLREventBase::Wait (this=0x92f0148, dwMilliseconds=4294967295, alertable=1, syncState=0x0) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/synch.cpp:426
dotnet/coreclr#13 0xf660abd4 in FinalizerThread::FinalizerThreadWatchDogHelper()::$_5::operator()(FinalizerThread::FinalizerThreadWatchDogHelper()::Param*) const (this=0xffb8e908, pParam=0xffb8e910)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/finalizerthread.cpp:1297
dotnet/coreclr#14 0xf660a631 in FinalizerThread::FinalizerThreadWatchDogHelper () at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/finalizerthread.cpp:1299
dotnet/coreclr#15 0xf660a026 in FinalizerThread::FinalizerThreadWatchDog () at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/finalizerthread.cpp:1156
dotnet/coreclr#16 0xf657956e in EEShutDownHelper (fIsDllUnloading=0) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/ceemain.cpp:1677
dotnet/coreclr#17 0xf657a758 in EEShutDown (fIsDllUnloading=0) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/ceemain.cpp:2207
dotnet/coreclr#18 0xf634e5fe in CorHost2::UnloadAppDomain2 (this=0x92a08c0, dwDomainId=1, fWaitUntilDone=1, pLatchedExitCode=0xffb8f5e4) at /media/kbaladurin/data/dotnet/forked/coreclr/src/vm/corhost.cpp:1155
dotnet/coreclr#19 0xf62cec68 in coreclr_shutdown_2 (hostHandle=0x92a08c0, domainId=1, latchedExitCode=0xffb8f5e4) at /media/kbaladurin/data/dotnet/forked/coreclr/src/dlls/mscoree/unixinterface.cpp:320
dotnet/coreclr#20 0x0804dd48 in ExecuteManagedAssembly (currentExeAbsolutePath=0x928aa10 "/media/kbaladurin/data/dotnet/clr_overlay_x86/corerun",
clrFilesAbsolutePath=0x928aa90 "/media/kbaladurin/data/dotnet/clr_overlay_x86",
managedAssemblyAbsolutePath=0x928aac8 "/home/kbaladurin/Desktop/dotnet/coredumps/lldb/dotnet/shutdown_test/bin/Debug/netcoreapp2.0/shutdown_test.dll", managedAssemblyArgc=0, managedAssemblyArgv=0x0)
at /media/kbaladurin/data/dotnet/forked/coreclr/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp:444
dotnet/coreclr#21 0x0804c894 in corerun (argc=2, argv=0xffb8f884) at /media/kbaladurin/data/dotnet/forked/coreclr/src/coreclr/hosts/unixcorerun/corerun.cpp:149
dotnet/coreclr#22 0x0804c9ba in main (argc=2, argv=0xffb8f884) at /media/kbaladurin/data/dotnet/forked/coreclr/src/coreclr/hosts/unixcorerun/corerun.cpp:161
Abort is called due to unhandled exception that is thrown here (vm/dllimportcallback.cpp:1027):
// Check for ShutDown scenario. This happens only when we have initiated shutdown
// and someone is trying to call in after the CLR is suspended. In that case, we
// must either raise an unmanaged exception or return an HRESULT, depending on the
// expectations of our caller.
if (!CanRunManagedCode())
{
// DO NOT IMPROVE THIS EXCEPTION! It cannot be a managed exception. It
// cannot be a real exception object because we cannot execute any managed
// code here.
pThread->m_fPreemptiveGCDisabled = 0;
COMPlusThrowBoot(E_PROCESS_SHUTDOWN_REENTRY);
}Is It valid situation? On arm and x86_64 this problem isn't reproduced (or reproduced much more rarely) with this sample.
Thank you!
Activity
kbaladurin commentedon Apr 3, 2018
@janvorli @jkotas PTAL
RussKeldorph commentedon Apr 3, 2018
I assume we wouldn't spend time on this in 2.1 unless we have a repro on something other than x86/Linux
janvorli commentedon Apr 4, 2018
@RussKeldorph I agree.
janvorli commentedon Apr 4, 2018
Actually, looking at the test source above, it seems the test itself is flawed. It starts 10 background threads and each thread calls the upcall native function that in turn calls managed callback StaticCallback. The main thread sleeps for 100 msec and then exits.
So in case one of the threads doesn't make it to the managed callback before the 100 msec ellapse, the window for allowed managed callback closes, since the runtime is in a process of shutdown.
While 100 msecs look like a long time, it is not necessarily the case, depending e.g. on the number of processors of the target machine.
kbaladurin commentedon Apr 4, 2018
@janvorli does it mean that application should stop all its background threads before shutdown to avoid situation when managed callback is called after window for allowed managed callback closes?
jkotas commentedon Apr 4, 2018
It used to be the case with shutdown finalization, and it was really hard for any even slightly more complex application to get right. The more complex applications dealt with this by exiting the process themselves, not giving the runtime a chance to shutdown gracefully.
I think we should look into whether the "runtime stopped" check is really needed for anything given that we have disabled shutdown finalization in .NET Core (https://github.com/dotnet/corefx/issues/5205 has full context).
janvorli commentedon Apr 4, 2018
@jkotas I can see that the CanRunManagedCode can return FALSE for multiple different reasons, the shutdown finalization being only one of them. E.g. when the g_fForbidEnterEE is set. Do you think we can ignore that one?
jkotas commentedon Apr 4, 2018
I think the plan we should be is that you can always run managed code once the runtime is initialized, do not block it during shutdown.
CanRunManagedCodehas a lot of checks in it. It is not clear why some of them are there, but none of them looks critical to me.I think we may want to change
CanRunManagedCodeto return true and see what happens.