Skip to content
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

LoaderAllocator selection logic for a generic type instance does not account for a LoaderAllocator of type itself #111611

Open
alexey-zakharov opened this issue Jan 20, 2025 · 4 comments · May be fixed by #111706
Labels
area-TypeSystem-coreclr in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner

Comments

@alexey-zakharov
Copy link
Contributor

alexey-zakharov commented Jan 20, 2025

Description

ClassLoader::ComputeLoaderModuleWorker uses a LoaderAllocator creation number to determine the latest LoaderAllocator to place a class in. However the method does not account the number of the type definition itself when calculating the latest number and this can lead into a situation when the generic instance is put into an older LoaderAllocator.

E.g. if we have type ClassA from AssemblyLoadContext A with number 2 and type ClassB from AssemblyLoadContext B with number 5 the resulting loader allocator for the type ClassB would correspond to AssemblyLoadContext A with creation number 2. If ClassB contains a static, such static would be then placed under the LoaderAllocator A, preventing AssemblyLoadContext B from unloading unless A is unloaded.

Reproduction Steps

  1. Create 2 classes in 2 different assemblies

Assembly A

public class ClassA
{
}

Assembly B

public class ClassB<T>
{
    public static T instance = new T();
}
  1. Load A.dll into a collectible AssemblyLoadContext A.
  2. Load B.dll into a collectible AssemblyLoadContext B.
  3. Instantiate type ClassB<ClassA>.
  4. Ensure instance is not referenced and unload AssemblyLoadContext B.

Expected behavior

AssemblyLoadContext B is unloaded successfully

Actual behavior

AssemblyLoadContext B is not unloaded

Regression?

No

Known Workarounds

No response

Configuration

No response

Other information

The following change seem to fix the issue - Unity-Technologies#279

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 20, 2025
Copy link
Contributor

Tagging subscribers to this area: @vitek-karas, @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

@alexey-zakharov alexey-zakharov changed the title LoaderAllocator selection logic for a generic type instance does not account for a LoaderAllocator of a type itself LoaderAllocator selection logic for a generic type instance does not account for a LoaderAllocator of type itself Jan 20, 2025
@jkotas
Copy link
Member

jkotas commented Jan 20, 2025

cc @davidwrighton

@davidwrighton
Copy link
Member

@alexey-zakharov this looks like a good idea. Would you like to open a PR with your change against the dotnet/runtime repo? I think it is a nice upgrade to the existing logic, and I would happily approve it.

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Jan 22, 2025
@alexey-zakharov
Copy link
Contributor Author

@alexey-zakharov this looks like a good idea. Would you like to open a PR with your change against the dotnet/runtime repo? I think it is a nice upgrade to the existing logic, and I would happily approve it.

awesome, thanks @davidwrighton! I'm happy to make a PR - this is it with small cleanups #111706

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-TypeSystem-coreclr in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

3 participants