Commit 302a49a
Enhance collectible assembly handling in XmlSerializer (#127695)
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]](diffhunk://#diff-e4d354039ba8ec739e290407b136c6af32d4d38e07fa9420078f662590ea0da2L448-R488)
[[2]](diffhunk://#diff-e4d354039ba8ec739e290407b136c6af32d4d38e07fa9420078f662590ea0da2L705-R746)
[[3]](diffhunk://#diff-e4d354039ba8ec739e290407b136c6af32d4d38e07fa9420078f662590ea0da2L720-R776)
**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]](diffhunk://#diff-ddd028dcb1c03a409ec83ec7247b1c4793ebbee56e379a0dab16696fedf1503dL37-R38)
[[2]](diffhunk://#diff-ddd028dcb1c03a409ec83ec7247b1c4793ebbee56e379a0dab16696fedf1503dL50-R50)
**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]](diffhunk://#diff-cb25d8cca02e9c7959514c29f15823cfbdcae1b51cd148ddc69e2a8a6fb780e4R2623-R2646)
[[2]](diffhunk://#diff-cb25d8cca02e9c7959514c29f15823cfbdcae1b51cd148ddc69e2a8a6fb780e4R2823-R2883)
**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
---------
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>1 parent 65b7467 commit 302a49a
4 files changed
Lines changed: 254 additions & 14 deletions
File tree
- src/libraries/System.Private.Xml
- src/System/Xml/Serialization
- tests/XmlSerializer
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1276 | 1276 | | |
1277 | 1277 | | |
1278 | 1278 | | |
1279 | | - | |
| 1279 | + | |
1280 | 1280 | | |
1281 | 1281 | | |
1282 | 1282 | | |
1283 | 1283 | | |
1284 | | - | |
| 1284 | + | |
| 1285 | + | |
| 1286 | + | |
1285 | 1287 | | |
1286 | 1288 | | |
1287 | 1289 | | |
| |||
Lines changed: 62 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
433 | 434 | | |
434 | 435 | | |
435 | 436 | | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
436 | 463 | | |
437 | 464 | | |
438 | 465 | | |
| |||
445 | 472 | | |
446 | 473 | | |
447 | 474 | | |
448 | | - | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
449 | 494 | | |
450 | 495 | | |
451 | 496 | | |
452 | 497 | | |
453 | | - | |
| 498 | + | |
454 | 499 | | |
455 | | - | |
| 500 | + | |
456 | 501 | | |
457 | 502 | | |
458 | | - | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
459 | 506 | | |
460 | 507 | | |
461 | 508 | | |
| |||
702 | 749 | | |
703 | 750 | | |
704 | 751 | | |
705 | | - | |
| 752 | + | |
| 753 | + | |
706 | 754 | | |
707 | 755 | | |
708 | 756 | | |
| |||
717 | 765 | | |
718 | 766 | | |
719 | 767 | | |
720 | | - | |
721 | 768 | | |
722 | 769 | | |
723 | 770 | | |
724 | | - | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
| 774 | + | |
| 775 | + | |
| 776 | + | |
| 777 | + | |
725 | 778 | | |
726 | | - | |
| 779 | + | |
727 | 780 | | |
728 | 781 | | |
729 | 782 | | |
730 | | - | |
| 783 | + | |
731 | 784 | | |
732 | 785 | | |
733 | 786 | | |
| |||
Lines changed: 3 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
| 37 | + | |
| 38 | + | |
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
| 50 | + | |
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| |||
Lines changed: 185 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
| |||
2701 | 2702 | | |
2702 | 2703 | | |
2703 | 2704 | | |
| 2705 | + | |
| 2706 | + | |
| 2707 | + | |
| 2708 | + | |
| 2709 | + | |
| 2710 | + | |
| 2711 | + | |
| 2712 | + | |
| 2713 | + | |
| 2714 | + | |
| 2715 | + | |
| 2716 | + | |
| 2717 | + | |
| 2718 | + | |
| 2719 | + | |
| 2720 | + | |
| 2721 | + | |
| 2722 | + | |
| 2723 | + | |
| 2724 | + | |
| 2725 | + | |
| 2726 | + | |
| 2727 | + | |
| 2728 | + | |
| 2729 | + | |
| 2730 | + | |
| 2731 | + | |
| 2732 | + | |
| 2733 | + | |
| 2734 | + | |
| 2735 | + | |
| 2736 | + | |
| 2737 | + | |
| 2738 | + | |
| 2739 | + | |
| 2740 | + | |
| 2741 | + | |
| 2742 | + | |
| 2743 | + | |
| 2744 | + | |
2704 | 2745 | | |
2705 | 2746 | | |
2706 | 2747 | | |
| |||
2877 | 2918 | | |
2878 | 2919 | | |
2879 | 2920 | | |
| 2921 | + | |
| 2922 | + | |
| 2923 | + | |
| 2924 | + | |
| 2925 | + | |
| 2926 | + | |
| 2927 | + | |
| 2928 | + | |
| 2929 | + | |
| 2930 | + | |
| 2931 | + | |
| 2932 | + | |
| 2933 | + | |
| 2934 | + | |
| 2935 | + | |
| 2936 | + | |
| 2937 | + | |
| 2938 | + | |
| 2939 | + | |
| 2940 | + | |
| 2941 | + | |
| 2942 | + | |
| 2943 | + | |
| 2944 | + | |
| 2945 | + | |
| 2946 | + | |
| 2947 | + | |
| 2948 | + | |
| 2949 | + | |
| 2950 | + | |
| 2951 | + | |
| 2952 | + | |
| 2953 | + | |
| 2954 | + | |
| 2955 | + | |
| 2956 | + | |
| 2957 | + | |
| 2958 | + | |
| 2959 | + | |
| 2960 | + | |
| 2961 | + | |
| 2962 | + | |
| 2963 | + | |
| 2964 | + | |
| 2965 | + | |
| 2966 | + | |
| 2967 | + | |
| 2968 | + | |
| 2969 | + | |
| 2970 | + | |
| 2971 | + | |
| 2972 | + | |
| 2973 | + | |
| 2974 | + | |
| 2975 | + | |
| 2976 | + | |
| 2977 | + | |
| 2978 | + | |
| 2979 | + | |
| 2980 | + | |
| 2981 | + | |
| 2982 | + | |
| 2983 | + | |
| 2984 | + | |
| 2985 | + | |
| 2986 | + | |
| 2987 | + | |
| 2988 | + | |
| 2989 | + | |
| 2990 | + | |
| 2991 | + | |
| 2992 | + | |
| 2993 | + | |
| 2994 | + | |
| 2995 | + | |
| 2996 | + | |
| 2997 | + | |
| 2998 | + | |
| 2999 | + | |
| 3000 | + | |
| 3001 | + | |
| 3002 | + | |
| 3003 | + | |
| 3004 | + | |
| 3005 | + | |
| 3006 | + | |
| 3007 | + | |
| 3008 | + | |
| 3009 | + | |
| 3010 | + | |
| 3011 | + | |
| 3012 | + | |
| 3013 | + | |
| 3014 | + | |
| 3015 | + | |
| 3016 | + | |
| 3017 | + | |
| 3018 | + | |
| 3019 | + | |
| 3020 | + | |
| 3021 | + | |
| 3022 | + | |
| 3023 | + | |
| 3024 | + | |
| 3025 | + | |
| 3026 | + | |
| 3027 | + | |
| 3028 | + | |
| 3029 | + | |
| 3030 | + | |
| 3031 | + | |
| 3032 | + | |
| 3033 | + | |
| 3034 | + | |
| 3035 | + | |
| 3036 | + | |
| 3037 | + | |
| 3038 | + | |
| 3039 | + | |
| 3040 | + | |
| 3041 | + | |
| 3042 | + | |
| 3043 | + | |
| 3044 | + | |
| 3045 | + | |
| 3046 | + | |
| 3047 | + | |
| 3048 | + | |
| 3049 | + | |
| 3050 | + | |
| 3051 | + | |
| 3052 | + | |
| 3053 | + | |
| 3054 | + | |
| 3055 | + | |
| 3056 | + | |
| 3057 | + | |
| 3058 | + | |
| 3059 | + | |
| 3060 | + | |
| 3061 | + | |
| 3062 | + | |
| 3063 | + | |
| 3064 | + | |
2880 | 3065 | | |
2881 | 3066 | | |
2882 | 3067 | | |
| |||
0 commit comments