Skip to content

fix(a11y): marshal UseAnnounce.Announce to the UI thread (#130) - #614

Merged
Alexandre Zollinger Chohfi (azchohfi) merged 2 commits into
mainfrom
azchohfi-marshal-useannounce-ui-thread
Jun 22, 2026
Merged

Alexandre Zollinger Chohfi (azchohfi) merged 2 commits into
mainfrom
azchohfi-marshal-useannounce-ui-thread

Conversation

@azchohfi

Copy link
Copy Markdown
Collaborator

Problem

AnnounceHandle.Announce(message, assertive) calls WinUI XAML APIs — FrameworkElementAutomationPeer.FromElement, RaiseNotificationEvent, and TextBlock.Text — all of which throw RPC_E_WRONG_THREAD (0x8001010E) when invoked off the UI thread.

This is a footgun for the obvious calling pattern: announcing when a long-running operation finishes from a Task.Run continuation. The failure mode is an unobserved Task exception, which surfaces to the user as "the feature does nothing" with no actionable diagnostic.

Button("Trigger", () => Task.Run(() =>
{
    Thread.Sleep(100);
    announce.Announce("Done!");  // threw COMException 0x8001010E
}));

Fix

  • Capture the DispatcherQueue in SetTextBlock, which runs on the UI thread during reconciler mount — no chicken-and-egg.
  • In Announce, marshal the work via DispatcherQueue.TryEnqueue when called off-thread; the UI-thread fast path is a single HasThreadAccess bool check.
  • Extract the existing announce body into a private AnnounceCore(message, assertive).

This mirrors how Observable<T> handles cross-thread subscribers.

Tests

  • Added selftest fixture CovBoost_UseAnnounceCrossThread that mounts a component, calls Announce from a Task.Run continuation, and asserts no exception is thrown (before the fix this threw synchronously on the background thread).
  • dotnet build Reactor.slnx succeeds.
  • dotnet test tests/Reactor.Tests — 9431 passed, 0 failed.
  • Selftests CovBoost_UseAnnounce* all pass.

Closes #130

AnnounceHandle.Announce called WinUI XAML APIs (FrameworkElementAutomationPeer.FromElement, RaiseNotificationEvent, TextBlock.Text) that throw RPC_E_WRONG_THREAD (0x8001010E) when invoked off the UI thread. Announcing from a Task.Run continuation silently ate the caller's Task.

Capture the DispatcherQueue in SetTextBlock (runs on the UI thread during reconciler mount) and, in Announce, marshal via TryEnqueue when called cross-thread. The UI-thread fast path is a single HasThreadAccess bool check. Existing announce body extracted into AnnounceCore.

Adds a selftest fixture that announces from a background thread and asserts no crash.

Closes #130

Co-authored-by: Copilot <[email protected]>

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 fixes a cross-thread footgun in UseAnnounce by marshalling AnnounceHandle.Announce(...) onto the UI thread (via a captured DispatcherQueue) to avoid WinUI RPC_E_WRONG_THREAD failures, and adds a selftest to validate the behavior.

Changes:

  • Capture the UI DispatcherQueue during live-region mount and marshal announcement work when called off-thread.
  • Extract the announce body into AnnounceCore(...) and add a UI-thread fast path (HasThreadAccess check).
  • Add/register a new selftest fixture that calls Announce(...) from a Task.Run continuation to assert it no longer throws synchronously.

Reviewed changes

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

File Description
src/Reactor/Hooks/UseAnnounce.cs Captures DispatcherQueue and marshals Announce() back to the UI thread; refactors announce logic into AnnounceCore.
tests/Reactor.AppTests.Host/SelfTest/Fixtures/CoverageBoostFixtures.cs Adds a cross-thread selftest fixture exercising UseAnnounce from a background thread.
tests/Reactor.AppTests.Host/SelfTest/SelfTestFixtureRegistry.cs Registers the new CovBoost_UseAnnounceCrossThread fixture in discovery + factory switch.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Reactor/Hooks/UseAnnounce.cs Resolved
Comment thread src/Reactor/Hooks/UseAnnounce.cs Resolved
Apply PR #614 review findings:

- M1: gate Announce readiness on the captured DispatcherQueue (not _textBlock) and mark both fields volatile, so an off-thread caller can never observe a wired text block with a stale/null dispatcher and run AnnounceCore off-thread.

- M2: cross-thread selftest now proves delivery on the UI thread via a FIFO sentinel enqueued after the off-thread Announce.

- M3: UseAnnounce fast-path fixture renders announce.Region so AnnounceCore's UI-thread path is exercised.

- M4: regenerate docs/guide/focus-and-input-internals.md from the updated announce-live-region snippet.

- L1: document the Announce threading contract in XML docs.

- L2: add CHANGELOG ### Fixed entry for #130.

Co-authored-by: Copilot <[email protected]>
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) merged commit b693a28 into main Jun 22, 2026
19 checks passed
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) deleted the azchohfi-marshal-useannounce-ui-thread branch June 22, 2026 17:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UseAnnounce.Announce should marshal to the UI thread automatically

2 participants