MudDialog: a reopened inline dialog is closed by the previous show's close callback
Before reporting
- I searched existing issues
- I tried in private/incognito browser: not applicable. This is a server-side timing race, so it is reproduced below in bUnit rather than in a browser.
What went wrong?
If an inline MudDialog is closed and then reopened before MudBlazor has finished handling the close, the reopened dialog closes itself.
ShowAsync registers a close callback on each dialog reference it shows:
// MudDialog.razor.cs, ShowAsync
_reference.Result.ContinueWith(t =>
{
return InvokeAsync(() => _visibleState.SetValueAsync(false));
}).CatchAndLog();That callback runs on the thread pool, a moment after the dialog has closed. It does not check whether _reference is still the reference it was registered for. So when the dialog has been reopened in the meantime, the old callback:
- finds
Visibletrue, because the new show set it; - sets it to false and raises
VisibleChanged(false); - which makes the parent's binding hide the dialog that is now showing.
Expected: a dialog that has been reopened stays open.
Actual: it closes with no user action.
In a browser this needs the thread pool to be busy for longer than the gap between two clicks, so it is rare. In bUnit tests, where clicks are instant, it fails intermittently under load. We saw that with a test that cancels an inline editor dialog and then opens it for another item.
It is also easy to misread from the parent's side. A close caused by this late callback looks the same as a genuine ✕/Escape close, since both arrive as VisibleChanged(false). And ignoring the callback does not help, because _visibleState is left false while the parent keeps passing an unchanged Visible=true.
Reproduction link
No try.mudblazor.com snippet. The race depends on thread-pool timing that a snippet cannot control. The bUnit test below reproduces it every time: 40 of 40 runs on v9.10.0. A control test that leaves a gap between the close and the reopen kept the dialog open in all 40 runs.
Reproduction steps
Host component:
@* InlineDialogHost.razor *@
<MudDialog @bind-Visible="Open">
<DialogContent><p class="content">Hello</p></DialogContent>
</MudDialog>
@code {
public bool Open { get; set; }
public void Show() { Open = true; StateHasChanged(); }
public void Hide() { Open = false; StateHasChanged(); }
}Test (bUnit 2.11.3 and NUnit 4; it passes, which shows the bug):
public class InlineDialogReopenTests : BunitContext
{
[Test]
public async Task Previous_dialogs_close_callback_closes_the_reopened_dialog()
{
Services.AddMudServices();
JSInterop.Mode = JSRuntimeMode.Loose;
var provider = Render<MudDialogProvider>();
var host = Render<InlineDialogHost>();
await host.InvokeAsync(host.Instance.Show);
await provider.WaitForStateAsync(() => provider.FindAll("p.content").Count == 1);
// Close and reopen in one dispatcher turn. Closing completes the first
// reference's Result, which queues ShowAsync's Result.ContinueWith to the
// thread pool; that callback's InvokeAsync can only run after this turn.
await host.InvokeAsync(() =>
{
host.Instance.Hide();
host.Instance.Show();
});
Assert.That(provider.FindAll("p.content"), Has.Count.EqualTo(1), "the dialog reopened");
// Nothing else happens, yet the reopened dialog closes: the first
// dialog's callback sets Visible false on the instance now showing the second.
await provider.WaitForStateAsync(() => provider.FindAll("p.content").Count == 0);
Assert.That(host.Instance.Open, Is.False);
}
}- Show the inline dialog.
- In one dispatcher turn, set
Visiblefalse, then true again. - Wait: the reopened dialog closes, and the parent's
Openis false again.
Suggested fix
This is untested against MudBlazor's own suite. The idea is to let the callback act only while the reference it was registered for is still current:
var reference = _reference;
reference.Result.ContinueWith(_ => InvokeAsync(() =>
ReferenceEquals(_reference, reference) ? _visibleState.SetValueAsync(false) : Task.CompletedTask))
.CatchAndLog();This covers both ways a dialog closes:
- Closing through
Visible=false:CloseAsyncalready sets_reference = nullstraight afterClose, so a late callback finds a different reference, or none, and does nothing. - Closing with the ✕ button or Escape:
_referenceis still the same reference, soVisibleChanged(false)reaches the parent as it does today.
Related, all closed: #11789 (fixed by #11804, the !_reference.Result.IsCompleted check next to this code) and #10968 / #11026 (fixed by #11048, the _showLock). This callback predates both fixes and is unchanged on dev.
Workaround we use until then: give the inline MudDialog a fresh instance on each open with @key, and ignore a VisibleChanged(false) that comes from an earlier open's instance.
Version (bug)
v9.10.0
Last working version
Unknown. The callback has been there since at least v7.
Environment
Other: .NET 10, found in bUnit tests of a Blazor Server app.
Blazor rendering mode
Interactive Server
Source: MudBlazor/MudBlazor