Skip to content

Each RethrowExceptions() call makes the stored exception's StackTrace longer: after 1,000 calls it is 2,009 lines (~280 KB) #105

Closed

Description

@matt-edmondson

What's wrong

When an action fails, the failure is stored once, on PollingTask or ActionTask. After that, every RethrowExceptions() call rethrows the same exception object with ExceptionDispatchInfo.Capture(exception).Throw() (IntervalAction/IntervalAction.cs:825; TryRun does the same at line 585).

Capture snapshots the exception's current trace, and that trace already includes every earlier rethrow. Each call therefore adds another --- End of stack trace from previous location --- section and a RethrowExceptions frame. The exception object is the same every time, so the trace keeps growing.

This is a side effect of the fix for #58 (PR #62), which added the EDI rethrow to keep the action's frames.

Repro (at 27c4ffa)

using ktsu.IntervalAction;
Exception? last = null;
var ia = IntervalAction.Start(new() { Action = () => throw new InvalidOperationException("boom"),
    PollingInterval = TimeSpan.FromMilliseconds(10), ActionInterval = TimeSpan.FromSeconds(10) });
await Task.Delay(300);
for (int i = 1; i <= 1000; i++)
{
    try { ia.RethrowExceptions(); }
    catch (Exception e)
    {
        if (i is 1 or 2 or 10 or 1000)
            Console.WriteLine($"call {i}: same={ReferenceEquals(e, last ?? e)} len={e.StackTrace?.Length} lines={e.StackTrace?.Split('\n').Length}");
        last = e;
    }
}
call 1: same=True len=1443 lines=11
call 2: same=True len=1723 lines=13
call 10: same=True len=3963 lines=29
call 1000: same=True len=281163 lines=2009

Expected: the trace is the same length on every call.

Why it matters

Calling RethrowExceptions() periodically, from a health check, watchdog or UI tick, is how a caller finds out the action failed. Once it has failed, each poll adds about 280 bytes to a string the instance keeps alive, and each call costs more than the last.

At one check per second, a day is about 86,400 calls. That makes a trace of roughly 24 MB, and every logged copy of it repeats the same frames tens of thousands of times.

Suggested fix

  • Capture one ExceptionDispatchInfo when the failure is first observed, store it, and call .Throw() on that stored instance each time.
  • Clear the stored instance wherever the failure is cleared today (the restart path).
  • Make the same change in TryRun (line 585) if it can rethrow the same failure more than once.

Acceptance criteria

Activity

  1. added
    bug
    on Nov 9, 2026
  2. matt-edmondson commented on Nov 9, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

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

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions