Skip to content

Support Python WinRT operations in asyncio tasks - #130

Merged
leileizhang (lei9444) merged 4 commits into
mainfrom
lei9444-async-create-task
Sep 3, 2026
Merged

leileizhang (lei9444) merged 4 commits into
mainfrom
lei9444-async-create-task

Conversation

@lei9444

@lei9444 leileizhang (lei9444) commented Sep 2, 2026 •

Copy link
Copy Markdown
Contributor

Closes #126

Summary

  • make generated Python WinRT async operations implement the full coroutine driving protocol so asyncio.create_task() and TaskGroup.create_task() accept them directly
  • preserve the existing structural WinRTAsync[T] and WinRTAsyncWithProgress[T, P] protocols for backward compatibility with mocks and adapters
  • expose dedicated WinRTCoroutine[T] and WinRTCoroutineWithProgress[T, P] return protocols that combine the legacy WinRT operation surface with collections.abc.Coroutine
  • annotate generated async methods with the dedicated coroutine return protocols and export them from the runtime and stubs
  • register only the private _DynWinRTAsync implementations with collections.abc.Coroutine at runtime
  • retain the existing shared completion future for repeatable direct awaits, ensure_future(), wait_for(), result conversion, failures, and cancellation
  • use a separate native-style one-shot task driver with explicit ownership, reentrancy, completion, throw(), and close() states
  • preserve task cancellation propagation to IAsyncInfo.Cancel, WinRT cancellation as asyncio.CancelledError, progress callbacks, blocking wait() safeguards, and idempotent release()
  • deactivate progress callback captures on terminal task execution, cancellation, close(), and release() so late WinRT notifications cannot retain observers or target a closed event loop

Design

asyncio.Task drives accepted objects through send() and throw() rather than __await__(). The runtime therefore delegates those methods to a real Python bridge coroutine while keeping __await__() backed by the existing cached asyncio future. This preserves repeatable direct awaits without pretending the task-driving coroutine itself is reusable. Concurrent or repeated task-driving attempts fail deterministically with native-coroutine-style errors.

The lifecycle state also keeps completed failures cached, lets an owning task drain cancellation after release(), validates legacy throw() arguments before creating a bridge, cancels pending WinRT work on close(), and releases progress callback captures after task completion or cancellation.

Progress dispatch is owned by the projected operation. The native WinRT delegate keeps only a weak dispatcher reference, while queued loop callbacks reference a mutable dispatch slot rather than the observer directly. Terminal execution clears that slot under the GIL, making already-queued and later native notifications harmless no-ops; dispatch also checks for a closed loop before scheduling. The cached cancelled future remains intact so a direct await after task cancellation still observes asyncio.CancelledError.

The public compatibility protocols remain structural and unchanged. Generated methods use separate coroutine-capable return protocols, allowing strict mypy to accept their results as both legacy WinRT operations and Coroutine[Any, Any, T] values without forcing existing adapters to implement send(), throw(), or close().

Rebase

Rebased the three-commit series onto 54a3912 (origin/main), preserving the copied IBuffer conversions from #129 and explicit Python unbox_object APIs from #131.

Rewritten commits: 5661fd3, d0a3b9c, 9778588.

Validation

  • coverage-profile stress reproduction of TestHttpProgress::test_struct_progress_survives_asyncio_dispatch: 20/20 passed with Rust LLVM coverage instrumentation and Python coverage tracing
  • full Python binding suite under coverage tracing: 145 passed
  • mypy.stubtest: success, 2 modules
  • cargo fmt --all -- --check
  • cargo test -p dynwinrt-py --lib: 5 passed
  • cargo test -p dynwinrt-codegen: 564 passed across 30 suites
  • .\tests\e2e\e2e_test.ps1 -SkipBuild -Lang py: strict mypy success; 42 E2E checks passed, 0 failed

@github-actions

github-actions Bot commented Sep 2, 2026 •

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ✅ Passed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 82.91% 79.39% 82.67% regions
Generated Python projections 70.5% n/a 21.71% branches
JavaScript aggregate 21.28% 20.6% 54.04% branches
JavaScript runtime 20.14% 14.28% 7.14% branches
Generated WinRT projections 25.8% 19.3% 54.74% branches
Generated Classic COM projections 14.64% 24.46% 53.75% branches

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

Implement the coroutine driving protocol for generated WinRT async operations while preserving repeatable direct awaits, cancellation, progress, blocking safeguards, and release semantics.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Handle throw injection before asyncio owner resolution, close the coroutine protocol synchronously, and preserve WinRT cancellation cleanup failures.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep the existing WinRTAsync contracts structurally compatible while exposing dedicated coroutine return protocols for generated asyncio task inputs.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Deactivate native and queued progress dispatch captures when coroutine execution finishes, is cancelled, or is released so late WinRT callbacks cannot retain observers or target a closed event loop.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lei9444
leileizhang (lei9444) merged commit ce007bd into main Sep 3, 2026
24 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-async-create-task branch September 3, 2026 08:18
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.

Make Python WinRT async operations compatible with asyncio.create_task

2 participants