5

When I cancel my request from browser to a HTTP Trigger it does not cancel and continues execution when hosted on Azure.

My function example:

[FunctionName("Test")]
public async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
    CancellationToken cancellationToken,
    ILogger log)
{
    var allCancellationTokens = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, req.HttpContext.RequestAborted);

    await Task.Delay(30000, allCancellationTokens.Token);

    if (allCancellationTokens.IsCancellationRequested) {
        log.LogInformation("Request was cancelled!");
        return new StatusCodeResult(499);
    }

    return new OkObjectResult("Complete");
}

I have been testing the cancellation through Postman and axios cancel tokens. It works when I run the project locally and cancel it, but does not seem to cancel once it's published to azure.

My expected result would be that it throws an OperationCanceledException if I cancel the request during the await Task.Delay(30000, allCancellationTokens.Token); However when checking logs on Azure Functions, just seems to carry on execution and complete the function.

Steps to reproduce:

  1. Create a HTTP Trigger like the one defined above.
  2. Publish the app to Azure.
  3. Open the functions logs on Azure in the monitor section.
  4. Using Postman, send a request to this function and cancel the request (within the timeout period of 30 seconds).
  5. Won't cancel the request and will still execute.
James
  • 63
  • 3
  • 7
  • Have you tried to perform the following? (This is just for experimenting) Instead of having a single 30 seconds delay, have six 5 seconds delay. Between each delay call the `allCancellationTokens.Token.ThrowIfCancellationRequested`. Does that cancel your function? – Peter Csala Mar 17 '21 at 08:18
  • Which version of Azure Functions are you running on? – Sebastian Achatz Mar 17 '21 at 17:12
  • 1
    @PeterCsala I made the following changes: https://gist.github.com/Jamess-Lucass/533ec5524d82d02886a5be67a46d00b0 and it appears to still execute after cancelling the request – James Mar 17 '21 at 19:57
  • @SebastianAchatz I am using Azure Functions v3 – James Mar 17 '21 at 19:58
  • @James I have a v3 function running here using the 'request.HttpContext.RequestAborted' and it works fine for me. But I do not incorporate the host cancellation via the injected CT and doing CancellationTokenSource.CreateLinkedTokenSource. Can you try to check whether this is working for you? – Sebastian Achatz Mar 17 '21 at 20:03
  • @SebastianAchatz I switched all the tokens over to "req.HttpContext.RequestAborted" and still not luck. Still cancels the request locally but not on azure. I'm on consumption plan if that's anything, not too sure what other differences there would be – James Mar 17 '21 at 23:12

1 Answers1

1

CancellationToken is helpful for implementing graceful shutdown of your Functions runtime host when it is notified by the Operating System that something has gone wrong to terminate it, please see the following answer for more: https://stackoverflow.com/a/63439515/528779

Mike Urnun MSFT
  • 463
  • 3
  • 4