Skip to content

Enhance collectible assembly handling in XmlSerializer#127695

Open
StephenMolloy wants to merge 4 commits intodotnet:mainfrom
StephenMolloy:100518_XmlSerializer-ALCs-and-collectible-types
Open

Enhance collectible assembly handling in XmlSerializer#127695
StephenMolloy wants to merge 4 commits intodotnet:mainfrom
StephenMolloy:100518_XmlSerializer-ALCs-and-collectible-types

Conversation

@StephenMolloy
Copy link
Copy Markdown
Member

@StephenMolloy StephenMolloy commented May 4, 2026

This pull request improves the handling of collectible assemblies and types within the XML serialization infrastructure, particularly when dealing with generic containers (like List<T> or arrays) that may contain collectible types. The changes ensure that serializers and related caches correctly associate with the appropriate collectible AssemblyLoadContext (ALC), preventing memory leaks and enabling proper unloading of collectible assemblies. Additionally, new tests have been added to verify these scenarios.

Improvements to collectible assembly handling:

  • Added the FindCollectibleAssembly helper method in Compilation.cs to recursively locate the correct collectible assembly for a given type, including generic arguments and element types. This ensures that serializer generation and caching are tied to the correct ALC, especially for generic or array types whose main assembly may not be collectible.
  • Updated serializer generation (GenerateRefEmitAssembly), cache lookup (TempAssemblyCache.GetTempAssembly), and cache insertion (TempAssemblyCache.Add) logic to use the collectible assembly found via FindCollectibleAssembly, rather than relying solely on t.Assembly. This change ensures that collectible types within generic containers are handled correctly and can be unloaded. [1] [2] [3]

Improvements to context-aware tables:

  • Adjusted logic in ContextAwareTables.GetOrCreateValue to use the default table only for non-collectible types and non-collectible load contexts, and to use a ConditionalWeakTable for collectible types or collectible load contexts, improving resource management for collectible scenarios. [1] [2]

Testing enhancements:

  • Added new tests (Xml_CollectibleTypeInGenericContainer) to verify that collectible types inside generic containers (like List<T> or arrays) are correctly handled and can be unloaded, preventing memory leaks. This includes a helper method ExecuteCollectibleContainerAndUnload that exercises these scenarios. [1] [2]

Minor changes:

  • Added a missing using System.Diagnostics; directive to support new assertions.

These changes collectively improve the reliability and correctness of XML serialization in scenarios involving collectible assemblies and types, especially when used in generic containers.

Fixes #100518

…mbly when entering the ALC for the generated serializer, fixing cases like `List<T>` where the outer type may live in the default ALC but a type argument is collectible. Also switch ContextAwareTables to use the collectible table whenever Type.IsCollectible is true, not only when the type's own ALC is collectible.
…yCache

- Expose FindCollectibleAssembly method to determine the correct collectible assembly for types.
- Update caching logic to utilize the collectible assembly for any collectible type, ensuring proper cache entry release when the collectible ALC is unloaded.
Copilot AI review requested due to automatic review settings May 4, 2026 05:59
@StephenMolloy StephenMolloy self-assigned this May 4, 2026
@StephenMolloy StephenMolloy added this to the 11.0.0 milestone May 4, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves XmlSerializer behavior for collectible types embedded in non-collectible container types (e.g., List<T> or T[] where T comes from a collectible AssemblyLoadContext). It ensures serializer generation and caching are associated with the correct collectible context/assembly, avoiding cache roots that would otherwise prevent unloading.

Changes:

  • Added a helper to locate an appropriate collectible assembly by walking generic arguments / element types, and used it to pick the contextual reflection scope for serializer generation.
  • Updated TempAssemblyCache lookups and inserts to key collectible entries off the discovered collectible assembly instead of t.Assembly (important for container types whose defining assembly is in the default ALC).
  • Updated ContextAwareTables to use the weak table for collectible types even when their AssemblyLoadContext is non-collectible (again, container-type scenario), and added coverage tests for List<T> / arrays.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
src/libraries/System.Private.Xml/tests/XmlSerializer/XmlSerializerTests.cs Adds test coverage for collectible types inside List<> / array containers and verifies unload behavior.
src/libraries/System.Private.Xml/src/System/Xml/Serialization/ContextAwareTables.cs Routes collectible types (not just collectible ALCs) into the weak-table-backed storage to avoid rooting collectible Type instances.
src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compilation.cs Introduces FindCollectibleAssembly and uses it to select the contextual reflection ALC and to key collectible temp-assembly caches appropriately.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 4, 2026 06:37
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XmlSerializer does not work with collections in collectible ALC

2 participants