#13917·MudBlazor

MudDialog: a reopened inline dialog is closed by the previous show's close callback

Author: thekameleonCreated Sep 26, 2026Updated Sep 26, 2026
Labelsbughas workaround

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:

csharp
// 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 Visible true, 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:

razor
@* 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):

csharp
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);
    }
}
  1. Show the inline dialog.
  2. In one dispatcher turn, set Visible false, then true again.
  3. Wait: the reopened dialog closes, and the parent's Open is 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:

csharp
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: CloseAsync already sets _reference = null straight after Close, so a late callback finds a different reference, or none, and does nothing.
  • Closing with the ✕ button or Escape: _reference is still the same reference, so VisibleChanged(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