Skip to content

fix(looper): close the Device-SMI handles on a failed or cancelled run (#4813) - #4823

Merged
Qubitium merged 2 commits into
mainfrom
fix/issue-4813
Nov 17, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/issue-4813

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Fixes #4813.

Every LoopProcessor opens a device_smi.Device per device plus the CPU; each
starts a daemon device-smi-metrics-* thread polling at 5 Hz. The handles were
closed only in LoopProcessor.finalize(), which runs only on the success path,
so every StopMainLoop, KeyboardInterrupt or callback/kernel exception leaked
two polling threads (NVML/procfs) per processor for the life of the process.

Change

  • LoopProcessor.release_failed_run(): closes the Device-SMI handles and
    releases the workspace cache, mirroring the part of finalize() that the
    failure path skipped. Idempotent, so a later finalize() is a no-op.
  • ModuleLooper.loop()'s failure branch calls it right after
    _fence_run_work() (before layer/offload restoration). Per-processor
    failures are contained so the original run error is never masked.
  • WeightOnlyLooper.loop() had the same leak; it releases there too.

Tests

New tests/test_failed_run_device_smi_cleanup_4813.py:

  • E2E for a cancelled (StopMainLoop) and a raising callback: snapshots
    device-smi* thread idents, then polls (up to 10 s) and asserts none outlive
    the run.
  • Unit: the failure branch releases after the fence, a raising release is
    contained, and release_failed_run() is idempotent with a real CPU handle.
pytest tests/test_failed_run_device_smi_cleanup_4813.py \
       tests/test_failed_run_fence_4595.py \
       tests/test_cancelled_quantize_layer_cleanup_4546.py  -> 10 passed
pytest tests/test_module_looper_quant_log_4321.py tests/test_quantize_run_summary.py \
       tests/test_offload_run_namespace.py tests/test_finalize_module_lookup.py -> 57 passed
pytest tests/test_weight_only_looper.py -> 19 passed, 1 failed

The one weight-only failure (..._respects_dense_and_moe_vram_strategy_devices)
also fails on the parent commit: the container exposes one GPU while the test
expects cuda:0/1/2 placement.

Red on parent (source reverted, test kept): 5 failed — leaked
device-smi-metrics-cpu, missing release call, and
AttributeError: no attribute 'release_failed_run'.

Manual repro: parent cancelled run leaves ['device-smi-metrics-cpu']; fixed
leaves []; fixed successful run leaves [].

ruff: clean.

@Qubitium
Qubitium merged commit e33acc8 into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4813 branch November 17, 2026 15:57
Qubitium added a commit that referenced this pull request Nov 17, 2026
#4906)

#4823 released the processors' Device-SMI handles in the failure branch of
ModuleLooper.loop() and WeightOnlyLooper.loop(). A quantize() that failed
before a looper existed still leaked them: LoopProcessor.__init__ opened one
device_smi.Device per accelerator plus the CPU first and only then validated
the calibration dataset, so every empty, too-short, or malformed dataset
raised with two live 5 Hz sampler threads. A later processor in the chain
failing (GPTQ + EoRA) leaked the processors already built, and the half-built
one, for the same reason. Retry loops accumulated two threads per attempt.

Open the Device-SMI handles lazily on the first metric query, so a processor
that never runs never starts a sampler, and keep a closed processor closed.
Additionally wrap the whole quantize prologue -- processor construction,
checkpoint entry, install_w4a_llama_replay, ModuleLooper.__init__, and the
loop itself -- in a try/except that releases every processor built so far
(shared release_failed_run_processors helper, also used by ModuleLooper).
Weight-only and EoRA generation get the same wrapper.

Adds tests/test_pre_looper_failure_cleanup_4862.py: empty, too-short, and
malformed calibration, a later EoRA processor failing after GPTQ was built,
and release_failed_run on a never-opened processor. On the parent the three
calibration cases each leave a device-smi-metrics-cpu thread and the EoRA
case never releases the built processor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant