Skip to content

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

Closed
@alexey-zakharov

Description

@alexey-zakharov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-TypeSystem-coreclrin-prThere is an active PR which will close this issue when it is merged

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions