From 7c027ae79be9b5df1e01e71814c52f4b2413d7c9 Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Thu, 12 Nov 2026 14:49:17 +0000 Subject: [PATCH] Async runtime: no wait for the pool while a Windows process exits ExitProcess terminates every other thread, the pool's workers too, and only then runs the DLLs' detach code. A runtime in a DLL (a --emit=dll library, the JIT's TypeScriptRuntime.dll) then waited in ~AsyncRuntime for a task no thread would finish. A worker wakes the thread awaiting its task before it counts the task done, so a host that returned from main in between hung forever. That is the Windows-only test-compile-foreign-thread-gc async-pool timeout, seen six times on CI. While the process exits (RtlDllShutdownInProgress) the default runtime is left alone. Co-Authored-By: Claude Opus 5.5 --- tslang/lib/AsyncRuntimeCommon.inc | 51 +++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/tslang/lib/AsyncRuntimeCommon.inc b/tslang/lib/AsyncRuntimeCommon.inc index 4f580cd07..abd8d8315 100644 --- a/tslang/lib/AsyncRuntimeCommon.inc +++ b/tslang/lib/AsyncRuntimeCommon.inc @@ -29,6 +29,16 @@ #include #include +#ifdef _WIN32 +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#endif + // only the hooks: GC_enable_threads and the collector calls are in AsyncGCThreadsCommon.inc #include "TypeScript/AsyncGCThreads.h" @@ -299,11 +309,48 @@ namespace } // namespace +// Whether the process is exiting on Windows. ExitProcess terminates every other thread - the pool's +// workers too - and only then runs the DLLs' detach code, a DLL's static destructors among them. +// A static library's destructors run earlier, from `exit`, while the workers still run; and off +// Windows they always do. +static bool processIsExiting() +{ +#ifdef _WIN32 + using ShutdownInProgressFn = BOOLEAN(NTAPI *)(); + static auto shutdownInProgress = reinterpret_cast( + GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "RtlDllShutdownInProgress")); + return shutdownInProgress != nullptr && shutdownInProgress(); +#else + return false; +#endif +} + +// The default per-process instance of an async runtime, destroyed with the module. +// +// Except while the process exits on Windows (processIsExiting): its workers are gone then, +// terminated wherever they were, a task half done, a lock held. A runtime in a DLL - a `--emit=dll` +// library, the JIT's TypeScriptRuntime.dll - would wait in ~AsyncRuntime for a task no thread will +// finish, and the process never ends. A host hung so now and then: the worker running the last +// task woke the thread awaiting it before it counted the task done, and the host returned from +// main in between. Leaving the runtime alone loses nothing - the process is going away. +struct DefaultAsyncRuntime +{ + std::unique_ptr runtime = std::make_unique(); + + ~DefaultAsyncRuntime() + { + if (processIsExiting()) + { + (void)runtime.release(); + } + } +}; + // Returns the default per-process instance of an async runtime. static std::unique_ptr &getDefaultAsyncRuntimeInstance() { - static auto runtime = std::make_unique(); - return runtime; + static DefaultAsyncRuntime instance; + return instance.runtime; } static void resetDefaultAsyncRuntime()