Skip to content

fix: stop the WaitFor timer when the first check passes before it is … - #1956

Merged
linkdotnet merged 1 commit into
bUnit-dev:mainfrom
xaviergxf:fix/waitfor-timer-leak
Oct 25, 2026
Merged

linkdotnet merged 1 commit into
bUnit-dev:mainfrom
xaviergxf:fix/waitfor-timer-leak

Conversation

@xaviergxf

@xaviergxf xaviergxf commented Oct 23, 2026 •

Copy link
Copy Markdown

There is a memory leak that this PR fixes.

@xaviergxf
xaviergxf force-pushed the fix/waitfor-timer-leak branch 3 times, most recently from b93370f to f80e973 Compare October 23, 2026 14:36
…started

When a WaitFor* check passes synchronously, the helper disposes itself from
inside the constructor, while the timer field is still null. The constructor
then looks at WaitTask.IsCompleted to decide whether to start the timer, but
WaitTask is a WhenAny(...).Unwrap() that completes on the thread pool, so it
usually still reads as not completed at that point and the timer is started
anyway. The later Dispose() from the using block returns early because the
helper is already disposed, and nothing ever stops the timer.

The timer's state is the helper, so until the timeout elapses it roots the
helper, the rendered component, the renderer and everything the renderer
holds. With bUnit's default timeout of one second that goes unnoticed. With a
long DefaultWaitTimeout, a test suite keeps every page it rendered in memory
for its whole run.

The constructor now also checks isDisposed before starting the timer, and
disposes the timer right after creating it if the check disposed the helper in
between.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@linkdotnet
linkdotnet force-pushed the fix/waitfor-timer-leak branch from f80e973 to 353126e Compare October 25, 2026 15:57
@linkdotnet

Copy link
Copy Markdown
Collaborator

Absolustely valid and much appreciated. I took the liberty of rebasing your branch to the current main as we had other things in there. Also renaming one test to reflect the numbering.

That said, let's get that in and make a new release!

@linkdotnet
linkdotnet merged commit 165eb12 into bUnit-dev:main Oct 25, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants