Quantcast
Channel: Common Language Runtime Internals and Architecture forum
Viewing all articles
Browse latest Browse all 1710

c# TheadPool and HttpListener issue

$
0
0

our application is currently running a little https service.
Sometimes the application is unexpectedly crashing.
The problem occurs in the loop method and it seems, that the exception cannot be caught.

What can be done to prevent this error?

    Unhandled Exception: System.ObjectDisposedException: Safe handle has been closed
       at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
       at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
       at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
       at System.Threading.EventWaitHandle.Set()
       at MonitoringProxy.WebServer.<>c__DisplayClass5.<ProcessRequest>b__0(Object sender, DataReceivedEventArgs e)
       at System.Diagnostics.Process.OutputReadNotifyUser(String data)
       at System.Diagnostics.AsyncStreamReader.FlushMessageQueue()
       at System.Diagnostics.AsyncStreamReader.ReadBuffer(IAsyncResult ar)
       at System.IO.Stream.ReadWriteTask.InvokeAsyncCallback(Object completedTask)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object
    state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, B
    oolean preserveSyncCtx)
       at System.IO.Stream.ReadWriteTask.System.Threading.Tasks.ITaskCompletionAction.Invoke(Task completingTask)
       at System.Threading.Tasks.Task.FinishContinuations()
       at System.Threading.Tasks.Task.FinishStageThree()
       at System.Threading.Tasks.Task.FinishStageTwo()
       at System.Threading.Tasks.Task.Finish(Boolean bUserDelegateExecuted)
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
       at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
    System.ObjectDisposedException: Safe handle has been closed
       at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
       at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
       at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
       at System.Threading.EventWaitHandle.Set()
       at MonitoringProxy.WebServer.<>c__DisplayClass8.<ProcessRequest>b__1(Object sender, DataReceivedEventArgs e)
       at System.Diagnostics.Process.ErrorReadNotifyUser(String data)
       at System.Diagnostics.AsyncStreamReader.FlushMessageQueue()
       at System.Diagnostics.AsyncStreamReader.ReadBuffer(IAsyncResult ar)
       at System.IO.Stream.ReadWriteTask.InvokeAsyncCallback(Object completedTask)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object
    state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, B
    oolean preserveSyncCtx)
       at System.IO.Stream.ReadWriteTask.System.Threading.Tasks.ITaskCompletionAction.Invoke(Task completingTask)
       at System.Threading.Tasks.Task.FinishContinuations()
       at System.Threading.Tasks.Task.FinishStageThree()
       at System.Threading.Tasks.Task.FinishStageTwo()
       at System.Threading.Tasks.Task.Finish(Boolean bUserDelegateExecuted)
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
       at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()


    Unhandled Exception:Unhandled Exception:  System.ObjectDisposedException: Safe handle has been closed
       at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
       at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
       at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
       at System.Threading.EventWaitHandle.Set()
       at MonitoringProxy.WebServer.<>c__DisplayClass8.<ProcessRequest>b__1(Object sender, DataReceivedEventArgs e)
       at System.Diagnostics.Process.ErrorReadNotifyUser(String data)
       at System.Diagnostics.AsyncStreamReader.FlushMessageQueue()
       at System.Diagnostics.AsyncStreamReader.ReadBuffer(IAsyncResult ar)
       at System.IO.Stream.ReadWriteTask.InvokeAsyncCallback(Object completedTask)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object
    state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, B
    oolean preserveSyncCtx)
       at System.IO.Stream.ReadWriteTask.System.Threading.Tasks.ITaskCompletionAction.Invoke(Task completingTask)
       at System.Threading.Tasks.Task.FinishContinuations()
       at System.Threading.Tasks.Task.FinishStageThree()
       at System.Threading.Tasks.Task.FinishStageTwo()
       at System.Threading.Tasks.Task.Finish(Boolean bUserDelegateExecuted)
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
       at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
    System.ObjectDisposedException: Safe handle has been closed
       at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
       at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
       at Microsoft.Win32.Win32Native.SetEvent(SafeWaitHandle handle)
       at System.Threading.EventWaitHandle.Set()
       at MonitoringProxy.WebServer.<>c__DisplayClass5.<ProcessRequest>b__0(Object sender, DataReceivedEventArgs e)
       at System.Diagnostics.Process.OutputReadNotifyUser(String data)
       at System.Diagnostics.AsyncStreamReader.FlushMessageQueue()
       at System.Diagnostics.AsyncStreamReader.ReadBuffer(IAsyncResult ar)
       at System.IO.Stream.ReadWriteTask.InvokeAsyncCallback(Object completedTask)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object
    state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, B
    oolean preserveSyncCtx)
       at System.IO.Stream.ReadWriteTask.System.Threading.Tasks.ITaskCompletionAction.Invoke(Task completingTask)
       at System.Threading.Tasks.Task.FinishContinuations()
       at System.Threading.Tasks.Task.FinishStageThree()
       at System.Threading.Tasks.Task.FinishStageTwo()
       at System.Threading.Tasks.Task.Finish(Boolean bUserDelegateExecuted)
       at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
       at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)
       at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
       at System.Threading.ThreadPoolWorkQueue.Dispatch()
       at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()



The code of the loop and processReqeust methods is listed below:


        public void Loop()
        {
            while (shouldRun)
            {

                try
                {
                    HttpListenerContext request = _listener.GetContext();
                    if (Environment.UserInteractive)
                    {
                        Console.WriteLine("Received request: " + request.Request.RemoteEndPoint.Address);
                    }
                    ThreadPool.QueueUserWorkItem(ProcessRequest, request);
                }
                catch (HttpListenerException e)
                {
                    LogWriter.WriteLog("An HttpListenerException occured with message: " + e.Message);

                }
                catch (ObjectDisposedException e)
                {
                    LogWriter.WriteLog("An ObjectDisposedException occured with message: " + e.Message);
                }
                catch (InvalidOperationException e)
                {
                    LogWriter.WriteLog("An InvalidOperationException occured with message: " + e.Message);
                }
                catch (NotSupportedException e)
                {
                    LogWriter.WriteLog("An NotSupportedException occured with message: " + e.Message);
                }
                catch (ArgumentNullException e)
                {
                    LogWriter.WriteLog("An ArgumentNullException occured with message: " + e.Message);
                }
                catch (Exception e)
                {
                    LogWriter.WriteLog("An Exception occured with message: " + e.Message);
                }
            }
        }

        void ProcessRequest(object listenerContext)
        {
            try
            {
                // the magic code
            }
            catch (Exception e)
            {
                LogWriter.WriteLog("An Exception occured within ProcessRequest with message: " + e.Message + "\n" + e.StackTrace, EventLogEntryType.Error);
            }
        }



Viewing all articles
Browse latest Browse all 1710

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>