Skip to content

Collectible-ALC static caches pin the context after unload (WASM batch) #23706

Description

@nickrandolph

Summary

When a host loads apps into their own collectible AssemblyLoadContexts (ALCs) and unloads them, a set of process-lifetime static caches in Uno keep strong references to the unloaded context's Assembly / Type / delegate objects. These pins prevent the collectible ALC from ever being collected after AssemblyLoadContext.Unload(), leaking the full previous-app object graph on every reload.

On WebAssembly this is especially acute: AssemblyLoadContext.Unloading is never raised, so there is no per-context callback a cache could subscribe to for self-cleanup. The only teardown seam is the host-driven cleanup hook (Application.CleanupNonDefaultAlcCaches, called from Window.CloseAlcWindow). Any static cache that outlives an app and is keyed by (or transitively references) that app's Assembly/Type/delegate must therefore be swept from that hook, or expose its own ALC-scoped removal.

This issue tracks a batch of additional pins found by rooting a disposed ALC's LoaderAllocator and following genuine managed roots (strong handle / static / stack) into it — distinct from the benign runtime residual.

Pins in scope (managed sweeps)

  1. ResourceLoader — the static _lookupAssemblies list (and _parsedResources markers) hold every app assembly registered via AddLookupAssembly for the process lifetime.
  2. UIElementNativeRegistrar._classNames (WASM) — Dictionary<Type,int> keyed by every element type ever created, including app control types. Invisible to a desktop-only ClrMD guard.
  3. WindowId-keyed maps — AppWindow, ApplicationView, CoreDragDropManager register per-window instances with no removal path, retaining the closed window's implementation graph (and its event subscribers) for a secondary-app window.
  4. CompositionTarget.Rendering (WASM) — the static handler list retains any secondary-ALC subscriber that did not unsubscribe before unload.
  5. Hot-reload client status history — each retained operation holds a Type[] of the hot-reloaded (collectible) types; the history grows unbounded.
  6. PagePool (WASM) — an orphaned pool plus an eternal 30s scavenger loop, with Type-keyed pooled instances.
  7. Residual Type-keyed statics — HtmlElementHelper._cache (WASM), FeatureConfiguration.Style.UseUWPDefaultStylesOverride.

Fix approach

Match the existing sweep pattern (Style.ClearCachesForNonDefaultAlc / DependencyProperty…RemoveNonDefaultAlcEntries): each cache gets an ALC-scoped removal wired into the cleanup hook, plus targeted self-detach / rekeying where a hook call is not reachable. Each sweep is guarded by a red/green test that asserts a collectible-ALC-keyed entry is dropped while default-ALC / framework entries are kept.

Out of scope (follow-ups)

Hot-reload delta pipeline retention; image Blob URL lifetime; FontFamilyLoader caches.

Activity

  1. added 2 commits that reference this issue on Jul 23, 2026
    55f258f
    9f95f42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions