TaskCanceledException causes the job to be Processing state indefinitely

As I understand it, Hangfire does not support async methods yet. As a workaround, I wrapped my async method calls with AsyncContext.Run() from AsyncEx to make it appear to be synchronous from Hangfire point of view. Exception seems to be bubbled up correctly as expected (unwrapped from AggregateException).

    public void Task()
        AsyncContext.Run(() => TaskAsync());

    private async Task TaskAsync()

However, when TaskAsync throws TaskCanceledException, Hangfire does not correctly mark it as “Failed”. Instead it will try to process the job again. If TaskAsync keeps on throwing TaskCanceledException, it will be stuck in that state indefinitely instead of stop retrying after 10 times as usual.

It seems to be because Hangfire treats OperationCanceledException as its own control flow, instead of treating it as an exception originating from the job. e.g. here, and here.

I know it’s a stretch, but can Hangfire not use its own custom exception internally rather than making use of .NET OperationCanceledException?

Hi @AdityaSantoso, thank you for the report! And sorry for the long delay. This issue is fixed in Hangfire 1.4.7, that is just released.

UPD. Forgot to mention that I’ve simply added a special case for TaskCanceledException class, non-workaround implementation for OperationCanceledException will be available in 1.6.0.