Skip to content

An AsyncAction that throws OperationCanceledException (e.g. an HttpClient timeout) is never reported and re-runs on every polling tick, ignoring ActionInterval #81

Open

Description

@matt-edmondson

What's wrong

When an AsyncAction fails with an OperationCanceledException that Stop() did not request (for example a TaskCanceledException from an HttpClient timeout), nothing reports the failure. The action is then started again on every polling tick instead of once per ActionInterval.

The path, at 603fe8c:

  1. RunAsyncAction (IntervalAction/IntervalAction.cs:419-450) only swallows an OCE when cancellation.IsCancellationRequested is true, which is correct. In every other case the OCE propagates out of the async Task method. An async Task that ends with an OperationCanceledException finishes Canceled, not Faulted.
  2. ActionTask = Task.Run(() => RunAsyncAction(...), CancellationToken.None) (line 387) unwraps that inner task, so ActionTask is Canceled and ActionTask.Exception is null.
  3. TryRun (lines 369-378) only rethrows when ActionTask.Exception is not null, so it treats the run as a success and clears the slot.
  4. RethrowExceptions (line ~504) only looks at ActionTask is { IsFaulted: true }, so it reports nothing.
  5. Under IntervalType.FromLastCompletion, the RecordRun() after the await (line ~440) was skipped by the exception, so the interval is never re-armed. The next poll finds the action due and starts it again.

This is a different path from #66. That issue covers the synchronous Action, which Task.Run(Action) turns into a Faulted task. Its rethrow in the loop cancels PollingTask, so polling stops. The AsyncAction path came with #78 and fails the other way: it keeps polling and hammers the action.

Reproduction

This was run as a temporary MSTest against main (603fe8c):

int runs = 0;
IntervalAction ia = IntervalAction.Start(new()
{
    AsyncAction = async ct => { Interlocked.Increment(ref runs); await Task.Yield(); throw new TaskCanceledException("http timeout"); },
    ActionInterval = TimeSpan.FromHours(1),
    PollingInterval = TimeSpan.FromMilliseconds(10),
    IntervalType = IntervalType.FromLastCompletion,
});
await Task.Delay(500);
ia.Stop();
ia.RethrowExceptions();
Expected Actual
Runs 1 41
RethrowExceptions() throws the TaskCanceledException returns normally
ActionTask.Status Canceled

Why it matters

The method's own comment says only a cancellation Stop() asked for is "not a failure to report". In practice, the most common transient failure of an async HTTP poll is reported to nobody. It also turns an hourly job into one that calls an already-timing-out service every PollingInterval, which here is 100× per second.

Suggested fix

Either approach works:

  • Make an unrequested cancellation fault the run. In RunAsyncAction, after the existing filtered catch, add:
    catch (OperationCanceledException ex) when (!cancellation.IsCancellationRequested)
    {
        throw new InvalidOperationException("AsyncAction was cancelled without Stop() being called.", ex);
    }
    Or use ExceptionDispatchInfo to rethrow the original exception wrapped so that the task faults.
  • Or treat ActionTask.IsCanceled like IsFaulted in both TryRun and RethrowExceptions, and keep the original exception so it can be rethrown.

Acceptance criteria

  • A regression test in which AsyncAction throws TaskCanceledException without Stop() runs the action exactly once within a long ActionInterval.
  • In that test, RethrowExceptions() throws the original exception, wrapped or not.
  • An OCE thrown after Stop() cancels the token is still swallowed, as it is today.

Activity

  1. added
    bug
    on Oct 6, 2026
  2. matt-edmondson commented on Oct 6, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Recommended next step: Fault the run on an unrequested cancellation in RunAsyncAction, the first option in the issue. It keeps TryRun/RethrowExceptions on their single IsFaulted check. Then add the three regression tests from the acceptance criteria.


    Generated by Claude Code

  3. matt-edmondson commented on Nov 12, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    This looks fixed on main (515bf38) already. I ran the issue's reproduction as a temporary MSTest against main: an AsyncAction that throws TaskCanceledException("http timeout"), ActionInterval = 1 h, PollingInterval = 10 ms, FromLastCompletion, 500 ms wait. It now behaves as expected:

    Expected Now on main
    Runs 1 1
    RethrowExceptions() throws the TaskCanceledException throws it

    The fix comes from GetActionFailure, which treats a Canceled ActionTask as a failure and recovers its OperationCanceledException. TryRun and RethrowExceptions both go through it. #97 covers the interval re-arm. RethrowExceptionsReportsAsyncActionThrowingTaskCanceledException covers the rethrow, and StopCancelsTheAsyncActionToken covers the cancellation that Stop() requests. Nothing is left to change here, so this can be closed as completed.


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions