Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Pin CoreRT OS modules in memory - #7011

Merged
jkotas merged 1 commit into
dotnet:masterfrom
jkotas:issue-6993
Feb 13, 2019
Merged

jkotas merged 1 commit into
dotnet:masterfrom
jkotas:issue-6993

Conversation

@jkotas

@jkotas jkotas commented Feb 12, 2019

Copy link
Copy Markdown
Member

CoreRT is not designed to be unloadable. Unloading modules before process shutdown leads to crashes and memory leaks.

Fixes #6993

@filipnavara filipnavara left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense as a stopgap solution.

Is there any major reason why proper unloading would not be possible, or is it just a lot of small issues like the missing FlsFree?

@jkotas

jkotas commented Feb 13, 2019

Copy link
Copy Markdown
Member Author

It is both a lot of small issues like FlsFree as well as a more fundamental problem with making sure that all threads exit the runtime before unloading. E.g. There would need to be new unmanaged StopRuntime API that one has to call before the runtime can be unloaded. This API would block until all threads leave the runtime gracefully.

CoreRT is not designed to be unloadable. Unloading modules before process shutdown leads to crashes and memory leaks.

Fixes #6993
@jkotas
jkotas merged commit 04fb46f into dotnet:master Feb 13, 2019
@filipnavara

Copy link
Copy Markdown
Member

making sure that all threads exit the runtime before unloading.

Is there any documentation on how that worked with CLR and the .export keyword in ILAsm?

I would imagine that one way to solve it is to keep the DLL unloadable as long as any CLR thread is running. Something like ThreadPool may need some special handling though.

@jkotas

jkotas commented Feb 13, 2019 •

Copy link
Copy Markdown
Member Author

how that worked with CLR

CLR itself was never unloadable. Assemblies loaded into AppDomain were unloadable via AppDomain.Unload thad needed Thread.Abort, shutdown finalization, constrained execution regions and a few extra other things. AppDomain.Unload and the related features were a reliability nightmare.

@filipnavara

filipnavara commented Feb 13, 2019 •

Copy link
Copy Markdown
Member

CLR itself was never unloadable.

Interesting. We have implemented Simple MAPI as .NET .dll with exports running on full CLR. I never realised that it would not unload (partly due to some indirection on newer Windows versions that allowed for 32-bit/64-bit compatibility and could have hidden that from us). I'll have to do some more tests on it since we planned to replace it with CoreRT eventually.

@jkotas
jkotas deleted the issue-6993 branch February 17, 2019 05:50
@mjsabby

mjsabby commented Apr 15, 2019

Copy link
Copy Markdown
Contributor

Would it be a lot of work to introduce such an API that at least can forcefully shut down runtime and managed code threads for CoreRT only? Thread.Abort could be brought back in some form?

@jkotas

jkotas commented Apr 15, 2019

Copy link
Copy Markdown
Member Author

What would be the reliability guarantees provided by such API? How often (under what circumstances) is it ok for it to crash or hang; and how much it can leak?

@mjsabby

mjsabby commented Apr 16, 2019

Copy link
Copy Markdown
Contributor

It can’t crash or hang. Maybe the leak is tolerable in some scenarios, but ideally we would get to fixing all the issues.

I’ve found a new use case for CoreRT modules, think of like it once accessed or infrequently accessed bloated code that adds a ton of dependencies to your main long running app that you may not desire for let’s say perf of security footprint purposes. It gets loaded at startup, does its job and gets unloaded.

So reliability has to be there but leaking some memory maybe ok in the beginning. Assuming no static constructors and their running is not involved can there be other issues with Thread.Abort like behavior?

@jkotas

jkotas commented Apr 16, 2019 •

Copy link
Copy Markdown
Member Author

The problem with Thread.Abort is that it can crash or hang. The .NET Core class libraries are not hardened for Thread.Abort. Instead of Thread.Abort, can you guarantee that nothing is running managed code when you want to unload? Then the problem is just about releasing memory and unregistering callbacks.

@mjsabby

mjsabby commented Apr 16, 2019

Copy link
Copy Markdown
Contributor

I think that is a reasonable start. Guaranteeing no managed code running for unloading such components will not be terribly hard for my cases.

If there is a list somewhere of what needs fixing we can all start contributing.

@jkotas

jkotas commented Apr 16, 2019

Copy link
Copy Markdown
Member Author

If there is a list somewhere of what needs fixing we can all start contributing.

There is no list. You would need to come up with one.

@mjsabby

mjsabby commented Aug 8, 2019

Copy link
Copy Markdown
Contributor

Started a quick list.

  • SetUnhandledExceptionFilter
  • AddVectoredExceptionHandler
  • EventRegister
  • FlsAlloc
  • CreateMemoryResourceNotification

Are there any handles we need to free? I guess one way to find out will be to load/unload the module as see what handles stay ... or track all handles created and ensure they get cleaned up at free time.

I think all the memory is going through the ClrVirtualAlloc abstraction (at least in CoreCLR it was), so maybe that'll be easy to audit as well.

FreeLibrary(handle);
#else
// TODO: How to pin the library in memory on Unix?
// dlclose(handle);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reference, dlopen has RTLD_NODELETE flag. It seems to be available on both Linux and macOS.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash on library unload via Python

3 participants