question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run mode does not behave to spec with code throwing an exception in using

See original GitHub issue

This caught me off-guard, assuming that Dispose() would not get called if an exception occurred, yet all the SO questions about exceptions inside usings claimed the opposite.

Reproduction code:

using System;

class P
{
    static void Main()
    {
        try
        {
            Use();
        }
        catch (Exception)
        {
            Console.WriteLine("Caught");
        }
    }
    
    static void Use()
    {
        Console.WriteLine("Constructing");
        using (new D())
        {
            Console.WriteLine("Throwing");
            throw new Exception();
        }
    }
    
    class D : IDisposable
    {
        public void Dispose() => Console.WriteLine("Disposed");
    }
}

Expected output (also seen in VS2017):

Constructing
Throwing
Disposed
Caught

Actual output:

Constructing
Throwing
Caught

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

0reactions
ashmindcommented, Apr 22, 2018

OK so just for curiosity sake (and recording for myself as well) the issue is caused by Dispose() hitting the stack limit of Unbreakable, and therefore never executing. The reason why it hits the stack limit is because being in finally after a throw seems to add tons of things to the stack:

0867a99c 1849a9fe (MethodDesc 185b31b4 +0x26 Unbreakable.Runtime.Internal.RuntimeGuard.EnsureStack()), calling clr!JIT_GetSharedNonGCThreadStaticBase
0867a9c4 1849a95e (MethodDesc 185b3158 +0x16 Unbreakable.Runtime.Internal.RuntimeGuard.GuardEnter()), calling (MethodDesc 185b31b4 +0 Unbreakable.Runtime.Internal.RuntimeGuard.EnsureStack())
0867a9d0 186e26e7 (MethodDesc 185b4170 +0x37 Program+D.Dispose()), calling (MethodDesc 185b3158 +0 Unbreakable.Runtime.Internal.RuntimeGuard.GuardEnter())
0867a9f0 5797078e clr!PreStubWorker+0x135, calling clr!_EH_epilog3
0867a9f4 5794ef99 clr!ThePreStub+0x11, calling clr!PreStubWorker
0867aa18 1849a83d (MethodDesc 185b2f44 +0x10d Program.Main()), calling 18476032
0867aa1c 5794e9bb clr!CallJitEHFinallyHelper+0x28
0867aa30 57a5e45f clr!CallJitEHFinally+0xa7, calling clr!CallJitEHFinallyHelper
0867aa64 57a5e379 clr!EEToProfilerExceptionInterfaceWrapper::ExceptionUnwindFinallyEnter+0x85, calling clr!_EH_epilog3
0867aa88 57a5d2cf clr!COMPlusUnwindCallback+0x564, calling clr!CallJitEHFinally
0867aaec 1849a7fa (MethodDesc 185b2f44 +0xca Program.Main()), calling clr!IL_Throw
0867ab08 5795ee23 clr!StackFrameIterator::Init+0xdd, calling clr!StackFrameIterator::Filter
0867ab28 5795e96b clr!Thread::StackWalkFramesEx+0x92
0867abe0 77d6d544 ntdll!LdrpResSearchResourceMappedFile+0x387, calling ntdll!_SEH_epilog4_GS
0867abe4 77d6ec0d ntdll!LdrResSearchResource+0x1fd, calling ntdll!LdrpResSearchResourceMappedFile
0867ac38 77da91fa ntdll!_SEH_epilog4_GS+0xa, calling ntdll!__security_check_cookie
0867ac88 77da23f2 ntdll!LdrpDecrementNodeLoadCountLockHeld+0x9f, calling ntdll!_SEH_epilog4
0867acbc 77d6c2b7 ntdll!LdrpDecrementModuleLoadCountEx+0x44, calling ntdll!RtlReleaseSRWLockExclusive
0867ad6c 1849a7fa (MethodDesc 185b2f44 +0xca Program.Main()), calling clr!IL_Throw
0867adbc 1849a7fa (MethodDesc 185b2f44 +0xca Program.Main()), calling clr!IL_Throw
0867adf4 5795ea71 clr!Thread::StackWalkFrames+0x9d, calling clr!Thread::StackWalkFramesEx
0867ae44 1849a7fa (MethodDesc 185b2f44 +0xca Program.Main()), calling clr!IL_Throw
0867b11c 61bd0d09 MSVCR120_CLR0400!_woutput_s_l+0xaa9, calling MSVCR120_CLR0400!_woutput_s_l+0xc24
0867b128 57a5dd2a clr!UnwindFrames+0x52, calling clr!Thread::StackWalkFrames
0867b150 57ad94e0 clr!CPFH_UnwindFrames1+0x84, calling clr!UnwindFrames
0867b1ac 57ad95e1 clr!CPFH_UnwindHandler+0x156, calling clr!CPFH_UnwindFrames1
0867b2b0 57ad8c58 clr!COMPlusFrameHandler+0xfb, calling clr!CPFH_UnwindHandler
0867b2dc 77da9df2 ntdll!ExecuteHandler2+0x26
0867b300 77da9dc4 ntdll!ExecuteHandler+0x24, calling ntdll!ExecuteHandler2
0867b324 77d815b2 ntdll!RtlpUnwindWithContext+0x117, calling ntdll!RtlpExecuteHandlerForUnwind
0867b364 77a2fc73 KERNELBASE!GetFileVersionInfoSizeExW+0xb3, calling ntdll!RtlSetLastWin32Error
0867b368 77a2fc85 KERNELBASE!GetFileVersionInfoSizeExW+0xc5, calling KERNELBASE!_SEH_epilog4
0867b384 57956c0c clr!ClrFlsSetValue+0x17
0867b394 57a0e9d3 clr!IncCantAllocCount+0x82, calling clr!ClrFlsSetValue
0867b3a8 77a2fc85 KERNELBASE!GetFileVersionInfoSizeExW+0xc5, calling KERNELBASE!_SEH_epilog4
0867b3ac 77a4ff12 KERNELBASE!GetFileVersionInfoSizeW+0x12, calling KERNELBASE!GetFileVersionInfoSizeExW
0867b3d4 61bb548c MSVCR120_CLR0400!_vswprintf_helper+0x114, calling MSVCR120_CLR0400!__security_check_cookie
0867b400 61bb52b5 MSVCR120_CLR0400!_vsnwprintf_s_l+0xa5, calling MSVCR120_CLR0400!_vswprintf_helper
0867b410 77d81107 ntdll!RtlUnwind+0x47, calling ntdll!RtlpUnwindWithContext
0867b430 61bb361a MSVCR120_CLR0400!_snwprintf_s+0x1a, calling MSVCR120_CLR0400!_vsnwprintf_s_l
0867b440 5796b760 clr!SString::ConvertToUnicode+0x47, calling clr!SString::IsRepresentation
0867b44c 5796b786 clr!SString::ConvertToUnicode+0xce, calling clr!__security_check_cookie
0867b450 57add300 clr!BaseBucketParamsManager::GetAppVersion+0x99, calling MSVCR120_CLR0400!_snwprintf_s
0867b474 57add30e clr!BaseBucketParamsManager::GetAppVersion+0xe3, calling clr!__security_check_cookie
0867b480 579588e9 clr!REGUTIL::EnvGetString+0x5a, calling clr!HashiStringKnownLower80
0867b48c 57958917 clr!REGUTIL::EnvGetString+0x103, calling clr!__security_check_cookie
0867b4e0 61bde28e MSVCR120_CLR0400!_UnwindNestedFrames+0x2a, calling ntdll!RtlUnwind
0867b518 5795894f clr!REGUTIL::RegCacheValueNameSeenPerhaps+0x2c, calling clr!HashiStringKnownLower80
0867b520 57958ad0 clr!REGUTIL::GetConfigInteger+0xa1, calling clr!REGUTIL::RegCacheValueNameSeenPerhaps
0867b55c 57958b09 clr!REGUTIL::GetConfigDWORD_DontUse_+0x18, calling clr!REGUTIL::GetConfigInteger
0867b584 579829d7 clr!CLRConfig::GetConfigValue+0x188, calling clr!REGUTIL::GetConfigDWORD_DontUse_
0867b59c 57958a7f clr!CLRConfig::GetConfigValue+0x20e, calling clr!__security_check_cookie
0867b5c4 61bb5419 MSVCR120_CLR0400!_vswprintf_helper+0xa1
0867b5d8 57a7f75f clr!DebuggerJitInfo::LazyInitBounds+0xb1, calling clr!_EH_epilog3
0867b5dc 57a7f77e clr!DebuggerJitInfo::MapNativeOffsetToIL+0x1e, calling clr!DebuggerJitInfo::LazyInitBounds
0867b5f4 57a7fa38 clr!Debugger::GetILOffsetFromNative+0x9e, calling clr!DebuggerJitInfo::MapNativeOffsetToIL
0867b630 61bb548c MSVCR120_CLR0400!_vswprintf_helper+0x114, calling MSVCR120_CLR0400!__security_check_cookie
0867b6c0 77d5fa0d ntdll!RtlFreeHeap+0x7cd, calling ntdll!RtlpFreeHeap
0867b6d8 57ad8919 clr!GetManagedBucketParametersForIp+0xda, calling clr!_EH_epilog3
0867b6fc 61bde28e MSVCR120_CLR0400!_UnwindNestedFrames+0x2a, calling ntdll!RtlUnwind
0867b704 61bde28e MSVCR120_CLR0400!_UnwindNestedFrames+0x2a, calling ntdll!RtlUnwind
0867b720 61bde28e MSVCR120_CLR0400!_UnwindNestedFrames+0x2a, calling ntdll!RtlUnwind
0867b728 61bdeb2e MSVCR120_CLR0400!__DestructExceptionObject+0x2ce, calling MSVCR120_CLR0400!_UnwindNestedFrames
0867b73c 61bdf051 MSVCR120_CLR0400!__DestructExceptionObject+0x7f1, calling MSVCR120_CLR0400!__DestructExceptionObject+0x29b
0867b784 61bdeeeb MSVCR120_CLR0400!__DestructExceptionObject+0x68b, calling MSVCR120_CLR0400!__DestructExceptionObject+0x6f3
0867b7d0 57c16d3f clr!ReflectionInvocationExceptionFilter+0x9e, calling clr!_EH_epilog3
0867b7dc 77d69197 ntdll!RtlpxLookupFunctionTable+0x47, calling ntdll!RtlAcquireSRWLockShared
0867b7e0 77d79d79 ntdll!RtlSetLastWin32Error+0x39, calling ntdll!__security_check_cookie
0867b7f8 61bdfa8b MSVCR120_CLR0400!__InternalCxxFrameHandler+0xf6, calling MSVCR120_CLR0400!__DestructExceptionObject+0x363
0867b830 61bde396 MSVCR120_CLR0400!__CxxFrameHandler+0x26, calling MSVCR120_CLR0400!__InternalCxxFrameHandler
0867b86c 77da9df2 ntdll!ExecuteHandler2+0x26
0867b890 77da9dc4 ntdll!ExecuteHandler+0x24, calling ntdll!ExecuteHandler2
0867b8b4 77d812d3 ntdll!RtlDispatchException+0x1ba, calling ntdll!RtlpExecuteHandlerForException
0867b8f8 57a84069 clr!EEDbgInterfaceImpl::GetMethodRegionInfo+0x69, calling clr!__security_check_cookie
0867b904 57956b08 clr!ClrFlsIncrementValue+0x14
0867b90c 5794f47e clr!CrstBase::Enter+0x11b, calling ntdll!RtlTryEnterCriticalSection
0867b918 57956b08 clr!ClrFlsIncrementValue+0x14
0867b924 57956b48 clr!CrstBase::Leave+0x64, calling clr!ClrFlsIncrementValue
0867b930 57e14b5f clr!Debugger::MapAndBindFunctionPatches+0x1f7, calling clr!_EH_epilog3
0867b958 77d90cb6 ntdll!KiUserExceptionDispatcher+0x26, calling ntdll!RtlDispatchException
00000000 00004015 00004015 ====> Exception cxr@0867b970
0867b978 77a43ef2 KERNELBASE!RaiseException+0x62, calling ntdll!RtlRaiseException
0867b9b0 77d79d79 ntdll!RtlSetLastWin32Error+0x39, calling ntdll!__security_check_cookie
0867ba74 77a43ef2 KERNELBASE!RaiseException+0x62, calling ntdll!RtlRaiseException
0867bbc0 57a1470b clr!MethodTable::FindDispatchSlot+0x16, calling clr!MethodTable::FindDispatchImpl
0867bbf8 77a43ef2 KERNELBASE!RaiseException+0x62, calling ntdll!RtlRaiseException
0867bc0c 57a148f7 clr!VirtualCallStubManager::Resolver+0x121, calling clr!__security_check_cookie
0867bc30 57a8428b clr!EEJitManager::JitTokenToMethodRegionInfo+0x5b
0867bc3c 57c27bb2 clr!ETW::MethodLog::SendMethodEvent+0xbff, calling clr!CoTemplate_xxxqqqzzzh
0867bc74 57956b08 clr!ClrFlsIncrementValue+0x14
0867bc7c 57956b08 clr!ClrFlsIncrementValue+0x14
0867bc84 5794f47e clr!CrstBase::Enter+0x11b, calling ntdll!RtlTryEnterCriticalSection
0867bca8 57971fef clr!CExecutionEngine::ReleaseLock+0x5c, calling clr!_EH_epilog3
0867bd00 579722f2 clr!Holder<SimpleRWLock *,&SimpleRWLock::AcquireReadLock,&SimpleRWLock::ReleaseReadLock,0,&CompareDefault<SimpleRWLock *>,2,1>::~Holder<SimpleRWLock *,&SimpleRWLock::AcquireReadLock,&SimpleRWLock::ReleaseReadLock,0,&CompareDefault<SimpleRWLock *>,2,1>+0x35, calling clr!_EH_epilog3
0867bd04 57a1346f clr!LockedRangeList::IsInRangeWorker+0x4f, calling clr!_EH_epilog3
0867bd2c 57a1346f clr!LockedRangeList::IsInRangeWorker+0x4f, calling clr!_EH_epilog3
0867bd30 57a134e3 clr!VirtualCallStubManager::isDispatchingStub+0x21
0867bd48 57a1396b clr!VirtualCallStubManager::BackPatchSite+0x34, calling clr!VirtualCallStubManager::isDispatchingStub
0867bd60 57a137f1 clr!VirtualCallStubManager::ResolveWorker+0x481, calling clr!VirtualCallStubManager::BackPatchSite
0867bd68 57a13819 clr!VirtualCallStubManager::ResolveWorker+0x515, calling clr!_EH_epilog3
0867bda4 57a0c44a clr!HndCreateHandle+0xf0, calling clr!_EH_epilog3
0867bdd0 57a0c44a clr!HndCreateHandle+0xf0, calling clr!_EH_epilog3
0867bdd4 57a0c5aa clr!BaseDomain::CreateHandle+0x23, calling clr!HndCreateHandle
0867bde8 57a5bf73 clr!Thread::SetLastThrownObject+0xca, calling clr!BaseDomain::CreateHandle
0867be0c 77a43ee5 KERNELBASE!RaiseException+0x55, calling ntdll!memcpy
0867be14 77a43ef2 KERNELBASE!RaiseException+0x62, calling ntdll!RtlRaiseException
0867be28 77a43ef2 KERNELBASE!RaiseException+0x62, calling ntdll!RtlRaiseException
0867be60 77d79d79 ntdll!RtlSetLastWin32Error+0x39, calling ntdll!__security_check_cookie
0867be7c 57a5e9d2 clr!RaiseTheExceptionInternalOnly+0x27c, calling KERNELBASE!RaiseException
0867bea8 77d79d79 ntdll!RtlSetLastWin32Error+0x39, calling ntdll!__security_check_cookie
0867bf18 57a5ebd8 clr!IL_Throw+0x141, calling clr!RaiseTheExceptionInternalOnly
0867bf7c 57a5eb1c clr!IL_Throw+0x3c, calling clr!LazyMachStateCaptureState
0867bf90 1849ab94 (MethodDesc 185b3a10 +0x24 System.Diagnostics.Stopwatch.GetTimestamp()), calling 18499c04
0867bfa0 1849abd0 (MethodDesc 185b3a1c +0x20 System.Diagnostics.Stopwatch.GetRawElapsedTicks()), calling (MethodDesc 185b3a10 +0 System.Diagnostics.Stopwatch.GetTimestamp())

I have an idea for a solution, but not sure if I’ll have time to apply it today.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fail with error & stack trace in asynchronous code #529
When an error is thrown from asynchronous code, Jasmine currently has no way of knowing that it happened, skipping subsequent stages (e.g. ...
Read more >
Rethrowing Exception without requiring throws Exception?
This code compiles without having to add throws Exception to the method signature. (It behaves similarly with Throwable in place of Exception , ......
Read more >
c# - What is the proper way to throwing an exception?
Don't create exceptions that can be thrown in debug mode but not release mode. To identify run-time errors during the development phase, use...
Read more >
Creating and Throwing Exceptions
Learn about creating and throwing exceptions. Exceptions are used to indicate that an error has occurred while running a program.
Read more >
Exception specifications (throw, noexcept) (C++)
When an exception is thrown from a function declared as throw() in /std:c++14 mode, the result is undefined behavior. No specific function is ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found