← Back to Pull requests

Fix Windows shutdown deadlock and leak with the Ruy backend #2076

Closed
unknown closed this pull request without merging on Sep 26, 2026 06:26 UTC
View on GitHub
unknown unknown commented on Jul 18, 2026 05:09 UTC
Author

Hi, thanks for CTranslate2. I ran into a Windows-only shutdown hang and tracked it down
to the Ruy backend, so here's a fix.

Creating and destroying a Translator (or Generator/Whisper) with the Ruy
backend on Windows hangs the process on shutdown. The culprit is the per-thread
ruy::Context in src/cpu/backend.cc:

ruy::Context *get_ruy_context() {
  static thread_local ruy::Context context;
  return &context;
}

It's constructed lazily on a worker thread the first time that thread runs a GEMM,
and its destructor joins Ruy's internal thread pool. Because it's thread_local, that
destructor runs while the worker thread is exiting, under the Windows loader lock, and
the join deadlocks. ThreadPool::~ThreadPool in src/thread_pool.cc then blocks
forever in worker->join().

Sequence when a model is destroyed:

1. ~ReplicaPool → ~ThreadPool closes the queue and calls worker->join()
2. the worker's run() loop returns and the thread begins to terminate
3. the thread's thread_local destructors run, so ~ruy::Context joins the Ruy threads
4. that join, on a thread that is already exiting, deadlocks

This only happens on CPU. On CUDA the GEMM never calls get_ruy_context(), so the
worker thread has nothing to join and exits cleanly. That matches the reports that GPU
builds don't hang while CPU builds do.

The fix is to destroy the context from ReplicaWorker::finalize() instead. finalize()
runs on the worker thread inside run(), before the thread exits, so the join happens
in a normal context (no loader lock) and completes. It already calls
destroy_context(device) for this kind of per-thread cleanup (freeing curand state on
CUDA today), so the Ruy cleanup goes in the same place:

  • backend.cc: make the context a resettable heap pointer and add clear_ruy_context()

  • backend.h: declare it

  • devices.cc: call cpu::clear_ruy_context() from destroy_context() for Device::CPU
  • Everything stays under the existing CT2_WITH_RUY / CT2_WITH_CUDA guards, so other
    backends and platforms are untouched.

    Besides fixing the hang, this frees the context instead of leaking it. RSS after each
    of 5 load/translate/unload cycles (Windows, MSVC 14.44, x64, int8 NLLB-200 600M, Ruy):

    | | cycle 1..5 (MB) |
    |---|---|
    | bypassing the model's destructor (workaround used downstream) | 885, 1755, 2624, 3496, 4371 |
    | heap-leaking just the ruy::Context | 268, 520, 772, 1024, 1276 |
    | this PR | 16, 17, 18, 18, 19 |

    The ~250 MB/cycle in the middle row is Ruy's prepacked cache, which the leak keeps
    alive; finalize-clear releases it. Translation output is unchanged and identical across
    repeated calls, so rebuilding the context after clearing it is fine.

    Found via jkawamoto/ctranslate2-rs#64, where the hang was worked around by skipping the
    model's destructor entirely (avoiding the hang but leaking the model). With this change
    the destructor can run.

    Happy to adjust the approach if you'd prefer the cleanup somewhere else. Thanks!

    Diff: 375 lines (+281 / -94) across 4 files Reviewer: jordimas Review duration: 100877m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.43) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.43 · 375 lines diff across 4 files
    0.43
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.355
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #2076"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.9% (MEDIUM) · Higher = larger blast radius
    42.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.5% · Safe to merge
    18.5%