abergs/OwinFriendlyExceptions

Async thread block issue on response WriteAsync

Closed this issue · 8 comments

When an exception is triggered in quick session, thread gets blocked and throws unhanded exception that breaks web API, triggers Just-in-Time Debugger.

Source: OwinFriendlyExceptionsMiddleware.cs line 78.

Did a temporary fix by making the TransformException method synchronous including the
and changing line 78 to context.Response.Write(content)

Full stack trace below.

System.NullReferenceException: Object reference not set to an instance of an object. at System.Web.Hosting.IIS7WorkerRequest.FlushCachedResponse(Boolean isFinal) at System.Web.HttpResponse.UpdateNativeResponse(Boolean sendHeaders) at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async) at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size) at System.IO.Stream.<>c.<BeginWriteInternal>b__46_0(Object ) at System.Threading.Tasks.Task1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.IO.Stream.EndWrite(IAsyncResult asyncResult)
at System.IO.Stream.<>c.b__53_1(Stream stream, IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory1.FromAsyncTrimPromise1.Complete(TInstance thisRef, Func3 endMethod, IAsyncResult asyncResult, Boolean requiresSynchronization) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Greenfrog.Web.Exceptions.OwinFriendlyExceptionsMiddleware.<TransformException>d__8.MoveNext() in OwinFriendlyExceptionsMiddleware.cs:line 78 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Greenfrog.Web.Exceptions.OwinFriendlyExceptionsMiddleware.<Invoke>d__5.MoveNext() in OwinFriendlyExceptionsMiddleware.cs:line 66 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContextStage.<RunApp>d__5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.IntegratedPipelineContext.<DoFinalWork>d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) at System.Web.HttpApplication.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar)

Hi, thanks for reporting.
Oh, could you elaborate more on the problem?

Could a ConfigureAwait(false) be implemented here?

Thanks for your response. I implemented friendly exceptions and to test it, I made requests at a controller predefined to throw an exception. On every two to three requests, it hangs for a long time and the dialog (image below) appears. When i close that dialog the request returns a response with stack trace as above. Tried a ConfigureAwait(false), but still had the same problem. Thus i changed the TransformException method, making it fully synchronous and that worked smoothly.

jitdebug

@ATLSAPI I'm gonna close this method since we make full use of async controllers have yet to run into this problem in production (or dev) 👍

@ATLSAPI I recently ran into this problem! Will update the nuget with your proposed fix (we came to the same conclusion´).

I'm still baffled in how this bug works since we are indeed awaiting the async write.

I've got the same problem. Fixed via replacing suggested code above
@abergs could you make a patch and push nuget update?

This was fixed by #12 but I need to publish new nuget packages. Keeping it open until new ones are online.

tiny reminder ;)

Thank you @semack. It only took 5 mins once I did it!

Publish as v 1.2.0: https://www.nuget.org/packages/OwinFriendlyExceptions/1.2.0