Azure / Azure/azure-functions-host

[V1] OperationCanceledExceptions in user code causes Service Bus retry to not happen

Open
#3,318 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 10h
Merged PRs (30d)
36

Description

When an OperationCanceledException occurrs in **pre-compiled C# functions** triggered by a Service Bus Topic, the function is not retried. When other exceptions (ex. System.Exception) are thrown, the function is retried.

```
//case that function is retried
throw new System.Exception("foo");

//case that function isn't retried
throw new System.Threading.TasksTaskCanceledException("bar");
```

Here is the [relevant retry code](https://github.com/Azure/azure-webjobs-sdk/blob/34036368a5b0ebcdecf9dd7fdd494ac6664d8c49/src/Microsoft.Azure.WebJobs.ServiceBus/MessageProcessor.cs#L67-L85).

Note that this does not repro in C# script functions (.csx). This is because there's a bug somewhere that special-case handles OperationCanceledexceptions, and C# function exceptions come back wrapped as a `FunctionInvocationException`.

## Workaround ##
Try-catch all business logic, and then re-throw any exceptions in a custom wrapper function.
```
[FunctionName("HandledServiceBusFunction")]
public static void Run([ServiceBusTrigger("mytopi", "subscription", AccessRights.Manage, Connection = "ServiceBus")]string mySbMsg, TraceWriter log)
{
log.Info($"ServiceBus topic trigger function processed message: {mySbMsg}");
try
{
ThrowMyException();
}
catch (Exception e)
{
throw new CustomException("This is my custom exception.", e);
}
}

private static void ThrowMyException()
{
// Can be any exception
throw new OperationCanceledException("Task canceled.");
}
```

**Note:** Unverified, but may be an issue with v2 as well.

Contributor guide

Open the contributing guide

Research direction

Start with src/Microsoft.Azure.WebJobs.ServiceBus/MessageProcessor.cs at the linked retry logic and compare handling of OperationCanceledException with other exceptions. Reproduce the behavior in a pre-compiled C# Service Bus Topic function, contrasting it with the provided C# script case; done means cancellation exceptions trigger the same retry behavior without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, csharp
Domain
backend, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.