-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[NativeAOT] Cleanup and rationalizing process/thread termination scenario #80063
Conversation
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsIt was noticed a while ago that while the current scheme works, it has some ad-hoc parts that may not be robust enough for broader set of scenarios such as terminating the process from multiple threads or by calling This PR tries to take care of that debt.
In either case the notification comes without loader/OS locks held, so detaching is safe.
|
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
// Indicate that runtime shutdown is complete and that the caller is about to start shutting down the entire process. | ||
g_threadPerformingShutdown = ThreadStore::RawGetCurrentThread(); | ||
} | ||
|
||
#ifdef _WIN32 | ||
EXTERN_C UInt32_BOOL WINAPI RtuDllMain(HANDLE hPalInstance, uint32_t dwReason, void* pvReserved) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not see any place where this is used. Delete it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed RtuDllMain
is used somehow when a NativeAOT dll is created. It is not used as-is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not think it is used anywhere. It looks like a left-over from redhawk.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like, at least in a case of .dll publishing, one would need to hookup InitDLL
/UninitDLL
, and on windows OnProcessExit
.
Is that handled somehow or dynamic NativeAOT libraries are not supported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we have a static lib sample, but no dlls.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see we have a static lib sample, but no dlls.
The DLL sample is in the same location: https://github.com/dotnet/samples/tree/main/core/nativeaot/NativeLibrary#building-shared-libraries
Building DLLs is the only officially documented thing. Static lib is just the sample: https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/#build-native-libraries
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I missed that, how does the .dll initialize the runtime? Calls RtuDllMain
somehow, or calls RhInitialize
on the first reverse pinvoke?
The latter should work (and perhaps more reliably) with this change.
Do we have a test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we initialize on first reverse p/invoke. The test is here: https://github.com/dotnet/runtime/tree/main/src/tests/nativeaot/SmokeTests/SharedLibrary
Co-authored-by: Jan Kotas <[email protected]>
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
/azp run runtime-extra-platforms |
Azure Pipelines successfully started running 1 pipeline(s). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM once the runtime-extra-platforms tests pass.
|
There are some failures in Mono tests, but I do not think this PR shares anything with Mono. I see similar failures in #80035 |
Thanks!! |
It was noticed a while ago that while the current scheme works, it has some ad-hoc parts that may not be robust enough for broader set of scenarios such as terminating the process from multiple threads or by calling
ExitProcess
from native code, or in a context of a.dll
This PR tries to take care of that debt.
Fixes: #74711
CrstSuspendEE
and threadstore lock into oneCrstThreadStore
ExitProcess
, processingCTRL-C
or in few other ways is special. Blocking it will prevent process exiting. We detect this thread viaDLL_PROCESS_DETACH
(.dll) oratexit()
(.exe) and remember to not detach this thread. We do not want to run managed code or take threadstore lock and risk blocking this thread when process may already be in inconsitent state.PalAttachThread
is to register with OS thread premortem notification.PalDetachThread
just unregisters.We use:
In either case the notification comes without loader/OS locks held, so detaching is safe.
The only case where loader lock could be held is shutting down the special thread on Windows. One more reason to not detach that one.