Skip to content

Fix collectible AssemblyLoadContext shutdown race - #35131

Merged
jkotas merged 1 commit into
dotnet:masterfrom
janvorli:fix-assemblyloadcontext-shutdown-race
Apr 18, 2020
Merged

jkotas merged 1 commit into
dotnet:masterfrom
janvorli:fix-assemblyloadcontext-shutdown-race

Conversation

@janvorli

Copy link
Copy Markdown
Member

When the last reference to a collectible AssemblyLoadContext goes away,
its finalizer is called on the finalizer thread. It ends up calling the
native CLRPrivBinderAssemblyLoadContext::PrepareForLoadContextRelease
method which replaces a long weak GC handle to the managed
AssemblyLoadContext stored in the CLRPrivBinderAssemblyLoadContext by a
strong GC handle to the same AssemblyLoadContext and closes the original
weak handle.
The problem is that another thread may have read the weak handle pointer
and then try to use it to call into the resolving methods on the managed
AssemblyLoadContext. If that thread tries to resolve the related handle
after the finalizer thread has closed it, it fails as the handle doesn't
exist anymore. It could even get a completely different object if the
handle got reused in between. Or even worse, if the handle got reused by
another AssemblyLoadContext, the handle would resolve, but to the
different AssemblyLoadContext.

This change fixes the problem by keeping the weak handle open and
closing it after the AssemblyLoadContext shutdown completes (there are
no more assemblies that were loaded into that context and no instances
of types from those assemblies). The strong handle now serves only to
keep the managed AssemblyLoadContext alive.

Close #34838

When the last reference to a collectible AssemblyLoadContext goes away,
its finalizer is called on the finalizer thread. It ends up calling the
native CLRPrivBinderAssemblyLoadContext::PrepareForLoadContextRelease
method which replaces a long weak GC handle to the managed
AssemblyLoadContext stored in the CLRPrivBinderAssemblyLoadContext by a
strong GC handle to the same AssemblyLoadContext and closes the original
weak handle.
The problem is that another thread may have read the weak handle pointer
and then try to use it to call into the resolving methods on the managed
AssemblyLoadContext. If that thread tries to resolve the related handle
after the finalizer thread has closed it, it fails as the handle doesn't
exist anymore. It could even get a completely different object if the
handle got reused in between. Or even worse, if the handle got reused by
another AssemblyLoadContext, the handle would resolve, but to the
different AssemblyLoadContext.

This change fixes the problem by keeping the weak handle open and
closing it after the AssemblyLoadContext shutdown completes (there are
no more assemblies that were loaded into that context and no instances
of types from those assemblies). The strong handle now serves only to
keep the managed AssemblyLoadContext alive.
@janvorli janvorli added the area-AssemblyLoader-coreclr only use for closed issues label Apr 17, 2020
@janvorli janvorli added this to the 5.0 milestone Apr 17, 2020
@janvorli
janvorli requested a review from jkotas April 17, 2020 20:42
@janvorli janvorli self-assigned this Apr 17, 2020
@ghost

ghost commented Apr 17, 2020

Copy link
Copy Markdown

Tagging subscribers to this area: @vitek-karas
Notify danmosemsft if you want to be subscribed.

@jkotas
jkotas merged commit 701d8ee into dotnet:master Apr 18, 2020
@janvorli
janvorli deleted the fix-assemblyloadcontext-shutdown-race branch April 18, 2020 09:16
@ghost ghost locked as resolved and limited conversation to collaborators Dec 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-AssemblyLoader-coreclr only use for closed issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AssemblyLoadContext is collected while still in use

2 participants