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.
Related
In VS2019 community on Win10/64 I have a Com port with which my c# program exchanges serial data.
The same port can be opened by another application on the same computer.
When the port is opened by the other app and I try to access the port from my current app, I (expectedly) receive an exception message.
The exception message is of type: System.Reflection.TargetInvocationException: 'Exception has been thrown by the target of an invocation.
and the inner exception is: UnauthorizedAccessException: Access to the port 'COM6' is denied.
So far so good.
The problem is that I can not handle that (or any other relevant) exception. With any exception catch I still get the message and the program halts to be restarted.
I have tried the general exception handler and all relevant (as is indicated by the documentation) exceptions and placed a break in each handler, for no avail. The program still halts at the same exception message.
This is what I have tried (each with a break):
catch (Exception ex)
catch (System.Reflection.TargetInvocationException portInUse)
catch (UnauthorizedAccessException portInUse)
catch (InvalidOperationException portInUse)
catch (FormatException)
catch (System.ArgumentOutOfRangeException)
This is the full call stack message:
This exception was originally thrown at this call stack:
System.IO.Ports.InternalResources.WinIOError(int, string)
System.IO.Ports.SerialStream.SerialStream(string, int, System.IO.Ports.Parity, int,
System.IO.Ports.StopBits, int, int, System.IO.Ports.Handshake, bool, bool, bool, byte)
System.IO.Ports.SerialPort.Open()
Lift_Motor_Control.Main.btnSend_Click(object, System.EventArgs) in Main.cs
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
My .NET application uses BlockingCollection to process data received from RabbitMQ. Sometimes on production it stops processing messages. I made dump using procdump util and tried to inspect the dump using WinDbg. !threads command shows me one thread with LockCount=1. ~*e!clrstack command gave me this output for another thread:
Child SP IP Call Site
0f3bf068 7760c8ac [GCFrame: 0f3bf068]
0f3bf118 7760c8ac [HelperMethodFrame_1OBJ: 0f3bf118] System.Threading.Monitor.ObjWait(Boolean, Int32, System.Object)
0f3bf1a4 70ac5fb7 System.Threading.Monitor.Wait(System.Object, Int32, Boolean)
0f3bf1b4 70ad76a7 System.Threading.SemaphoreSlim.WaitUntilCountOrTimeout(Int32, UInt32, System.Threading.CancellationToken)
0f3bf1d0 70ad75f0 System.Threading.SemaphoreSlim.Wait(Int32, System.Threading.CancellationToken)
0f3bf230 703dc110 System.Collections.Concurrent.BlockingCollection`1[[System.__Canon, mscorlib]].TryAddWithNoTimeValidation(System.__Canon, Int32, System.Threading.CancellationToken)
I tried to find more information about locks. !locks command gave me only this Scanned 40 critical sections. !SyncBlock shows this:
Index SyncBlock MonitorHeld Recursion Owning Thread Info SyncBlock Owner
-----------------------------
Total 1364
CCW 1
RCW 1
ComClassFactory 0
Free 713
What isGCFrame? What is CCW and RCW? I can't find it in documentation.
Also is it a way to find the thread which owns the lock?
I have a worker role which consumes messages from an Azure Queue does some processing in the background.
When I check my log there appears to be no exception logged while processing the message but still in my logs I get the following Exception(pasting some of the relevant texts from the long error log only):
System.Net.WebException
Microsoft.WindowsAzure.Storage.StorageException Exception messages:
The remote server returned an error: (404) Not Found. The remote
server returned an error: (404) Not Found. Stack Traces: at
System.Net.HttpWebRequest.GetResponse() at
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1
cmd, IRetryPolicy policy, OperationContext operationContext) --- Next
Call Stack: at
Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand1
cmd, IRetryPolicy policy, OperationContext operationContext) at
Microsoft.WindowsAzure.Storage.Queue.CloudQueue.DeleteMessage(String
messageId, String popReceipt, QueueRequestOptions options,
OperationContext operationContext) at
InnovativeExams.Azure.CloudStorage.AzureQueue`1.DeleteMessage(T
message) The specified message does not exist.
ErrorCode:MessageNotFound Prod-WorkerError Context
Here is the code I have in the worker role:
private void ProcessQueueMessage(object queueMessageToProcess)
{
var queueMessage = queueMessageToProcess as EventCompletedQueueMessage;
try
{
if (_eventCompletedProcessor.Process(queueMessage))
_azureQueue.DeleteMessage(queueMessage);
}
catch (Exception ex)
{
_logger.LogError(string.Format("Event Completed message <{0}> was not processed due to an exception", queueMessage.Id), ex, LogSources.WorkerRole_EventCompletedDispatcher);
}
}
The above exception is caught in the above catch block and logged.
I think there is some problem when the worker role tries to Delete the message in the Queue and the message is not found.
Need help from someone to help me resolve this error and to know the background what's going wrong here.
Few Questions asked in comment:
1) Are you running multiple instances of your worker role?
A: This is an existing application and I figured out we are using ThreadPool to pre-instantiate, threads which stand ready to be given work.
2) How are you "getting" the messages in your worker role? Are you using some kind of leader election pattern to decide which instance gets the messages?
A: Yes there is a framework which determines the appropriate dispatcher for a QueueMessage to be processed.
3) When you get messages, what's the visibility timeout for those messages?
A: Its set to 120.
4) How long does it take for you to process these messages i.e. how much time between getting the message and deleting the messages?
A: I am not sure on this.
Let me explain under what situation you would get the error you're encountering.
When you dequeue a message (i.e. GET Messages in Azure terminology), Azure Queue Service returns something called popreceipt which must be used to delete or update a message. This popreceipt is an opaque value (i.e. you should not build any business logic around it) which remains valid till the time that same message is dequeued again. When a message is dequeued again, you get a new value for popreceipt and you should use this new value to delete or update the message.
If you try to use an old popreceipt value to delete a message while the message was dequeued again (by some other process), you will get the error you're getting.
My guess is that this is what is happening in your application. Please check if this is indeed the case:
One of the worker role instances dequeues the message and starts working on that message. Based on what you told above, you hide the message for 120 seconds when you dequeue a message. I am assuming that the actual time taken for processing the message is more than 120 seconds and thus the message reappears in the queue. There's another process which now dequeues this message (and thus you get a new popreceipt). However soon after the 2nd process dequeued the message, 1st process finished working on the message and now it wants to delete the message using the popreceipt it has. Because this popreceipt is no longer valid, any attempt to perform delete operation on that message using this popreceipt will cause a message not found error.
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.
I have a Console app that uses a parallel.foreach producing a number of working threads that performs some tasks such as
reading OS data through SNMP from a number of servers in our intranet and
writes these values to a SQL server DB.
When I ran the code within Visual Studio 2010 in either debug- or release mode the program executes without exceptions. When I deploy the program and run it outside VS I get an exception (.NET Runtime Exceptiopn).
Stack Trace:
Application: ServerMonitorSNMP.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.AggregateException Stack: at System.Threading.Tasks.Parallel.ForWorker[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](Int32, Int32, System.Threading.Tasks.ParallelOptions, System.Action`1,
...
at ServerMonitoringSNMP.Program.Main(System.String[])
The AggregateException details are:
System.UnhandledExceptionEventArgs
System.AggregateException: One or more errors occurred. ---> System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
(Inner Exception #0) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
(Inner Exception #1) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
(Inner Exception #2) System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
...
System.AggregateException: One or more errors occurred. ---> System.NullReferenceException: Object reference not set to an instance of an object.
at ServerMonitoringSNMP.Program.GetStorageValue(Dictionary`2 StorageQuery, Int32 diskOidIndex) in \Program.cs:line 896
This is an unhandled aggregate exception. You will need to catch this type of error, loop through its exceptions and log each one to figure out what is going on.
You are probably not breaking on these types of exceptions in your debug routine.
Turn on Break on All Exceptions (Debug, Exceptions) or (CTRL + ALT + E) and rerun the program.
This will show you the original exception when it was thrown in the first place.
Catch code:
catch (AggregateException e)
{
foreach (var ex in e.InnerExceptions)
{
//log here
}
}
The attached exception is too abstract. The best approach would be handle the TPL exception and log. Things shall be much clearer when you do so. Then you can updated the post with proper exception.
Eg:
try
{
YourSNMPTrapSenderMethod();
}
catch (AggregateException ae)
{
// This is where you can choose which exceptions to handle.
foreach (var ex in ae.InnerExceptions)
{
// log your exception. may be in temp directory
}
}
Or
try
{
YourSNMPTrapSenderMethod();
}
catch (AggregateException ex)
{
ex.Handle((x) =>
{
Log.Write(x);
return true;
});
}
static void YourSNMPTrapSenderMethod()
{
var exceptions = new ConcurrentQueue<Exception>();
Parallel.ForEach(data, d =>
{
try
{
//do your operations
}
catch (Exception e) { exceptions.Enqueue(e); }
});
if (exceptions.Count > 0) throw new AggregateException(exceptions);
}