Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is Nancy's CancellationToken for async Request Handlers coming from and when is it cancelled?

This is more a question regarding understanding how and when Nancy might cancel an async request handler via the provided cancellationToken instance.

Basically what I am wondering about is in what conditions is the token's .IsCancellationRequested set to true - is this documented or explained somewhere?

How does Nancy handle async handlers that 'never' return / in 'time'? And regarding 'in time': is there a timeout / limit after which handlers do get cancelled?

like image 314
Jörg Battermann Avatar asked Jun 05 '15 20:06

Jörg Battermann


2 Answers

I know this is an old question but I was in your same situation and I think I found an answer at least if you are using Owin to host your app (using Nancy.Owin).

The CancellationToken comes directly from Owin via the IOwinRequest.CallCancelled property (Nancy source code and used here). This token can be set by Owin if the request is cancelled (for example by forcibly closing the HTTP connection).

like image 113
aghidini Avatar answered Oct 15 '22 02:10

aghidini


Every CancellationToken comes from somewhere, and that somewhere is its CancellationTokenSource.

When you call CancellationTokenSource.Cancel, every token created from it is flagged.


Fun fact: CancellationToken is a struct, which means every time you pass it to a function or assign it to a variable it makes a new copy. Since the source can't keep track of all of those copies, we can't have a CancellationToken.IWasCancelled event. Instead, when you call IsCancellationRequested, the token has to asks its source.

ref: https://msdn.microsoft.com/en-us/library/system.threading.cancellationtokensource(v=vs.110).aspx


So going back to Nancy, search their source code for CancellationTokenSource and you'll find your answer. Here's the only one I saw.

https://github.com/NancyFx/Nancy/blob/8a29b0495bfac4806536327c4d78de1ee59bd513/src/Nancy/NancyEngine.cs

like image 32
Jonathan Allen Avatar answered Oct 15 '22 04:10

Jonathan Allen