Skip to content

fix(Toast): make close handling idempotent - #8775

Merged
ArgoZhang merged 3 commits into
mainfrom
fix-toast
Nov 17, 2026
Merged

ArgoZhang merged 3 commits into
mainfrom
fix-toast

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Link issues

fixes #8774

Summary By Copilot

  • Remove a Toast from container state before awaiting OnCloseAsync, so overlapping close calls do not invoke the callback repeatedly.
  • Guard Bootstrap hide/hidden events against repeated close processing, and restore the close guard when a hide event is canceled.
  • Add C# tests for closing without a callback and ignoring a concurrent close while the callback is pending.

Regression?

  • Yes
  • No

No known regression; the change is limited to Toast close lifecycle handling.

Risk

  • High
  • Medium
  • Low

The change coordinates asynchronous close callbacks with Bootstrap's toast hide events.

Verification

  • Manual (required)

  • Automated

  • dotnet test test/UnitTest/UnitTest.csproj --no-restore --filter "FullyQualifiedName~UnitTest.Components.ToastTest.Close_" — passed (3 tests).

  • node --check src\BootstrapBlazor\Components\Toast\Toast.razor.js — passed (syntax check only; no JavaScript behavior tests were run).

  • git diff --check origin/main...HEAD — passed.

  • Manual browser validation was not run.

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Ensure Toast close operations remove the Toast once and invoke asynchronous close handling only once.

Bug Fixes:

  • Make Toast closing idempotent so concurrent or repeated close requests invoke the close callback at most once.
  • Prevent duplicate Bootstrap Toast close processing while preserving normal behavior when a hide event is canceled.

Tests:

  • Add coverage for closing Toasts without callbacks and ignoring concurrent close requests.

@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Implementation

Remove a toast from container state before awaiting its close callback, making repeated container close calls a no-op.

  • src/BootstrapBlazor/Components/Toast/ToastContainer.razor.cs L85-90, L95 (removed lines)

Guard Bootstrap hide and hidden events against duplicate close processing, and clear the in-progress guard if hiding is canceled.

  • src/BootstrapBlazor/Components/Toast/Toast.razor.js L31-48 (+3 small edits)

Tests

Verify closing without a callback removes the toast and concurrent close calls invoke a pending callback only once.

  • test/UnitTest/Components/ToastTest.cs L259-309

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot added the bug Something isn't working label Nov 17, 2026
@bb-auto bb-auto Bot added this to the v11.1.0 milestone Nov 17, 2026

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@ArgoZhang
ArgoZhang merged commit 9409b12 into main Nov 17, 2026
4 checks passed
@ArgoZhang
ArgoZhang deleted the fix-toast branch November 17, 2026 12:41
@sourcery-ai

sourcery-ai Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Linked issues

Checked at commit f51f5eb

#8774 bug(Toast): prevent duplicate close callbacks · 2 of 2 met

# Requirement Status Notes
1 Remove each Toast once and run its close callback at most once. ✅ Met ToastContainer.Close removes the option before awaiting its callback, and the hidden event handler also guards against duplicate callback invocation.
2 Allow a later close attempt to succeed if a hide event is canceled. ✅ Met The hide handler clears its closing guard in a microtask when the event was prevented.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 2 issues

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/BootstrapBlazor/Components/Toast/ToastContainer.razor.cs" line_range="85-88" />
<code_context>
     /// <param name="option"></param>
     public async Task Close(ToastOption option)
     {
+        if (!Toasts.Remove(option))
+        {
+            return;
+        }
+
+        StateHasChanged();
</code_context>
<issue_to_address>
**Failed callback loses toast**

When `OnCloseAsync` throws after the option is removed, `Toasts.Remove(option)` and `StateHasChanged()` run before `OnCloseAsync`; when that callback throws, the toast is already gone, and later `Close` calls return without retrying the callback, leaving the caller with a dismissed toast and failed close handling.

Retain a per-option in-progress guard and restore or retain the toast if the callback fails, so duplicate calls are suppressed without losing retry behavior.
</issue_to_address>

### Comment 2
<location path="test/UnitTest/Components/ToastTest.cs" line_range="259-273" />
<code_context>
         Assert.True(close);
     }

+    [Fact]
+    public async Task Close_WithoutCallback_Ok()
+    {
+        var cut = Context.Render<ToastContainer>();
+        var service = Context.Services.GetRequiredService<ToastService>();
+        var option = new ToastOption();
+        await service.Show(option);
+
+        Assert.Single(cut.FindComponents<Toast>());
+
</code_context>
<issue_to_address>
**Unrequested close tests are added**

The patch adds two unit tests for a bug fix without an explicit request to add tests, contrary to the repository's default testing policy.

Remove the added unit tests unless their addition is explicitly requested.

Also at `test/UnitTest/Components/ToastTest.cs:274-308`.
</issue_to_address>

Sourcery assessment

Approval pending. 1 finding to address first.

Blocking findings: src/BootstrapBlazor/Components/Toast/ToastContainer.razor.cs:88


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment on lines +85 to +88
if (!Toasts.Remove(option))
{
return;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Failed callback loses toast

When OnCloseAsync throws after the option is removed, Toasts.Remove(option) and StateHasChanged() run before OnCloseAsync; when that callback throws, the toast is already gone, and later Close calls return without retrying the callback, leaving the caller with a dismissed toast and failed close handling.

Retain a per-option in-progress guard and restore or retain the toast if the callback fails, so duplicate calls are suppressed without losing retry behavior.

Prompt for AI agents
In `src/BootstrapBlazor/Components/Toast/ToastContainer.razor.cs` at lines 85-88:

**Failed callback loses toast**

When `OnCloseAsync` throws after the option is removed, `Toasts.Remove(option)` and `StateHasChanged()` run before `OnCloseAsync`; when that callback throws, the toast is already gone, and later `Close` calls return without retrying the callback, leaving the caller with a dismissed toast and failed close handling.

Retain a per-option in-progress guard and restore or retain the toast if the callback fails, so duplicate calls are suppressed without losing retry behavior.

Comment on lines +259 to +273
[Fact]
public async Task Close_WithoutCallback_Ok()
{
var cut = Context.Render<ToastContainer>();
var service = Context.Services.GetRequiredService<ToastService>();
var option = new ToastOption();
await service.Show(option);

Assert.Single(cut.FindComponents<Toast>());

await cut.InvokeAsync(() => cut.Instance.Close(option));

Assert.Empty(cut.FindComponents<Toast>());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚪ Low · Unrequested close tests are added

The patch adds two unit tests for a bug fix without an explicit request to add tests, contrary to the repository's default testing policy.

Remove the added unit tests unless their addition is explicitly requested.

Also at test/UnitTest/Components/ToastTest.cs:274-308.

Prompt for AI agents
In `test/UnitTest/Components/ToastTest.cs` at lines 259-273:

**Unrequested close tests are added**

The patch adds two unit tests for a bug fix without an explicit request to add tests, contrary to the repository's default testing policy.

Remove the added unit tests unless their addition is explicitly requested.

Also at `test/UnitTest/Components/ToastTest.cs:274-308`.

From .github/copilot-instructions.md L10:

普通功能开发、缺陷修复等任务,默认不新增、修改或运行单元测试,除非用户明确要求

@codecov

codecov Bot commented Nov 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e4384b2) to head (f51f5eb).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #8775   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          663       663           
  Lines        30362     30364    +2     
=========================================
+ Hits         30362     30364    +2     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(Toast): prevent duplicate close callbacks

1 participant