Skip to content

[BUG] Every cancelled/failed quantize() leaks the processors' device-smi metric threads (2 per run, NVML/CPU polled at 5 Hz forever): handles are closed only in finalize(), which the failure path never calls #4813

Closed

Description

@Qubitium

Found on main @ dba142d, RTX 4090, torch 2.15, device-smi installed in the venv.

What is wrong

Each LoopProcessor opens device_smi.Device handles for every device plus the CPU (gptqmodel/looper/loop_processor.py:535-536). Each handle starts a daemon device-smi-metrics-<type> thread that polls metrics every 200 ms (device_smi/device.py:_configure_fast_metrics, NVML for the GPU).

The handles are closed only in LoopProcessor.finalize() (loop_processor.py:1600, _close_device_smi_handles), and finalize() runs only on the success path. The failure branch of ModuleLooper.loop() (module_looper.py:2042-2063) does the following, and none of it closes the processors' handles:

  • fences work,
  • restores the in-flight layer and base modules,
  • releases the offload root,
  • releases the run workspace.

Each poll thread holds its Device through the bound target, so garbage collection never stops it.

As a result, every cancelled or failed quantize() permanently leaks 2 polling threads (GPU + CPU) per processor and device. This covers StopMainLoop from subset_callback, KeyboardInterrupt, and callback or kernel exceptions. The leaked threads keep querying NVML/procfs 5 times per second for the life of the process. A later successful run does not reap them.

Repro (Qwen2.5-0.5B-Instruct truncated to 4 layers, GPTQ 4-bit; cancel at layer 1 through the supported subset-callback path)

import gc, threading, time, resource
from gptqmodel import GPTQModel
from gptqmodel.quantization import config as C
from gptqmodel.looper.module_looper import StopMainLoop
def smi(): return [t.name for t in threading.enumerate() if t.name.startswith("device-smi")]
for i in range(4):
    m = GPTQModel.load(MODEL, C.GPTQConfig(bits=4, group_size=128), dtype=torch.float16)
    m.subset_callback = lambda **kw: StopMainLoop if kw.get("stage") == "forward_start" and kw.get("layer_idx") == 1 else None
    try: m.quantize(C.CalibrationConfig(dataset=calib, batch_size=4))
    except StopMainLoop: pass
    del m; gc.collect(); time.sleep(1)
    print(f"after cancelled run {i+1}: device-smi threads {len(smi())}")   # + idle CPU over 5 s
start: device-smi threads 0, idle cpu% 0.0
after cancelled run 1: device-smi threads 2 idle cpu% 0.4
after cancelled run 2: device-smi threads 4 idle cpu% 0.8
after cancelled run 3: device-smi threads 6 idle cpu% 1.3
after cancelled run 4: device-smi threads 8 idle cpu% 1.4
after a successful run: device-smi threads 8 idle cpu% 1.5

A failure from an exception inside GPTQ.quantize, a layer_callback exception or KeyboardInterrupt, or a layer-forward exception leaves the same two extra threads (device-smi-metrics-cpu, device-smi-metrics-gpu) next to the pool's DPWorker-*/DP-Janitor. A successful run leaves none.

The same lingering threads were already visible in #4356's pytest thread count ("Twenty of those threads are lingering device-smi-metrics threads"), but the root cause was not filed.

Suggested fix

In ModuleLooper.loop()'s failure branch, after _fence_run_work(), close every processor's device-smi handles. Either call a new LoopProcessor.release_failed_run() that runs _close_device_smi_handles() (and the release_workspace_cache() that finalize() also skips), or make _close_device_smi_handles part of release_run_workspace(), which the finally already calls on every exit.

Test that should have caught it

tests/test_failed_run_fence_4595.py / test_cancelled_quantize_layer_cleanup_4546.py: assert that no device-smi-metrics* thread outlives a failed quantize().

Severity

Low-medium. It is a thread and CPU leak that grows with every cancelled or failed run in long-lived processes (notebooks, services that retry, sweeps that catch errors), and it pollutes thread-count tests.

🤖 Generated with Claude Code

Activity

  1. Qubitium commented on Nov 17, 2026

    @Qubitium
    CollaboratorAuthor

    Agent claim released: agent-53ba17eb4d83 finished this issue.

    Pull request: #4823

    Fixing: reproduce, root-cause, add regression test, keep existing suites green.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions