-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Use AssemblyLoadContext-aware caches in TypeDescriptor to support unloading of assemblies cached by TypeDescriptor #114619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ericstj
merged 7 commits into
dotnet:main
from
Unity-Technologies:dotnet-upstream/fix-typedescription-alc-leak
Sep 4, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ef43616
Use separate hashtables for collectible types to support assembly un…
alexey-zakharov 93dfcf1
Addressed codereview feedback
alexey-zakharov 7467087
Added a test for custom provider and updated WeakHashTable to use Con…
alexey-zakharov 43e50c6
Renamed ContextAwareHashtable to CollectibleKeyHashtable
alexey-zakharov edfac0d
Merge branch 'main' into dotnet-upstream/fix-typedescription-alc-leak
alexey-zakharov 18e1aad
Merge branch 'main' into dotnet-upstream/fix-typedescription-alc-leak
alexey-zakharov 0022999
Merge branch 'main' into dotnet-upstream/fix-typedescription-alc-leak
alexey-zakharov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
138 changes: 138 additions & 0 deletions
138
...mponentModel.TypeConverter/src/System/ComponentModel/CollectibleKeyConcurrentHashtable.cs
This file contains hidden or 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,138 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.ComponentModel | ||
{ | ||
/// <summary> | ||
/// Concurrent dictionary that maps MemberInfo object key to an object. | ||
/// Uses ConditionalWeakTable for the collectible keys (if MemberInfo.IsCollectible is true) and | ||
/// ConcurrentDictionary for non-collectible keys. | ||
/// </summary> | ||
internal sealed class CollectibleKeyConcurrentHashtable<TKey, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TValue> : IEnumerable<KeyValuePair<TKey, TValue>> | ||
where TKey : MemberInfo | ||
where TValue : class? | ||
{ | ||
private readonly ConcurrentDictionary<TKey, TValue> _defaultTable = new ConcurrentDictionary<TKey, TValue>(); | ||
private readonly ConditionalWeakTable<TKey, object?> _collectibleTable = new ConditionalWeakTable<TKey, object?>(); | ||
|
||
public TValue? this[TKey key] | ||
{ | ||
get | ||
{ | ||
return TryGetValue(key, out TValue? value) ? value : default; | ||
} | ||
|
||
set | ||
{ | ||
if (!key.IsCollectible) | ||
{ | ||
_defaultTable[key] = value!; | ||
} | ||
else | ||
{ | ||
_collectibleTable.AddOrUpdate(key, value); | ||
} | ||
} | ||
} | ||
|
||
public bool ContainsKey(TKey key) | ||
{ | ||
return !key.IsCollectible ? _defaultTable.ContainsKey(key) : _collectibleTable.TryGetValue(key, out _); | ||
} | ||
|
||
public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) | ||
{ | ||
if (!key.IsCollectible) | ||
return _defaultTable.TryGetValue(key, out value); | ||
|
||
if (_collectibleTable.TryGetValue(key, out object? valueObj) && valueObj != null) | ||
{ | ||
value = (TValue)valueObj; | ||
return true; | ||
} | ||
|
||
value = default; | ||
return false; | ||
} | ||
|
||
public bool TryAdd(TKey key, TValue value) | ||
{ | ||
return !key.IsCollectible | ||
? _defaultTable.TryAdd(key, value) | ||
: _collectibleTable.TryAdd(key, value); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
_defaultTable.Clear(); | ||
_collectibleTable.Clear(); | ||
} | ||
|
||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => | ||
new Enumerator(_defaultTable.GetEnumerator(), ((IEnumerable<KeyValuePair<TKey, object?>>)_collectibleTable).GetEnumerator()); | ||
|
||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); | ||
|
||
private sealed class Enumerator : IEnumerator<KeyValuePair<TKey, TValue>> | ||
{ | ||
private readonly IEnumerator<KeyValuePair<TKey, TValue>> _defaultEnumerator; | ||
private readonly IEnumerator<KeyValuePair<TKey, object?>> _collectibleEnumerator; | ||
private bool _enumeratingCollectibleEnumerator; | ||
|
||
public Enumerator(IEnumerator<KeyValuePair<TKey, TValue>> defaultEnumerator, IEnumerator<KeyValuePair<TKey, object?>> collectibleEnumerator) | ||
{ | ||
_defaultEnumerator = defaultEnumerator; | ||
_collectibleEnumerator = collectibleEnumerator; | ||
_enumeratingCollectibleEnumerator = false; | ||
} | ||
|
||
public KeyValuePair<TKey, TValue> Current { get; private set; } | ||
|
||
object IEnumerator.Current => Current; | ||
|
||
public void Dispose() | ||
{ | ||
_defaultEnumerator.Dispose(); | ||
_collectibleEnumerator.Dispose(); | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
if (!_enumeratingCollectibleEnumerator && _defaultEnumerator.MoveNext()) | ||
{ | ||
Current = _defaultEnumerator.Current; | ||
return true; | ||
} | ||
|
||
_enumeratingCollectibleEnumerator = true; | ||
|
||
while (_collectibleEnumerator.MoveNext()) | ||
{ | ||
if (_collectibleEnumerator.Current.Value is TValue value) | ||
{ | ||
Current = new KeyValuePair<TKey, TValue>(_collectibleEnumerator.Current.Key, value); | ||
return true; | ||
} | ||
} | ||
|
||
Current = default; | ||
return false; | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_defaultEnumerator.Reset(); | ||
_collectibleEnumerator.Reset(); | ||
_enumeratingCollectibleEnumerator = false; | ||
Current = default; | ||
} | ||
} | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
.../System.ComponentModel.TypeConverter/src/System/ComponentModel/CollectibleKeyHashtable.cs
This file contains hidden or 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,43 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace System.ComponentModel | ||
{ | ||
/// <summary> | ||
/// Hashtable that maps a <see cref="MemberInfo"/> object key to an associated value. | ||
/// <para> | ||
/// For keys where <see cref="MemberInfo.IsCollectible"/> is <c>false</c>, a standard <see cref="Hashtable"/> is used. | ||
/// For keys where <see cref="MemberInfo.IsCollectible"/> is <c>true</c>, a <see cref="ConditionalWeakTable{TKey, TValue}"/> is used. | ||
/// This ensures that collectible <see cref="MemberInfo"/> instances (such as those from collectible assemblies) do not prevent their assemblies from being unloaded. | ||
/// </para> | ||
/// </summary> | ||
internal sealed class CollectibleKeyHashtable | ||
{ | ||
private readonly Hashtable _defaultTable = new Hashtable(); | ||
private readonly ConditionalWeakTable<object, object?> _collectibleTable = new ConditionalWeakTable<object, object?>(); | ||
|
||
public object? this[MemberInfo key] | ||
{ | ||
get | ||
{ | ||
return !key.IsCollectible ? _defaultTable[key] : (_collectibleTable.TryGetValue(key, out object? value) ? value : null); | ||
} | ||
|
||
set | ||
{ | ||
if (!key.IsCollectible) | ||
{ | ||
_defaultTable[key] = value; | ||
} | ||
else | ||
{ | ||
_collectibleTable.AddOrUpdate(key, value); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.