Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OWIN SelfHost Web Api - Request Cancellation - How is it done? Thread aborts?

Could someone point me to a resource that would help explain how web api (specifically using Owin Self Host) handles request cancellation?

Here's the sequence of events i'm observing:

  • someone makes a GET from chrome
  • Api controller (through some layers) fires off an async SQL query
  • someone hits the X button in chrome (i don't know exactly what happens on socket for this)

What happens next in Web Api??

There was some code running in a controller, does the thread running it get aborted? If it was an async controller awaiting another Task, does that task still have an awaiter in case it returns with an exception?

For context: I do have an async controller awaiting a Task (this is the only call site) which looks to be throwing an unobserved exception in some edge cases. I haven't been able to isolate or re-produce yet :)

I did find something called HttpResponse.ClientDisconnectedToken, but don't know well that is supported in Owin Selfhost + is it even the good thing to use for all user cancels.

like image 722
Vivek Avatar asked May 07 '15 19:05

Vivek


People also ask

How does a cancellation token work?

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource. Token property.

How do you handle a cancellation token?

The wait handle of the cancellation token will become signaled in response to a cancellation request, and the method can use the return value of the WaitAny method to determine whether it was the cancellation token that signaled. The operation can then just exit, or throw an OperationCanceledException, as appropriate.

What is cancellation token in .NET core?

So CancellationToken can be used to terminate a request execution at the server immediately once the request is aborted or orphan. Here we are going to see some sample code snippets about implementing a CancellationToken for Entity FrameworkCore, Dapper ORM, and HttpClient calls in Asp.


1 Answers

I've dealt with this by handing the System.OperationCanceledException in a custom middleware I've registered before WebApi.

public class ExceptionHanldingMiddleware : OwinMiddleware
{
    public override async Task Invoke(IOwinContext context)
    {
        try
        {
            await Next.Invoke(context);
        }
        catch (OperationCanceledException) when (context.Request.CallCancelled.IsCancellationRequested)
        {
            //swallow user-agent cancelling request.
            _log.Trace($"client disconnected on request for: {context.Request.Path}.");
        }
        catch (Exception ex)
        {
            _log.Error(ex);
            context.Response.StatusCode = (int) HttpStatusCode.InternalServerError;
            context.Response.ReasonPhrase = "Internal Server Error";
        }
    }   
}
like image 140
Dav Evans Avatar answered Oct 27 '22 05:10

Dav Evans