Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ All notable changes to **bUnit** will be documented in this file. The project ad

## [Unreleased]

### Fixed

- `DisposeComponentsAsync()` could dispose nothing when the renderer's dispatcher was busy, e.g. with a timer calling `InvokeAsync`. Reported by [@xfunc-tim-schneider](https://github.com/xfunc-tim-schneider) in #1939. Fixed by [@linkdotnet](https://github.com/linkdotnet).

## [2.11.3] - 2026-09-13

### Fixed
Expand Down
41 changes: 12 additions & 29 deletions src/bunit/Rendering/BunitRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,27 +281,21 @@ public IReadOnlyList<IRenderedComponent<TComponent>> FindComponents<TComponent>(
public Task DisposeComponents()
{
ObjectDisposedException.ThrowIf(disposed, this);
Task? returnTask;

lock (renderTreeUpdateLock)
return Dispatcher.InvokeAsync(async () =>
{
returnTask = Dispatcher.InvokeAsync(async () =>
{
ResetUnhandledException();

foreach (var root in rootComponents)
{
root.Detach();
}

await Task.WhenAll(disposalTasks).ConfigureAwait(false);
disposalTasks.Clear();
});
ResetUnhandledException();

var roots = rootComponents.ToArray();
rootComponents.Clear();
}

return returnTask;
foreach (var root in roots)
{
root.Detach();
}

await Task.WhenAll(disposalTasks.ToArray()).ConfigureAwait(false);
});
}

/// <inheritdoc/>
Expand Down Expand Up @@ -425,21 +419,11 @@ protected override void AddPendingTask(ComponentState? componentState, Task task
if (componentState is null)
{
ArgumentNullException.ThrowIfNull(task);
AddDisposalTaskToQueue();
disposalTasks.RemoveAll(static t => t.IsCompleted);
disposalTasks.Add(task);
}

base.AddPendingTask(componentState, task);

void AddDisposalTaskToQueue()
{
var t = task;
t = task.ContinueWith(_ =>
{
disposalTasks.Remove(t);
}, TaskScheduler.Current);

disposalTasks.Add(t);
}
}

internal Task SetDirectParametersAsync<TComponent>(IRenderedComponent<TComponent> renderedComponent, ParameterView parameters)
Expand Down Expand Up @@ -602,7 +586,6 @@ protected override void Dispose(bool disposing)
if (disposing)
{
returnedRenderedComponentIds.Clear();
disposalTasks.Clear();
unhandledExceptionTsc.TrySetCanceled();
}

Expand Down
22 changes: 22 additions & 0 deletions tests/bunit.tests/BunitContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,28 @@ public async Task Test104()
callStack.Count.ShouldBe(1);
}

[Fact(DisplayName = "DisposeComponents disposes components while the dispatcher is busy")]
public async Task Test105()
{
var callStack = new List<string>();
Render<ChildDispose>(ps => ps.Add(p => p.CallStack, callStack));
using var entered = new ManualResetEventSlim();
using var release = new ManualResetEventSlim();
var blocker = Task.Run(() => Renderer.Dispatcher.InvokeAsync(() =>
{
entered.Set();
release.Wait();
}), Xunit.TestContext.Current.CancellationToken);
entered.Wait(Xunit.TestContext.Current.CancellationToken);

var disposeTask = DisposeComponentsAsync();
release.Set();
await disposeTask;
await blocker;

callStack.Count.ShouldBe(1);
}

[Fact(DisplayName = "The test service provider should register a placeholder HttpClient which throws exceptions")]
public void Test024()
{
Expand Down
Loading