The process was terminated due to an unhandled exception - c#

In the code for my windows service I have a try catch block.
When I run a unit test on my development machine on a the database , the unit test passes.
When the service runs on the production server with the same database, an error occurs and two events appear in the Windows Event log.
EventType clr20r3, P1 MKG.XPREFLIGHT.servicehost.exe, P2 2.0.62.0,
P3 574a1457, P4 mscorlib, P5 4.0.0.0, P6 53b50a71, P7 3e5, P8 10d,
P9 system.formatexception, P10 NIL.
and
Event Type: Error
Event Source: .NET Runtime
Event Category: None
Event ID: 1026
Date: 29/05/2016
Time: 8:03:54 AM
User: N/A
Computer: SERVERX1
Description:
Application: MKG.XPREFLIGHT.ServiceHost.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Reflection.TargetInvocationException
Stack:
at System.RuntimeMethodHandle._InvokeMethodFast(System.IRuntimeMethodInfo, System.Object, System.Object[], System.SignatureStruct ByRef, System.Reflection.MethodAttributes, System.RuntimeType)
at System.RuntimeMethodHandle.InvokeMethodFast(System.IRuntimeMethodInfo, System.Object, System.Object[], System.Signature, System.Reflection.MethodAttributes, System.RuntimeType)
at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo, Boolean)
at System.Reflection.RuntimeMethodInfo.Invoke(System.Object, System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
at System.Reflection.MethodBase.Invoke(System.Object, System.Object[])
at MKG.XPREFLIGHT.Engine.FlightEngine.ParseCommand(MKG.ServiceData.EngineCommandQueue)
at MKG.XPREFLIGHT.Engine.FlightEngine.TakeCommands()
at MKG.XPREFLIGHT.Engine.FlightEngine.PollingProc(System.Object)
at System.Threading._TimerCallback.TimerCallback_Context(System.Object)
at System.Threading.ExecutionContext.runTryCode(System.Object)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading._TimerCallback.PerformTimerCallback(System.Object)
I isolated the area of code that the error occurs in, but since the unit test passes I cannot see why the error occurs.
The error is repeatable although the fault does not occur in the same place the first time the process runs after a server reboot.
The code is within a Try Catch block, however it does not get caught. Instead the service stops with errors in the windows event log.
How can I troubleshoot this?
[update]
the code that fails is
public Job GetDeepJobWithUploadInfo(int jobId)
{
// if I throw an error here it is caught
Job job = null;
using (var connect = MakeConnect())
{
job =
connect.job.AsNoTracking()
.Include(
j =>
j.VivUpload.Select(
u =>
u.VivSplitUpload.Select(
s =>
s.VivSplitProfile.Select(
p => p.VivSplitLog.Select(l => l.VivSplitLogType)))))
.Include(j => j.VivUpload.Select(u => u.VivSplitUpload.Select(s => s.VivSplitProfile)))
.Include(j => j.JobTag)
.SingleOrDefault(j => j.JobID == jobId);
}
// never gets to here in production, but does get here in unit test
return job;
}
I have tried running the custom tool on the edmx to regenerate the Entity Framework classes.

Related

reactjs.net Thread was being aborted

We've developed our code using Ractjs 3, JavaScriptEngineSwitcher, and Microsoft.ChakraCore.
Once in a while my application took some seconds to load a page but I didn't have any errors on my application log so I decided to diagnose it with DebugDiag tools on windows server. Then I found several ThreadAbortException related to Chakra.
System.Threading.ThreadAbortException
Thread was being aborted
System.Threading.WaitHandle.WaitOneNative(System.Runtime.InteropServices.SafeHandle, UInt32, Boolean, Boolean)
System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle, Int64, Boolean, Boolean)
System.Threading.WaitHandle.WaitOne(Int32, Boolean)
JavaScriptEngineSwitcher.ChakraCore.ScriptDispatcher.StartThread()
System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
System.Threading.ThreadHelper.ThreadStart()
20% of all threads have:.
Entry point ChakraCore!JsDisposeRuntime+12a44
Call Stack
ntdll!NtWaitForMultipleObjects+14
KERNELBASE!WaitForMultipleObjectsEx+ef
ChakraCore+bd824
ChakraCore!JsDisposeRuntime+26ee
ChakraCore!JsDisposeRuntime+12aa1
kernel32!BaseThreadInitThunk+14
ntdll!RtlUserThreadStart+21
Also another 20% have:
Entry point ChakraCore!JsDisposeRuntime+12a44
Call Stack
ntdll!NtWaitForMultipleObjects+14
KERNELBASE!WaitForMultipleObjectsEx+ef
ChakraCore!JsGetPropertyIdFromName+1673
ChakraCore!JsGetPropertyIdFromName+1410
ChakraCore!JsGetPropertyIdFromName+993
ChakraCore!JsDisposeRuntime+12aa1
kernel32!BaseThreadInitThunk+14
ntdll!RtlUserThreadStart+21
Are you correctly adding references to JavaScriptValues that you are processing on the native side? If not, you'll have memory corruption of which one of the symptoms would be crashes on disposal of the ChakraCore context/runtime.
For an example of how to guard against this class of issues, see React Native Windows' real-world example of how to safely embed ChakraCore (while still being performant).

