Thursday, September 13, 2018

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

Leave a Comment

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() 

2 Answers

Answers 1

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

Answers 2

Its code issue

Read file async and check.At any time if you are doing an api call and it times out it will definitely throw exception.

using (StreamReader reader = new StreamReader(await selectedFile.OpenStreamForReadAsync()))             {                 while ((nextLine = await reader.ReadLineAsync()) != null)                 {                     contents.AppendFormat("{0}. ", lineCounter);                     contents.Append(nextLine);                     contents.AppendLine();                     lineCounter++;                     if (lineCounter > 3)                     {                         contents.AppendLine("Only first 3 lines shown.");                         break;                     }                 }             } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment