Skip to content

Call GetResults for WinUI async actions on the owning JS thread - #237

Open
leileizhang (lei9444) wants to merge 1 commit into
mainfrom
lei9444-winui-async-action-results
Open

leileizhang (lei9444) wants to merge 1 commit into
mainfrom
lei9444-winui-async-action-results

Conversation

@lei9444

Copy link
Copy Markdown
Contributor

Summary

MediaCapture.InitializeWithSettingsAsync could finish successfully on a WinUI STA while its JS Promise rejected with RPC_E_WRONG_THREAD. The bridge dispatched Promise settlement to the UI thread, but had already called GetResults on a completion thread or an MTA worker. Calling GetResults on the same action on the originating thread succeeded.

For IAsyncAction and IAsyncActionWithProgress converted to Promises while the WinUI queue is registered, retain the action in the JS environment and enqueue only the completion notification. The settling callback calls the real, zero-argument GetResults on the owning thread, including when queue shutdown routes the callback through Node. Its HRESULT determines success or failure. Data-returning operations and actions without a registered WinUI queue keep their existing result collection path; no public JS API changes.

Reproduction

Prerequisites: Windows with Node.js and the Rust/MSVC build tools. For the before/after comparison, set $oldAddon to the absolute path of an unpatched .node file matching your Node architecture (for example, from 0.1.0-preview.24). The fixture does not require a camera or a Windows App SDK installation.

Steps:

  1. From this PR's checkout, run Set-Location .\bindings\js, then npm ci and npm run build.

  2. Run the same owner-thread action against both addons:

    $env:DYNWINRT_ASYNC_TRACE = '1'
    $env:DYNWINRT_TEST_RUNTIME = $oldAddon
    node .\__test__\dispatcher-shutdown-child.mjs
    Remove-Item Env:DYNWINRT_TEST_RUNTIME
    node .\__test__\dispatcher-shutdown-child.mjs

Actual behavior: The unpatched addon logs GetResults failed: Async operation failed: 0x8001010E; the action's owner-thread implementation is never entered, and the assertion fails.

After the fix: The test prints dispatcher-shutdown-ok. The already-completed action runs GetResults exactly once as the queue drains. An action that finishes after unregistering the queue still rejects with its actual 0x80004005 failure.

Validation

On Windows ARM64, npx --no-install ava __test__\index.spec.ts --match '*WinUI scheduled start*' passed with the generated WinUI Application and matching bootstrap DLL: the action settled before Application.Start returned. With only the native addon replaced, the Gallery initialized the real camera, displayed its preview, navigated away, and exited with no remaining active native objects or components.

Queue the completion notification, not the action. Keep the action in the environment registry until settlement or cleanup, including when queue shutdown falls back to Node. Data-returning and non-WinUI operations keep their existing result collection path.

Call the void GetResults slot without a dummy output argument. Add a device-free native owner-thread action covering scheduled WinUI startup, shutdown draining, and a late error after unregister.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ✅ Passed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 87.3% 83.1% 85.65% regions
Python aggregate 70.61% n/a 38.03% branches
Python runtime 98.29% n/a 95.06% branches
Generated Python WinRT projections 68.46% n/a 30.26% branches
Generated Python WinRT implementations 71.75% n/a 46.08% branches
JavaScript aggregate 18.38% 24.84% 55.92% branches
JavaScript runtime 41.6% 40.9% 77.68% branches
Generated WinRT projections 17.27% 19.36% 52.51% branches
Generated WinRT implementations 45.99% 59.19% 60.97% branches
Generated Classic COM projections 11.38% 23.88% 53.1% branches

View workflow run and download full HTML/LCOV/XML reports

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.

1 participant