Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Owin SelfHost WebApi - client closing the connection during response raises an exception?

I'm running an Owin Selfhost based WebApi where i've put in an API unhandled exception logger via

config.Services.Add(typeof(IExceptionLogger), _apiExceptionLogger);

Relevant part of ApiExceptionLogger:

    public override void Log(ExceptionLoggerContext context)
    {
        if (context == null || context.ExceptionContext == null) return;

        Logger.Error("Unhandled exception from Web API", context.ExceptionContext.Exception);
    }

The cases it's catching and logging regularly are ones where the client requests a dataset and then closes the connection while the results (JSON) are being sent back - people making a request in chrome, and then hitting the X button before all results come back :P

I've pasted a stacktrace below for completeness, just want to know two things:

  • Is this regular/expected behavior? AFAIK it is...I'm running a pretty default API and pipeline
  • Is there any way to handle this? Essentially stop request processing more gracefully in case of a cancellation (the cancellation tokens peppered throughout the request pipeline do come to mind, but doesn't look like they do much in this case, after all the tokens only support co-operative cancellation)

I haven't done any deep dive on the sequence of events happening at the socket level, so far this is only a logging nuisance.

System.Net.Http.HttpRequestException: Error while copying content to a stream. ---> System.IO.IOException ---> System.Net.HttpListenerException: The I/O operation has been aborted because of either a thread exit or an application request
   at System.Net.HttpResponseStream.EndWrite(IAsyncResult asyncResult)
   at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.EndWrite(IAsyncResult asyncResult)
   --- End of inner exception stack trace ---
   at Microsoft.Owin.Host.HttpListener.RequestProcessing.ExceptionFilterStream.EndWrite(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
   --- End of inner exception stack trace ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Owin.HttpMessageHandlerAdapter.<SendResponseContentAsync>d__20.MoveNext()    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Owin.HttpMessageHandlerAdapter.<SendResponseContentAsync>d__20.MoveNext()
like image 338
Vivek Avatar asked May 07 '15 18:05

Vivek


1 Answers

I had a similar issue with Owin host on Raspberry Pi. This may help https://stackoverflow.com/a/30583109/1786034

like image 80
zmechanic Avatar answered Oct 14 '22 15:10

zmechanic