Skip to content

Add SemaphoreSlim Support for Popup CloseAsync to Avoid Race Conditions#3235

Merged
TheCodeTraveler merged 28 commits into
mainfrom
Ensure-Popup-Has-Closed-Before-`CloseAsync`-Returns
Jun 9, 2026
Merged

Add SemaphoreSlim Support for Popup CloseAsync to Avoid Race Conditions#3235
TheCodeTraveler merged 28 commits into
mainfrom
Ensure-Popup-Has-Closed-Before-`CloseAsync`-Returns

Conversation

@TheCodeTraveler

Copy link
Copy Markdown
Collaborator

Description of Change

This PR builds on the work started in #3234, expanding the scope of the SemaphoreSlim(1,1) to include both ShowAsync and CloseAsync.

I updated PopupPage, adding the method PopupPage.ShowAsync and moved the SemaphoreSlim(1,1) from PopupExtensions into PopupPage. This simplifies the logic in PopupExtensions and ensures that PopupPage handles the logic for both showing + closing Popups.

This PR also updates PopupPage.CloseAsync to subscribe to the Window.ModalPopped event and Page.NavigatedFrom event; PopupPage.CloseAsync now awaits the result of these MAUI Navigation events before invoking PopupClosed, guaranteeing we only invoke the events PopupPage.PopupClosed and Popup.Closed after MAUI has removed the page from the underlying ModalStack.

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard

Additional information

The existing Unit Tests covering CloseAsync all still pass indicating no breaking changes.

I also asked CoPilot to have unit tests covering every branch in the following classes which is why there are a few new unit tests added:

  • PopupExtensions
  • Popup
  • PopupResult
  • PopupOptions
  • IPopupOptions
  • DefaultPopupOptionsSettings
  • DefaultPopupSettings
  • PopupService
  • IPopupResult
  • PopupPage

Copilot AI 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.

Pull request overview

This PR refactors popup navigation coordination by moving the SemaphoreSlim(1,1) synchronization from PopupExtensions into PopupPage, adding PopupPage.ShowAsync(...) overloads, and extending serialized navigation to include CloseAsync flows. It also expands unit test coverage across popup primitives, options/defaults, the popup service, and navigation edge-cases.

Changes:

  • Added PopupPage.ShowAsync(...) overloads and centralized navigation serialization via a shared SemaphoreSlim.
  • Updated PopupPage.CloseAsync to wait for MAUI navigation signals (Window.ModalPopped / Page.NavigatedFrom) before raising popup-closed events.
  • Added/expanded unit tests across popup APIs (results/options/defaults/service/extensions) and added timeouts to reduce hangs.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs Centralizes semaphore-based navigation serialization in PopupPage and changes CloseAsync to await MAUI navigation confirmation signals.
src/CommunityToolkit.Maui/Extensions/PopupExtensions.shared.cs Simplifies extension methods to delegate navigation work to PopupPage.ShowAsync(...) (removes local semaphore).
src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupTests.cs Adds tests validating exception message guidance for popup exceptions.
src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupResultTests.cs Adds tests for PopupResult<T> behaviors and interface conformance.
src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupPageTests.cs Adds timeouts and new navigation-edge-case tests (custom navigation containers, query attribute forwarding, blocked scenarios).
src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupOptionsTests.cs Adds tests for PopupOptions.Empty defaults and IPopupOptions contract adherence.
src/CommunityToolkit.Maui.UnitTests/Views/Popup/DefaultPopupSettingsTests.cs Adds tests verifying defaults and override behavior for popup settings.
src/CommunityToolkit.Maui.UnitTests/Views/Popup/DefaultPopupOptionsSettingsTests.cs Adds tests verifying defaults and override behavior for popup options settings.
src/CommunityToolkit.Maui.UnitTests/Services/PopupServiceTests.cs Expands test coverage for Shell-based popup service APIs and cancellation/null-argument behaviors.
src/CommunityToolkit.Maui.UnitTests/Extensions/PopupExtensionsTests.cs Adds argument validation tests and a typed default-result test for closing without a typed result.

Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs Outdated
Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs Outdated
TheCodeTraveler and others added 3 commits June 6, 2026 12:08
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI 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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.

Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 10 out of 10 changed files in this pull request and generated 5 comments.

Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
Comment thread src/CommunityToolkit.Maui.UnitTests/Views/Popup/PopupResultTests.cs
Comment thread src/CommunityToolkit.Maui.UnitTests/Services/PopupServiceTests.cs Outdated
Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs Outdated
TheCodeTraveler and others added 2 commits June 6, 2026 12:39
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Comment thread src/CommunityToolkit.Maui/Views/Popup/PopupPage.shared.cs
@TheCodeTraveler
TheCodeTraveler merged commit 147b0d3 into main Jun 9, 2026
10 checks passed
@TheCodeTraveler
TheCodeTraveler deleted the Ensure-Popup-Has-Closed-Before-`CloseAsync`-Returns branch June 9, 2026 00:05
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 10, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants