Repository navigation
Async runtime: no wait for the pool while a Windows process exits (foreign-thread-gc async-pool hang) - #826
Merged
Conversation
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 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
test-compile-foreign-thread-gctimed out on Windows CI six times. The failing run each time was itsasync-poollibrary, and the latest blocked release alpha125's first attempt. It wasn't a flaky test. It was a hang in the async runtime at process exit, which any host of a tslang DLL that awaits can hit.Cause
Every failing log prints both host lines,
loading thread: 2000 callsand8 other threads: 2000 calls each. All the work had finished and been checked; the process hung on its way out.Locally, 3 stress loops on 4 shared cores hung once in about 700 runs. The ProcDump dump shows one thread left:
The sequence:
mainin that window,ExitProcessterminates the worker withactiveTasksstill 1.ExitProcessthen runs the DLL's detach code, and with it the static~AsyncRuntime.~AsyncRuntimecallsthreadPool.wait(), which waits on a condition variable that no thread is left to signal.The bug is Windows-only. A static library's destructors run from
exitwhile the workers still run, and off Windows they always do. The JIT met the same hang inTypeScriptRuntime.dlland works around it by ending withTerminateProcess(jit.cpp). A--emit=dlllibrary has no such way out.Fix
AsyncRuntimeCommon.incholds the default runtime in a smallDefaultAsyncRuntimeholder. While the process exits on Windows (RtlDllShutdownInProgress), the holder leaves the runtime alone: no wait, no pool lock, no join of threads that no longer exist. Every other teardown is unchanged.Testing
test-compile-foreign-thread-gcpasses.async-pooltimes out like CI.test-compile-foreign-thread-gcpasses, and the same 4-core stress loops ran 1,200 runs (3 × 400) with no hang. Before the fix they hung once in about 700.🤖 Generated with Claude Code