Repository navigation
Description
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:
RunAsyncAction(IntervalAction/IntervalAction.cs:419-450) only swallows an OCE whencancellation.IsCancellationRequestedis true, which is correct. In every other case the OCE propagates out of theasync Taskmethod. Anasync Taskthat ends with anOperationCanceledExceptionfinishes Canceled, not Faulted.ActionTask = Task.Run(() => RunAsyncAction(...), CancellationToken.None)(line 387) unwraps that inner task, soActionTaskis Canceled andActionTask.Exceptionisnull.TryRun(lines 369-378) only rethrows whenActionTask.Exception is not null, so it treats the run as a success and clears the slot.RethrowExceptions(line ~504) only looks atActionTask is { IsFaulted: true }, so it reports nothing.- Under
IntervalType.FromLastCompletion, theRecordRun()after theawait(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:Or usecatch (OperationCanceledException ex) when (!cancellation.IsCancellationRequested) { throw new InvalidOperationException("AsyncAction was cancelled without Stop() being called.", ex); }
ExceptionDispatchInfoto rethrow the original exception wrapped so that the task faults. - Or treat
ActionTask.IsCanceledlikeIsFaultedin bothTryRunandRethrowExceptions, and keep the original exception so it can be rethrown.
Acceptance criteria
- A regression test in which
AsyncActionthrowsTaskCanceledExceptionwithoutStop()runs the action exactly once within a longActionInterval. - 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
matt-edmondson commentedon Oct 6, 2026
Triage
AsyncActionpath that shipped with Add AsyncAction so async work is awaited, not run as async void [minor] #78 (merged 2026-10-05). The most common async failure, anHttpClienttimeout, is never reported, and underFromLastCompletionan hourly job retries everyPollingIntervalagainst a service that is already struggling. The repro measured 41 runs in 500 ms instead of 1.IntervalAction/IntervalAction.cs:RunAsyncAction,TryRunandRethrowExceptions(the async action execution path).Actionpath, where the loop stops instead of hammering. Related issues:FromLastCompletion, a failed run doesn't re-arm the interval. Once this OCE faults the run, With FromLastCompletion, a failed run doesn't count toward the interval, so Restart() after a fault re-runs the action immediately however long ActionInterval is #72's behaviour decides when it runs again, so fix the two together or at least test them together.Recommended next step: Fault the run on an unrequested cancellation in
RunAsyncAction, the first option in the issue. It keepsTryRun/RethrowExceptionson their singleIsFaultedcheck. Then add the three regression tests from the acceptance criteria.Generated by Claude Code
matt-edmondson commentedon Nov 12, 2026
Triage
This looks fixed on
main(515bf38) already. I ran the issue's reproduction as a temporary MSTest againstmain: anAsyncActionthat throwsTaskCanceledException("http timeout"),ActionInterval= 1 h,PollingInterval= 10 ms,FromLastCompletion, 500 ms wait. It now behaves as expected:mainRethrowExceptions()TaskCanceledExceptionThe fix comes from
GetActionFailure, which treats a CanceledActionTaskas a failure and recovers itsOperationCanceledException.TryRunandRethrowExceptionsboth go through it. #97 covers the interval re-arm.RethrowExceptionsReportsAsyncActionThrowingTaskCanceledExceptioncovers the rethrow, andStopCancelsTheAsyncActionTokencovers the cancellation thatStop()requests. Nothing is left to change here, so this can be closed as completed.Generated by Claude Code