Thread.Abort in ASP.NET app causes w3wp.exe to crash

Please do not set duplicate flag on this qustion - it is not about "why ThreadAbortException occurs", it is about "why w3wp.exe process terminates after ThreadAbortException".
Let's say we have simple web application with following code sample:
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("http://google.com");
}
Which by fact means something like (see Is Response.End() considered harmful?):
protected void Page_Load(object sender, EventArgs e)
{
...response write some data...
System.Threading.Thread.CurrentThread.Abort();
}
On my machine (Windows 10 Pro + IIS) this code leads to IIS pool process termination with error code 0x0 (redirect not performs). On other machines (which is NOT Windows 10) this code only generates ThreadAborted exception, but process continue working (redirect performs).
Can someone check this sample and explain what is going on?
UPDATE
Here some windows event logs related to this issue.
log #1
An unhandled exception occurred and the process was terminated.
Application ID: /LM/W3SVC/1/ROOT/AS
Process ID: 6700
Exception: System.Threading.ThreadAbortException
Message: Thread was being aborted.
StackTrace: at
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest
wr, HttpContext context) at
System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr
rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData,
Int32 flags) at
System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr
rootedObjectsPointer, IntPtr nativeRequestContext, IntPtr moduleData,
Int32 flags)
log #2
Faulting application name: w3wp.exe, version: 10.0.10240.16384, time stamp: 0x559f3dad
Faulting module name: KERNELBASE.dll, version: 10.0.10240.16384, time stamp: 0x559f3b2a
Exception code: 0xe0434352
Fault offset: 0x000b3e28
Faulting process id: 0x1a2c
Faulting application start time: 0x01d0e4b1b3ed01cb
Faulting application path: C:\WINDOWS\SysWOW64\inetsrv\w3wp.exe
Faulting module path: C:\WINDOWS\SYSTEM32\KERNELBASE.dll
Report Id: 23b5298d-3b36-49c7-a294-de9c864b703f
Faulting package full name:
Faulting package-relative application ID:
I was able to reproduce the issue on Server 2008r2 with .NET 4.6 installed.
I suspect it was the same problem the rest of you are running into; ThreadAbortExceptions killing the application pool in the event log (though any unhandled exception would cause the issue in my case; but that may just be a global exception handler catching it and finishing with a Response.End or Redirect). The dump stacktrace also matches the one in Ian's answer.
There was a MS Connect ticket opened for the issue, and a recent KB hotfix resolves the issue for me.
Connect Article: https://connect.microsoft.com/VisualStudio/feedback/details/1605438/disabling-ryujit-breaks-asp-net
KB Hotfix:
https://support.microsoft.com/en-us/kb/3098786
So far I have the only one solution:
static class WebExtensions
{
public static void EndSafe(this HttpResponse response)
{
response.Flush();
response.SuppressContent = true;
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
public static void RedirectSafe(this HttpResponse response, string url)
{
response.Redirect(url, false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
This is however forces me to ensure that there will be no code executed after it:
...some code
response.RedirectSafe(url);
return; //<-- important
...some more code
Pay attention, that only "return" is not enough in some cases (for example with recursive calls) and in some cases you may need to avoid using "return" (with try-finally constructions)
I ran into this exact same problem on Windows 8.1 today, after rebooting to install Windows Updates.
The problem was that I had manually disabled RyuJIT in the Registry, due to this issue, by adding the useLegacyJit DWORD and setting it to 1 (see Method #3). But one of the updates created the UseRyuJIT key in the same location and set it to 1 as well, and this apparently confused ASP.NET horribly.
The solution was to set useLegacyJit to 0 and issue an iisreset. After that, all is good in the world.
WinDbg's !clrstack showed the following frames when I debugged the w3wp.exe dump. Perhaps this will help others with the same error who are Googling for a solution:
000000ef9892be98 00007ffa0e2d1fea [HelperMethodFrame: 000000ef9892be98]
000000ef9892bf80 00007ff99d776588 System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)
000000ef9892df90 00007ff9fc172345 [FaultingExceptionFrame: 000000ef9892df90]
000000ef9892e490 00007ff99d7796c0 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest, System.Web.HttpContext)
000000ef9892e520 00007ff99d777377 System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)
000000ef9892e700 00007ff99d77655a System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)
000000ef9892e740 00007ff99d775c11 DomainNeutralILStubClass.IL_STUB_ReversePInvoke(Int64, Int64, Int64, Int32)
000000ef9892ef58 00007ff9fc100b4e [InlinedCallFrame: 000000ef9892ef58] System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr, System.Web.RequestNotificationStatus ByRef)
000000ef9892ef58 00007ff99d78cc1b [InlinedCallFrame: 000000ef9892ef58] System.Web.Hosting.UnsafeIISMethods.MgdIndicateCompletion(IntPtr, System.Web.RequestNotificationStatus ByRef)
000000ef9892ef30 00007ff99d78cc1b DomainNeutralILStubClass.IL_STUB_PInvoke
000000ef9892f000 00007ff99d77756c System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)
000000ef9892f1e0 00007ff99d77655a System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)
000000ef9892f220 00007ff99d775c11 DomainNeutralILStubClass.IL_STUB_ReversePInvoke(Int64, Int64, Int64, Int32)
000000ef9892f418 00007ff9fc100da3 [ContextTransitionFrame: 000000ef9892f418]
I've been experiencing the same issue on Win7 SP1. Web app compiled targeting .net 4.5.2 and running on .net 4.6. And I haven't been messing with the useLegacyJit or useRyuJIT registry flags.
Turned out "Enable 32-Bit applications" was unnecessarily set to Enabled on my app domain. Disabling it fixed the problem.

NullReferenceException throw by CompletionPortCallback

My applicatiion is crash twice and the stacktrace as below:
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
My application use Begin/End async IO model as below:
HttpRequest.BeginGetRequestStream(callback,context);
singal.wait(3000);
.......
context.dispose();//the context contain buffer to be read and buffer to be sent and the networkstream.
Is the problem caused by the overlapped data removed by call context.dispose method when the thread start?
but i must set a time to wait,otherwise the thread will be block for a long time.

Get object details from clrstack output in windbg

I have a multi-threaded .NET application that hangs on an OnUserPreferenceChanged event. This is typically caused by a UI control or message loop started on a background thread (see e.g. http://www.ikriv.com/en/prog/info/dotnet/MysteriousHang.html), but as far as I can tell that isn't the case here. I verified this by setting a breakpoint in the WindowsFormsSynchronizationContext (as suggested here http://www.aaronlerch.com/blog/2008/12/15/debugging-ui/) and it is only constructed once, in the main UI thread.
Here's the output from !clrstack in windbg:
0013eea8 7c90e514 [HelperMethodFrame_1OBJ: 0013eea8]
System.Threading.WaitHandle.WaitOneNative(Microsoft.Win32.SafeHandles.SafeWaitHandle,
UInt32, Boolean, Boolean) 0013ef54 792b68af
System.Threading.WaitHandle.WaitOne(Int64, Boolean) 0013ef70 792b6865
System.Threading.WaitHandle.WaitOne(Int32, Boolean) 0013ef84 7b6f1a4f
System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle)
0013ef98 7ba2d68b
System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control,
System.Delegate, System.Object[], Boolean) 0013f038 7b6f33ac
System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[])
0013f06c 7b920bd7
System.Windows.Forms.WindowsFormsSynchronizationContext.Send(System.Threading.SendOrPostCallback,
System.Object) 0013f084 7a92ed62
Microsoft.Win32.SystemEvents+SystemEventInvokeInfo.Invoke(Boolean,
System.Object[]) 0013f0b8 7a92dc8f
Microsoft.Win32.SystemEvents.RaiseEvent(Boolean, System.Object,
System.Object[]) 0013f104 7a92e227
Microsoft.Win32.SystemEvents.OnUserPreferenceChanged(Int32, IntPtr,
IntPtr) 0013f124 7aaa06ec
Microsoft.Win32.SystemEvents.WindowProc(IntPtr, Int32, IntPtr, IntPtr)
The last method I can get param info on is:
0013f084 7a92ed62
Microsoft.Win32.SystemEvents+SystemEventInvokeInfo.Invoke(Boolean,
System.Object[])
PARAMETERS:
this = 0x01404420
checkFinalization = 0x00000001
args = 0x0144a298
Here's my question: How can I get more information here? Ultimately, I'd like to know which objects and/or threads this Invoke is for. Something like "!do 0x01404420" or "!do 0x0144a298" but I don't know where to go from there.
Search for exceptions in the heap by using !dumpheap -type Exception.
Also you can see the value of variables in a class,which will be useful to understand the state of the class. Use !dumpheap -type ClassName. You will get a MT(Method Table) address. From MT address see the Object address. Use !do address to dump the class.
Use !syncblk to see the locked threads
Regarding problems caused by SystemEvents class and in particular OnUserPreferenceChanged event try to use the CheckSystemEventsHandlersForFreeze() function in this answer which can help to find root cause, i.e. which controls was created on wrong thread and thus causing freeze.

