1
votes

I have a Service Bus Trigger Azure function, which is triggered every time a topic receives a message. Messages arrive at regular intervals, for example every 30 minutes. Between lots, no activity.

The function does nothing special, it does an asynchronous posting of the message via HttpClient. The function is regularly stopped with a TaskCanceledException.

The HttpClient is static

public static class SampleEventTrigger
{
    private static DefaultHttpWebHook webHook = new DefaultHttpWebHook(new Uri("https://nonexistent.invalid/sampleWebHook"), "/event/sampleEvent");

    [FunctionName("SampleEventTrigger")]
    public static async Task Run(
        [ServiceBusTrigger("sampleevent", "SampleEvent.Subs", AccessRights.Manage, Connection = GlobalConfiguration.ServiceBusConnection)]BrokeredMessage message,
        TraceWriter log)
    {
        log.Info("launch sample event subscription");

        try
        {
            var resp = await webHook.Post(message, log);
            log.Info($"{resp.StatusCode}, {resp.ReasonPhrase}");                
        }
        catch (Exception ex)
        {
            log.Error($"exception in webhook: {ex.Message}", ex);
            throw;
        }
    }
}

If I raise it again just after, this time it passes. Where does this exception come from? How do we avoid that? Is it related to a timeout, or to launching the function that would be too slow?

My function is in Consumption mode.

1
Why are you awaiting on Task.CompletedTask by the way? You can remove that line :) - juunas
sure, it's just a quick copy and paste code to demonstrate the problem - volia17
Can you provide the following information? 1) The region your function app is deployed in, 2) function app name (you can share privately), and 3) timeframe within which this happened (in UTC for preference.) - Katy Shimizu
Hello, Id=a27b04cd-7ce4-4a11-b8ed-e145fc9eabae region is West Europe, timestamp : 2018-07-18T09:30:00.193Z - volia17
I get a similar TaskCancelledException with an empty inner exception. Though in my case, I'm using a TimerTrigger. I think this is related to the method being async. - Ehtesh Choudhury

1 Answers

0
votes

Chances are that your Http call is timing out. Awaited Http calls that time out throw TaskCanceledException . I'm not sure what your DefaultHttpWebHook class does under the covers, but it should be using PostAsync in the Post method (which itself should have the Async suffix).

To verify you could catch TaskCanceledException and examine the inner exception. If you are still struggling, convert your code to non-async during local development to get a better handle on what's happening - it'll give you back a true exception rather than bubbling it up as a TCE.