Skip to content

Add Python AsyncEventScope for same-thread notifications - #229

Merged
leileizhang (lei9444) merged 4 commits into
mainfrom
lei9444-python-async-event-scope
Oct 30, 2026
Merged

leileizhang (lei9444) merged 4 commits into
mainfrom
lei9444-python-async-event-scope

Conversation

@lei9444

Copy link
Copy Markdown
Contributor

Summary

Add public dynwinrt.AsyncEventScope, a small typed Python adapter over existing subscribe_* methods and asyncio.TaskGroup. No Rust ABI, codegen subscription behavior, JavaScript/Node APIs, or generated-package layout changes.

with RoApartment(), projected_lifetime_scope():
    properties = PropertySet()
    async with AsyncEventScope(loop=asyncio.get_running_loop()) as events:
        unsubscribe = events.subscribe(properties.subscribe_map_changed, on_changed)
        properties["accepted"] = None
        unsubscribe()  # Stops future notifications, not the accepted task.

on_changed may be an async function or a callable returning an awaitable. Normal exit removes subscriptions and drains accepted tasks. Handler/body errors and external cancellation use TaskGroup cleanup and propagation. Every unsubscribe is attempted on exit; native removal errors remain observable and saved unsubscribe closures can retry on the owner loop/thread before native lifetimes end.

Reproduction and example

Passing a bare async def to the existing properties.subscribe_map_changed(handler) creates an unawaited coroutine; it does not schedule the handler. Registering it through events.subscribe(...) owns and awaits its work without changing existing synchronous callbacks, raw delegates, or callbacks returning already TaskGroup-owned tasks.

With the new runtime installed, follow samples/python/async-events/README.md to generate stock Windows bindings, then run app.py and app.py --fail. The sample uses real PropertySet notifications and native async temporary-file I/O; it demonstrates unsubscribe-before-drain and handler-error propagation after finally cleanup.

Supported boundary

Only ordinary, deferrable-to-later notifications on the same OS thread and actively driven asyncio loop. The application owns the loop, apartment, and projection lifetimes. There is no COM marshaling, WinUI pump integration, synchronous return/Handled/Cancel/deferral support, ordering guarantee, or backpressure policy. Eager task factories can begin handlers inside the native callback; native reentrancy restrictions still apply.

Validation

  • Normally installed CPython 3.12 Windows x64 wheel: 972 Python tests passed; 14 optional WinUI fixture tests skipped. Focused scope/typing/package checks: 41 passed; existing CI stubtest passed without allowlist changes.
  • Generated Python E2E: 56/56 passed; interface implementation E2E: 4/4 passed. Native STA/MTA tests cover default/eager factories, accepted-task drain, errors/cancellation/finally, non-agile event-args thread guards, context/lifetime behavior, and legacy callback compatibility.
  • Strict mypy and Pyright validated actual generated event signatures: valid async/awaitable handlers accepted; wrong sender, args, arity, and synchronous result rejected.
  • Added scope tests and real notification checks to the existing CPython 3.11–3.14 wheel-consumer matrix. Those other Python versions and ARM64 were not run locally; CI results are separate from local validation.

Based directly on main; independent of #228.

Compose TaskGroup with existing event subscriptions, preserve typed handlers and native lifetimes, and cover installed wheels and real PropertySet notifications.

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

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ✅ Passed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 86.67% 82.15% 86.3% regions
Python aggregate 70.97% n/a 38.22% branches
Python runtime 98.28% n/a 95% branches
Generated Python WinRT projections 68.86% n/a 30.46% branches
Generated Python WinRT implementations 72% n/a 46.3% branches
JavaScript aggregate 18.38% 24.85% 55.94% branches
JavaScript runtime 41.73% 41.53% 78.33% 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

leileizhang (lei9444) and others added 3 commits October 30, 2026 08:38
Keep exact callback cardinality and native lifetime checks while accepting Windows path aliases. Exercise a dot-dot temp-root alias in every native run so canonicalization is covered even without NTFS short names.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Python 3.11 preserves dot segments in tempfile paths, unlike 3.12+, and StorageFolder can reject them. Exercise equivalent directory spelling through case instead; retain exact callback count and samefile validation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep incoming cancellation out of the cleanup exception group while retaining removal errors. Compare combined internal/external cancellation cases with the running Python version's TaskGroup, including native PropertySet delivery and retryable unsubscription.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lei9444
leileizhang (lei9444) merged commit aa3c43a into main Oct 30, 2026
33 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-python-async-event-scope branch October 30, 2026 02:28
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.

3 participants