diff --git a/src/coreclr/nativeaot/Bootstrap/main.cpp b/src/coreclr/nativeaot/Bootstrap/main.cpp
index cc78cf8d6710a9..c2ff85b50e81fd 100644
--- a/src/coreclr/nativeaot/Bootstrap/main.cpp
+++ b/src/coreclr/nativeaot/Bootstrap/main.cpp
@@ -93,7 +93,7 @@ static char& __unbox_z = __stop___unbox;
#endif // _MSC_VER
-extern "C" bool RhInitialize();
+extern "C" bool RhInitialize(bool isDll);
extern "C" void RhSetRuntimeInitializationCallback(int (*fPtr)());
extern "C" bool RhRegisterOSModule(void * pModule,
@@ -164,7 +164,13 @@ extern "C" void __managed__Startup();
static int InitializeRuntime()
{
- if (!RhInitialize())
+ if (!RhInitialize(
+#ifdef NATIVEAOT_DLL
+ /* isDll */ true
+#else
+ /* isDll */ false
+#endif
+ ))
return -1;
void * osModule = PalGetModuleHandleFromPointer((void*)&NATIVEAOT_ENTRYPOINT);
diff --git a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets
index b3219708a8cd13..5b1e1e8769bdad 100644
--- a/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets
+++ b/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Publish.targets
@@ -50,8 +50,7 @@
Text="RuntimeIdentifier is required for native compilation. Try running dotnet publish with the -r option value specified." />
-
-
+
diff --git a/src/coreclr/nativeaot/Runtime/startup.cpp b/src/coreclr/nativeaot/Runtime/startup.cpp
index 9a6fd2f9c22bb4..59968f939cd934 100644
--- a/src/coreclr/nativeaot/Runtime/startup.cpp
+++ b/src/coreclr/nativeaot/Runtime/startup.cpp
@@ -292,6 +292,10 @@ static void UninitDLL()
Thread* g_threadPerformingShutdown = NULL;
#endif
+#if defined(_WIN32) && defined(FEATURE_PERFTRACING)
+bool g_safeToShutdownTracing;
+#endif
+
static void __cdecl OnProcessExit()
{
#ifdef _WIN32
@@ -304,8 +308,16 @@ static void __cdecl OnProcessExit()
#endif
#ifdef FEATURE_PERFTRACING
- EventPipe_Shutdown();
- DiagnosticServer_Shutdown();
+#ifdef _WIN32
+ // We forgo shutting down event pipe if it wouldn't be safe and could lead to a hang.
+ // If there was an active trace session, the trace will likely be corrupted without
+ // orderly shutdown. See https://github.com/dotnet/runtime/issues/89346.
+ if (g_safeToShutdownTracing)
+#endif
+ {
+ EventPipe_Shutdown();
+ DiagnosticServer_Shutdown();
+ }
#endif
}
@@ -343,7 +355,7 @@ void RuntimeThreadShutdown(void* thread)
#endif
}
-extern "C" bool RhInitialize()
+extern "C" bool RhInitialize(bool isDll)
{
if (!PalInit())
return false;
@@ -352,6 +364,10 @@ extern "C" bool RhInitialize()
atexit(&OnProcessExit);
#endif
+#if defined(_WIN32) && defined(FEATURE_PERFTRACING)
+ g_safeToShutdownTracing = !isDll;
+#endif
+
if (!InitDLL(PalGetModuleHandleFromPointer((void*)&RhInitialize)))
return false;