How to deal with this C# hang invloving SystemEvents.OnUserPreferenceChanged

My WinForm application having a hang problem. What happen is that client sometime leave the application running overnight and when they comeback in morning application is typically in a hang state. This is what I see in dump file on the main thread. What I don't understand is what could make SystemEvents.OnUserPreferenceChanged event to be invoked, although I don't think I am doing anything that is invoking this event.
0024e480 770496f4 System.Threading.WaitHandle.WaitOneNative(Microsoft.Win32.SafeHandles.SafeWaitHandle, UInt32, Boolean, Boolean)
0024e52c 702c68af System.Threading.WaitHandle.WaitOne(Int64, Boolean)
0024e548 702c6865 System.Threading.WaitHandle.WaitOne(Int32, Boolean)
0024e55c 6e891a6f System.Windows.Forms.Control.WaitForWaitHandle(System.Threading.WaitHandle)
0024e570 6ebcd6eb System.Windows.Forms.Control.MarshaledInvoke(System.Windows.Forms.Control, System.Delegate, System.Object[], Boolean)
0024e610 6e8933cc System.Windows.Forms.Control.Invoke(System.Delegate, System.Object[])
0024e644 6eac0c83 System.Windows.Forms.WindowsFormsSynchronizationContext.Send(System.Threading.SendOrPostCallback, System.Object)
0024e65c 6fe1eed2 Microsoft.Win32.SystemEvents+SystemEventInvokeInfo.Invoke(Boolean, System.Object[])
0024e690 6fe1d07f Microsoft.Win32.SystemEvents.RaiseEvent(Boolean, System.Object, System.Object[])
0024e6dc 6fe1e38f Microsoft.Win32.SystemEvents.OnUserPreferenceChanged(Int32, IntPtr, IntPtr)
0024e6fc 6fa64c29 Microsoft.Win32.SystemEvents.WindowProc(IntPtr, Int32, IntPtr, IntPtr)
0024e700 000a1104 [InlinedCallFrame: 0024e700]
0024e8d8 6e378d5e System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32, Int32, Int32)
0024e974 6e3789c7 System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
0024e9c8 6e378811 System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
0024e9f8 6e88de47 System.Windows.Forms.Application.RunDialog(System.Windows.Forms.Form)
0024ea0c 6e8c25cb System.Windows.Forms.Form.ShowDialog(System.Windows.Forms.IWin32Window)
0024ea98 6e8c27e3 System.Windows.Forms.Form.ShowDialog()
0024ea9c 56c26e76 MyNameSpace.MyForm.MyMethod2(Object, Boolean, Boolean, System.Guid, Boolean)
0024eba0 56c26c47 MyNameSpace.MyForm.MyMethod1(System.Guid, System.Guid, System.Guid, Boolean)
0024ecf8 56c91f4c MyNameSpace.MyForm.MyButton_Click(System.Object, System.EventArgs)
0024ee88 6e334180 System.Windows.Forms.Control.OnClick(System.EventArgs)
Controls subscribe this event so that they will redraw themselves when the user changed the theme or system colors. This event also gets fired when you're not close to the machine and Windows automatically locks the workstation. Which explains the morning-after hangover.
The deadlock is caused by a threading problem, the SystemEvents class fires the event on the wrong thread. Which is caused by an initialization problem in your program. The typical trigger is not creating the first window on the main thread, that confuzzles SystemEvents. It tries to fire an event on that same thread again but it isn't around anymore. Or it copied SynchronizationContext.Current before it got initialized by Winforms. Either way, the event will fire on a threadpool thread instead of the main UI thread. That's lethal.
Common when you implement your own splash screen for example. Use the built-in support instead.

Categories

Resources