# TaskCanceledException causes the job to be Processing state indefinitely

**URL:** <https://discuss.hangfire.io/t/taskcanceledexception-causes-the-job-to-be-processing-state-indefinitely/1331>\
**Category:** bug?\
**Created:** [September 18, 2015, 1:55am UTC](https://discuss.hangfire.io/t/taskcanceledexception-causes-the-job-to-be-processing-state-indefinitely/1331 "2015-09-18T01:55:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdityaSantoso](https://avatars.discourse-cdn.com/v4/letter/a/5f8ce5/32.png) [@AdityaSantoso](https://discuss.hangfire.io/u/AdityaSantoso)\
**Post date:** [September 18, 2015, 1:55am UTC](https://discuss.hangfire.io/t/taskcanceledexception-causes-the-job-to-be-processing-state-indefinitely/1331/1 "2015-09-18T01:55:09Z")

</div>

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](https://github.com/StephenCleary/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.

 ![](https://europe1.discourse-cdn.com/flex017/uploads/hangfire/original/1X/6468d1922758a60c22d9821e81242bc5d7d3d16d.JPG)

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](https://github.com/HangfireIO/Hangfire/blob/5a770aab402b820f262f0872b18c2d3304bb7128/src/Hangfire.Core/Server/Worker.cs), and [here](https://github.com/HangfireIO/Hangfire/blob/5a770aab402b820f262f0872b18c2d3304bb7128/src/Hangfire.Core/Server/DefaultJobPerformanceProcess.cs).

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

---

<div class="post-metadata">

**Author:** ![odinserj](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/odinserj/32/648_2.png) [@odinserj](https://discuss.hangfire.io/u/odinserj)\
**Post date:** [October 1, 2015, 12:45pm UTC](https://discuss.hangfire.io/t/taskcanceledexception-causes-the-job-to-be-processing-state-indefinitely/1331/2 "2015-10-01T12:45:38Z")

</div>

Hi @AdityaSantoso, thank you for the report! And sorry for the long delay. This issue is fixed in [Hangfire 1.4.7](http://hangfire.io/blog/2015/10/01/hangfire-1.4.7.html), 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.
