CancellationToken
Sorry if this has been asked before but i cant find any documentation on it. Is it possible in MVC6 to get a CancellationToken triggered when aborting an ajax request for example. IsCancellationRequested is always false. From what i understand this was possible in MVC4 and WebAPI, not MVC 5+6?
public async Task<IActionResult> CancelTest(int id, CancellationToken cToken)
{
Debug.WriteLine("Started");
for(var i = 1; i <= 10; i++)
{
await Task.Delay(3000, cToken);
if (cToken.IsCancellationRequested)
{
Debug.WriteLine("Canceled");
break;
}
}
return Json(new { data = "CancellationTest", id = id });
}
Use the httpContext.RequestAborted token?
Thank you. Same behaviour. I believe if you put a CancellationToken in your action method the model binder binds it to HttpContext.RequestAborted though.
Yeah, using httpContext.RequestAborted is the same thing as taking it as a parameter; https://github.com/aspnet/Mvc/blob/a5db01169af91bee3eca6ebfabf2b6b3d6c75539/src/Microsoft.AspNetCore.Mvc.Core/ModelBinding/Binders/CancellationTokenModelBinder.cs#L29
some background here..
/cc @Tratcher in case he wants to add more
The RequestAborted token is triggered server-side, by your code. It's intended to be used when the server wants to cancel pending work based on a timeout or some other criteria.
In old webapi, we used to provide a token that fired when the client disconnected, and this is what we model-bound by default. This led to the assumption that the right thing to do was to always use that token, and pass it into every API call that could accept it.
This led to a lot of exception noise in logs for cases that you really can't troubleshoot.
This also led to the wrong semantics in a lot of cases. Would you abort a database transaction if the client disconnects while it's processing?
This is why we bind RequestAborted and why aborts are now triggered in server-side code rather than by the client hanging up.
Use it in good health with this knowledge.
Thank you for background and clarification. Makes sense to me now.
Just to clarify. I understood that now CancellationToken is not triggered by a client. But is there any other way to be notified that the client aborted the connection? In my application this is a desired behavior to abandon any further server-side processing when a request is aborted.
/cc @Tratcher @davidfowl - where did we put the client disconnected token?
https://github.com/aspnet/HttpAbstractions/issues/197 ?
@rynowak RequestAborted is ClientDisconnected, we haven't made any effort to separate them. That said, client disconnects can be difficult to detect. If the client disconnects gracefully (FIN), Kestrel won't detect it unless you send multiple writes and the client sends a RST in response. If the client disconnects with a RST first Kestrel should detect that and fire RequestAborted. WebListener currently triggers RequestAborted for FIN or RST.
So is all the information that I've ever given out on this topic false then?
@Tratcher We should do the same in Kestrel (i.e. fire the RequestAborted token for both FIN and RST). It's affecting SignalR right now.
The order the RST comes in is irrelevant. If a RST is received with or without a prior FIN, RequestAborted will fire. RequestAborted will not fire with just a FIN from the client since the TCP connection is in a half-open CLOSE_WAIT state (i.e. it's still valid to write to the response). Maybe for HTTP we should treat CLOSE_WAIT as disconnected/aborted, but we should investigate to make sure this wouldn't break real clients.
Is there any update on this issue? This is really important especially when you are dealing with disaster recovery situations where the majority of the request should not be processed (due to client disconnection). We have a microservice archuiteture and this can actually prevent a "snowball" effect.
We are currently looking at changing the RequestAborted token for the next release. There's an open PR for this change at https://github.com/aspnet/KestrelHttpServer/pull/1218.
The RequestAborted token will still fire today given failed writes or a RST from the client. In some scenarios, it's possible to send keep alive messages to the client and check the RequestAborted token to more reliably ensure the connection is still alive.
This was fixed in Kestrel.
This definately used to work as I tried it when David Paquette wrote a blog post blog post about it. What release was this fixed in, I'm trying the following code with ASP.NET Core 1.1.1 and can't get this to work as expected.
public class Model { public class CancellationToken { get; set; }
[HttpGet("foo")]
public async Task<IActionResult> Foo(Model model, CancellationToken cancellationToken)
{
await Task.Delay(10000);
var a = cancellationToken.IsCancellationRequested;
var b = this.HttpContext.RequestAborted.IsCancellationRequested;
var c = model.CancellationToken.IsCancellationRequested;
return this.Ok(); // Set breakpoint here
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var request = $.get(
"http://localhost:5000/Bar/foo",
function(msg) {
alert("done");
});
setTimeout(function() {
request.abort();
alert('aborted');
},
1000);
</script>
@RehanSaeed - can you provide more information about what you mean by used to work? How do you expect the system to behave vs what is it doing?
"In MVC 6, uses RequestAborted which from the HttpContext which a CancellationToken so it does work as expected." David Paquette
However, my code above does not show true for this.HttpContext.RequestAborted.IsCancellationRequested using 1.1.1.
This only works in 2.0 not in 1.x
Hello, Please clarify: is it true that cancel a ajax call to a async action in ASP.NET Core is not possible in Version 1.x?
robert