-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[release/6.0] 1388 xml serializer assembly load context awareness (#6…
…1266) * Generate dynamic serialization assembly in the appropriate ALC, and don't keep any hard refs to types that could prevent unloading. * Add small test for ALC unloading. * PR feedback; Move into TempAssembly; Strong/Weak lookup tables; Test refinement * Refining TempAssembly cache; Reflection-based fix; Test corrections * Mono/wasm test fixup * Ensure that serializers from XmlMappings don't run afoul of collectible ALCs. * PR feedback * Unloadable contexts not yet supported in Mono. * Fix trimming of types for XmlSerializer tests that got moved out of main test assembly. * Encapsulating the duality of dealing with unloadable ALC instead of rewriting similar code everywhere. * Missed new file in last commit. * Compilation bug not seen locally. * Perf improvement. * Remove extraneous line. Test feedback. * Fix unloadability (#58948) There is a bug in AppDomain::FindAssembly code path that iterates over the domain assemblies. The iteration loop leaves the refcount of the LoaderAllocator related to the returned DomainAssembly bumped, but nothing ever decrements it. So when a code that needs to be unloaded ends up in that code path, all the managed things like managed LoaderAllocator, LoaderAllocatorScout are destroyed, but the unloading doesn't complete due to the refcount. We have never found it before as this code path is never executed in any of the coreclr tests even with unloadability testing option. (cherry picked from commit 8b38c19) Co-authored-by: Steve Molloy <[email protected]> Co-authored-by: Jan Vorlicek <[email protected]>
- Loading branch information
1 parent
3f6d8fa
commit 04a5836
Showing
13 changed files
with
418 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
309 changes: 183 additions & 126 deletions
309
src/libraries/System.Private.Xml/src/System/Xml/Serialization/Compilation.cs
Large diffs are not rendered by default.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
src/libraries/System.Private.Xml/src/System/Xml/Serialization/ContextAwareTables.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.Xml.Serialization | ||
{ | ||
using System; | ||
using System.Collections; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.Loader; | ||
|
||
internal class ContextAwareTables<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T> where T : class? | ||
{ | ||
private Hashtable _defaultTable; | ||
private ConditionalWeakTable<Type, T> _collectibleTable; | ||
|
||
public ContextAwareTables() | ||
{ | ||
_defaultTable = new Hashtable(); | ||
_collectibleTable = new ConditionalWeakTable<Type, T>(); | ||
} | ||
|
||
internal T GetOrCreateValue(Type t, Func<T> f) | ||
{ | ||
// The fast and most common default case | ||
T? ret = (T?)_defaultTable[t]; | ||
if (ret != null) | ||
return ret; | ||
|
||
// Common case for collectible contexts | ||
if (_collectibleTable.TryGetValue(t, out ret)) | ||
return ret; | ||
|
||
// Not found. Do the slower work of creating the value in the correct collection. | ||
AssemblyLoadContext? alc = AssemblyLoadContext.GetLoadContext(t.Assembly); | ||
|
||
// Null and non-collectible load contexts use the default table | ||
if (alc == null || !alc.IsCollectible) | ||
{ | ||
lock (_defaultTable) | ||
{ | ||
if ((ret = (T?)_defaultTable[t]) == null) | ||
{ | ||
ret = f(); | ||
_defaultTable[t] = ret; | ||
} | ||
} | ||
} | ||
|
||
// Collectible load contexts should use the ConditionalWeakTable so they can be unloaded | ||
else | ||
{ | ||
lock (_collectibleTable) | ||
{ | ||
if (!_collectibleTable.TryGetValue(t, out ret)) | ||
{ | ||
ret = f(); | ||
_collectibleTable.AddOrUpdate(t, ret); | ||
} | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.