Skip to content

[Bug] Windows Native GGUF Inference: GPU/CUDA Hang on Cancel & High CPU Spinlock Overhead #5692

Closed

Description

@hjin4eo

Description: We identified two critical performance and resource-leak issues when running GGUF models on Windows Native environment using the llama-server backend. We have successfully resolved these locally with the following patches.

Issue 1: GPU/CUDA Hang on Stream Cancellation (Socket Leak)
Symptom: When a user stops/cancels a chat completion streaming midway (e.g., stopping the generation or closing the tab), llama-server continues decoding in the background. The GPU utilization stays at 100%, and the fans keep spinning indefinitely.
Root Cause: In

inference.py
, the httpx.AsyncClient proxies completions using HTTP Keep-Alive. When the client disconnects, llama-server is never notified of the closed connection.
Fix: Disable Keep-Alive with Connection: close header and max_keepalive_connections=0. Add a background cancel_watcher task checking request.is_disconnected() to actively trigger resp.aclose() to drop the connection:
python

In backend/routes/inference.py

async def cancel_watcher():
try:
while True:
if await request.is_disconnected():
await resp.aclose()
break
await asyncio.sleep(0.1)
except asyncio.CancelledError:
pass
Issue 2: High CPU Spinlock & KV Cache Backup Bottleneck on Windows
Symptom: During GGUF inference (especially with MTP/speculative decoding), CPU usage spikes to 100% causing heavy thermal throttling and loud noise, even when fully offloaded to the GPU (ngl -1).
Root Causes:
OpenMP Spinlock: The default OpenMP behavior in Windows spawns threads up to the logical core count to await GPU completion, wasting CPU cycles in a spinlock.
KV Cache Sync: llama-server copies prompt cache checkpoints to system RAM by default, causing massive WDDM driver and PCI-E bandwidth overheads on Windows.
Fix: Apply Windows-specific optimizations in

llama_cpp.py
:
Limit CPU threads to 2 when fully offloaded to GPU on Windows.
Force OMP_WAIT_POLICY=PASSIVE and OMP_NUM_THREADS=2 in the subprocess environment.
Disable context RAM checkpoints explicitly by adding --cache-ram 0, --ctx-checkpoints 0, --no-cache-prompt, and --checkpoint-every-n-tokens -1 to the server command on Windows.
Environment:

OS: Windows 11 (Native)
CUDA Version: 12.x
Unsloth Studio Version: Latest (Windows prebuilts)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions