Skip to content

Async runtime: no wait for the pool while a Windows process exits (foreign-thread-gc async-pool hang) - #826

Merged
ASDAlexander77 merged 1 commit into
mainfrom
async-runtime-exit-no-wait
Nov 12, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
async-runtime-exit-no-wait

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

test-compile-foreign-thread-gc timed out on Windows CI six times. The failing run each time was its async-pool library, 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 calls and 8 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:

SleepConditionVariableSRW
async_pool!...                      (static destructors)
ntdll!LdrShutdownProcess
kernel32!ExitProcess
foreign_thread_gc_host (main returned)

The sequence:

  1. A pool worker runs the last task. The task wakes the host thread that awaits it, and only then does the worker take the pool lock to count the task done.
  2. If the host returns from main in that window, ExitProcess terminates the worker with activeTasks still 1.
  3. ExitProcess then runs the DLL's detach code, and with it the static ~AsyncRuntime.
  4. ~AsyncRuntime calls threadPool.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 exit while the workers still run, and off Windows they always do. The JIT met the same hang in TypeScriptRuntime.dll and works around it by ending with TerminateProcess (jit.cpp). A --emit=dll library has no such way out.

Fix

AsyncRuntimeCommon.inc holds the default runtime in a small DefaultAsyncRuntime holder. 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

  • Deterministic repro: a temporary 100 ms sleep in the worker, between finishing a task and counting it done.
    • With the fix: test-compile-foreign-thread-gc passes.
    • Without it: async-pool times out like CI.
  • Without the sleep: test-compile-foreign-thread-gc passes, 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.
  • Full ctest, Windows release: 6976/6976 passed (3 disabled tests not run).

🤖 Generated with Claude Code

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>
@ASDAlexander77
ASDAlexander77 merged commit 62aa5b8 into main Nov 12, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the async-runtime-exit-no-wait branch November 12, 2026 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant