Skip to content

fix(win32): Fix UIA structure event crash and subtree provider leak - #26012

Merged
agneszitte merged 2 commits into
release/stable/6.7from
dev/xygu/20261103/win32-uia-cleanup-6.7
Nov 4, 2026
Merged

agneszitte merged 2 commits into
release/stable/6.7from
dev/xygu/20261103/win32-uia-cleanup-6.7

Conversation

@Xiaoy312

@Xiaoy312 Xiaoy312 commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

GitHub Issue: related to unoplatform/kahua-private#564

PR Type:

🐞 Bugfix

What changed? 🚀

This is a follow-up to #25943, which backported only the owner release from #23722 to release/stable/6.7. The client reports that the leak remains in their app, and that removing elements can crash. This PR brings their two patches, with the cleanup-lookup correction from #25985 folded into the second.

1. UiaRaiseStructureChangedEvent signature (crash). The P/Invoke marshalled the runtime ID as a SAFEARRAY and omitted the length argument. The native signature is (IRawElementProviderSimple*, StructureChangeType, int* pRuntimeId, int cRuntimeIdLen). When a UIA client subscribes to structure changes, a ChildRemoved raised with a runtime ID can crash the process with an access violation (0xC0000005). The try/catch around the call can't catch it. The P/Invoke now passes an int[] and its length. This is the same fix as #25985 on master.

2. Every provider of a removed subtree is invalidated (leak). These parts come from #23722:

  • CleanupProviders now resolves each element's provider with TryGetExistingProviderForElement. That lookup also finds providers registered only by their peer, which the walk used to skip (e.g. realized ListViewItems, or an element whose peer belongs to another element).
  • UiaDisconnectProvider calls back into the provider it disconnects, including Navigate(Parent). While a cleanup runs, lookups only return providers that already exist (peer key first, then the owner's element key when its provider represents the same peer), and nothing is created or registered. Before, a provider was created for the parent that had just been cleaned and was never invalidated, and UIA's reference to it kept the removed tree alive.

The client's patch blocked every lookup during cleanup. With #25985's correction, a parent that is still connected keeps resolving during the callback.

The client measured the leak in their app with a UIA client subscribed to StructureChanged: 18 of 18 closed record views were retained on 6.8.0-dev.311, through [COM/WinRT Objects] → Win32RawElementProvider → view. With 0002, none are retained through a provider and the heap levels off.

Adds Given_Win32ProviderLifetime from #25985, minus two tests:

Validation

Runtime (Skia Win32, SamplesApp.Skia.Generic net10.0, local), Given_Win32* filter:

Build Result
This PR 13/13 passed (Given_Win32Accessibility + Given_Win32ProviderLifetime)
Client's 0002 as sent (lookups blocked during cleanup) When_Cleanup_Reenters_Live_Provider_Lookup fails: a connected sibling resolves to null
Signature fix only, without 0002 When_Removed_Element_Uses_Another_Elements_Peer_Then_Provider_Is_Invalidated fails: the provider keyed by the peer's owner stays connected

The fail-before rows were run on servicing/6.8; Win32Accessibility.cs and Win32RawElementProvider.cs are identical on both lines after this change, and the 13/13 run was repeated on release/stable/6.7.

Not covered by an automated test:

  • The signature fix. It needs an out-of-process UIA client; master covers it with Win32UiaEventProbe in CI.
  • The parent-recreation leak. On 6.x, a provider created during cleanup only stays alive while a real UIA client holds it, so an in-process test can't observe it. The client's measurement above covers it.

PR Checklist ✅

🤖 Generated with Claude Code

jbourque-kahua and others added 2 commits November 3, 2026 19:23
The native function is declared in UIAutomationCoreApi.h as
UiaRaiseStructureChangedEvent(IRawElementProviderSimple*, StructureChangeType,
int* pRuntimeId, int cRuntimeIdLen). The P/Invoke declared three parameters
and marshalled the runtime id as a SAFEARRAY, so UIA read the SAFEARRAY
header as the id array and took its length from an uninitialized register.

Once a UIA client subscribes to StructureChanged events, a ChildRemoved
raised with a runtime id intermittently crashes the process with an access
violation in FlushStructureChanges -> UiaRaiseStructureChangedEvent. The
try/catch around the call cannot catch it.

Pass the runtime id as an int array with its length.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Backport of the provider-cleanup part of #23722 (master), complementing the
owner release already backported to 6.x, with the cleanup-lookup correction
from #25985.

While a UIA client subscribes to StructureChanged events, providers of a
removed subtree could stay connected and never be invalidated, so UIA's COM
reference kept each provider, and through its owner the removed element tree,
alive:

- CleanupProviders walks the removed subtree top-down. Invalidating a
  provider calls UiaDisconnectProvider, during which UIA asks for its
  parent; the parent was cleaned a moment earlier, so a new provider was
  created for it and never invalidated. While a cleanup runs
  (_providerCleanupDepth), lookups now only return providers that already
  exist (peer key, then the owner's element key when it represents the
  same peer) and never create or register one. Lookups are not blocked
  outright: a connected parent must still resolve during the callback.
- Providers registered only by automation peer (e.g. realized
  ListViewItems) are not in the element-keyed table, so the cleanup walk
  skipped them. CleanupProviders now resolves providers with
  TryGetExistingProviderForElement.

Measured in an app that swaps record views in and out of a peer-bearing
container, with a UIA client subscribed to StructureChanged (Subtree):
18 of 18 closed record views were retained on 6.8.0-dev.311, with the root
path [COM/WinRT Objects] -> Win32RawElementProvider -> view. With this change,
none are retained through a provider and the heap levels off.

Adds the Given_Win32ProviderLifetime runtime tests from #25985, without the
native-client test that needs the out-of-process probe.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

@mergify

mergify Bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again.

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.

5 participants