sdk icon indicating copy to clipboard operation
sdk copied to clipboard

.NET Core apps don't break on unhandled exceptions

Open schellap opened this issue 10 years ago • 5 comments

From @danroth27

Repro steps

  • I tried the latest ASP.NET Core dogfood tooling build with the .NET Core tools.
  • I create a .NET Core Console App
  • Add “throw new ArgumentException();” in the Main method
  • Hit F5

Expected:

  • Break on unhandled exception

Actual:

  • App just dies. I see the following in the output window:

Exception thrown: 'System.ArgumentException' in ConsoleApp1.dll The program '[8412] dotnet.exe: Program Trace' has exited with code 0 (0x0). The program '[8412] dotnet.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

Product Information:

Version: 1.0.0-rc2-002426 Commit Sha: 08ee6dedd9

Runtime Environment:

OS Name: Windows OS Version: 10.0.10586 OS Platform: Windows RID: win10-x64

schellap avatar Apr 27 '16 08:04 schellap

From @BillHiebert: The only difference between the Console app case (which works) and the aspnet case, is that VS does an attach in the 2nd case (the process is launched suspended and the debugger attaches to it before it is resumed).

schellap avatar Apr 27 '16 08:04 schellap

Based on what Bill said, I reproed this with the below program. This reproes for any program that initializes the locale (_lock_locale call) in std.

See Converter.cpp as what the offending code is. If you removed that “thread_local” things are fine. If you put it there, crash.

The sad part is we don’t need this UTF8 converter in the host because the OS API supports it cleanly with MultiByteToWideChar. Nevertheless, it doesn’t have to be a global or even thread_local, I will just use the OS API instead (this has been bothering me regardless of this issue.)

// createprocess.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>

int main()
{
    STARTUPINFO si = { 0 };
    si.cb = sizeof(si);

    PROCESS_INFORMATION pi = { 0 };

    LPWSTR pszCommandLine = _wcsdup(L"h:\\createprocess\\Converter\\x64\\Debug\\Converter.exe");
    BOOL success = ::CreateProcessW(NULL,
        pszCommandLine,
        NULL,
        NULL,
        FALSE,
        NORMAL_PRIORITY_CLASS | CREATE_SUSPENDED,
        NULL,
        NULL,
        &si,
        &pi);

    // Attach debugger to dotnet.exe or Converter.exe here.
    ::Sleep(5000);

    if (success != FALSE)
    {
        success = ::ResumeThread(pi.hThread);
        if (success != FALSE)
        {
            ::WaitForSingleObject(pi.hThread, INFINITE);
        }
    }
}
// Converter.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <locale>
#include <codecvt>

static thread_local std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> t_converter;

int main()
{
    return 0;
}

Windbg Logs

First chance exceptions are reported before any exception handling.
This exception may be expected and handled.
ntdll!RtlpIncrementCriticalSectionContentionCount+0xc [inlined in ntdll!RtlpWaitOnCriticalSection+0xbc]:
00007ff8`4868fe34 ff4024          inc     dword ptr [rax+24h] ds:00000000`00000024=????????
{

    if (CriticalSection->DebugInfo != CS_INVALID_DEBUG_INFO) {
        CriticalSection->DebugInfo->ContentionCount += 1;
    }

    return;
}

.  0  Id: 156c.6b6c Suspend: 1 Teb: 000000f9`043a1000 Unfrozen
# Child-SP          RetAddr           Call Site
00 (Inline Function) --------`-------- ntdll!RtlpIncrementCriticalSectionContentionCount+0xc [d:\th\minkernel\ntdll\resource.c @ 888]
01 000000f9`0457f160 00007ff8`4868ea0b ntdll!RtlpWaitOnCriticalSection+0xbc [d:\th\minkernel\ntdll\resource.c @ 1351]
02 000000f9`0457f230 00007ff6`36f14017 ntdll!RtlpEnterCriticalSectionContended+0xcb [d:\th\minkernel\ntdll\resource.c @ 2057]
03 000000f9`0457f260 00007ff6`36f140fe dotnet!__acrt_lock+0x27 [d:\th\minkernel\crts\ucrt\src\appcrt\internal\locks.cpp @ 55]
04 000000f9`0457f290 00007ff6`36e53d07 dotnet!_lock_locales+0xe [d:\th\minkernel\crts\ucrt\src\appcrt\internal\locks.cpp @ 65]
05 000000f9`0457f2c0 00007ff6`36e564b6 dotnet!std::_Lockit::_Lockit+0x27 [f:\dd\vctools\crt\crtw32\stdcpp\xlock.cpp @ 67]
06 000000f9`0457f2f0 00007ff6`36e25bfe dotnet!std::locale::_Init+0x26 [f:\dd\vctools\crt\crtw32\stdcpp\locale0.cpp @ 165]
07 000000f9`0457f360 00007ff6`36e4e197 dotnet!std::locale::locale+0x2e [c:\program files (x86)\microsoft visual studio 14.0\vc\include\xlocale @ 330]
08 000000f9`0457f3a0 00007ff6`36e1df37 dotnet!std::wstring_convert<std::codecvt_utf8<wchar_t,1114111,0>,wchar_t,std::allocator<wchar_t>,std::allocator<char> >::wstring_convert<std::codecvt_utf8<wchar_t,1114111,0>,wchar_t,std::allocator<wchar_t>,std::allocator<char> >+0x47 [c:\program files (x86)\microsoft visual studio 14.0\vc\include\xlocbuf @ 333]
09 000000f9`0457f3f0 00007ff6`36eaafef dotnet!`dynamic initializer for 't_converter''+0x37 [e:\clr\git\cli\src\corehost\common\pal.windows.cpp @ 13]
0a 000000f9`0457f420 00007ff8`486752c8 dotnet!__dyn_tls_init+0x6f [f:\dd\vctools\crt\vcstartup\src\tls\tlsdyn.cpp @ 96]
0b 000000f9`0457f470 00007ff8`48671577 ntdll!LdrpCallInitRoutine+0x4c [d:\th\minkernel\ntdll\ldr.c @ 211]
0c 000000f9`0457f4d0 00007ff8`48672f44 ntdll!LdrpCallTlsInitializers+0x93 [d:\th\minkernel\ntdll\ldrtls.c @ 1068]
0d 000000f9`0457f550 00007ff8`486d6e3b ntdll!LdrpInitializeThread+0x1e4 [d:\th\minkernel\ntdll\ldrinit.c @ 6127]
0e 000000f9`0457f640 00007ff8`486d6d5e ntdll!_LdrpInitialize+0x87 [d:\th\minkernel\ntdll\ldrinit.c @ 1610]
0f 000000f9`0457f6c0 00000000`00000000 ntdll!LdrInitializeThunk+0xe [d:\th\minkernel\ntdll\ldrstart.c @ 91]

schellap avatar Apr 27 '16 08:04 schellap

Fixed by https://github.com/dotnet/cli/pull/2705

@piotrpMSFT marking for RC2.

schellap avatar Apr 27 '16 08:04 schellap

@schellap did you send approval mail?

TheRealPiotrP avatar Apr 27 '16 23:04 TheRealPiotrP

Yes, closing...

schellap avatar Apr 28 '16 00:04 schellap