Skip to content

fix(looper): fence submitted work before tearing down a failed run (#4595) - #4631

Merged
Qubitium merged 1 commit into
mainfrom
fix/issue-4595-fence-failed-run
Nov 17, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/issue-4595-fence-failed-run

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Root cause

ModuleLooper.loop() drains DEVICE_THREAD_POOL, the finalizer watcher threads and STREAM_DEVICE_POOL only on the success path. The except BaseException branch went straight to _restore_inflight_layer() / release_private_offload_root(), so submodule finalizers that were already submitted kept running:

  • they re-created the per-run offload directory release_private_offload_root() had just deleted, and wrote that layer's bundles into it. The model attribute and weakref finalizer were already cleared, so nothing removed it — one leaked bundle set per failed/cancelled run;
  • they kept packing and replacing modules after quantize() had raised, racing caller code that retries, saves or inspects the model.

#4515 fixed only the subset capture-hook half of #4470.

Change

gptqmodel/looper/module_looper.py: new _fence_run_work() drains the device thread pool, the dangling finalizer watcher threads and the stream device pool, and the failure branch calls it before restoring the in-flight layer, restoring base modules, or releasing the run directory. Each wait is guarded independently so one wedged pool cannot skip the remaining waits, and nothing in the fence can mask the original exception.

Tests

New tests/test_failed_run_fence_4595.py (3 tests):

  • ordering test: the three pool waits must complete before any teardown, and the run directory must still hold the finalizer's bundle at the moment it is deleted;
  • a wedged DEVICE_THREAD_POOL.wait() must not skip the other waits, teardown, or replace the run's original exception;
  • end-to-end tiny Llama quantize with offload_to_disk, a finalizer held in flight (offload_to_disk slowed) and a layer_callback that raises: after the raise, no run-* directory may reappear.

All three fail on unmodified main (the end-to-end one reproduces the leaked run-126242db45b84bc9 directory) and pass with the fix:

tests/test_failed_run_fence_4595.py -> 3 passed
tests/test_failed_run_fence_4595.py tests/test_subset_cancel_hook_cleanup_4470.py
tests/test_offload_run_cleanup_4368.py tests/test_offload_run_namespace.py
tests/test_offload_reference_4395.py tests/test_offload_quantized_modules_restored_4450.py
-> 33 passed, 3 skipped

tests/test_exl3_workspace_release.py and 7 tests/test_stage_modules.py cases fail identically on unmodified main (they build ModuleLooper via object.__new__ without the attributes those paths need); they are unrelated and untouched.

Closes #4595.

The failure branch of ModuleLooper.loop() restored the model and deleted the
per-run offload directory while submodule finalizers already submitted to
DEVICE_THREAD_POOL were still running. Those finalizers re-created the run
directory that release_private_offload_root had just removed (one leaked
bundle set per failed run) and kept packing/replacing modules after
quantize() had raised to the caller. #4515 only fixed the capture-hook half
of #4470.

Drain the device pool, the finalizer watcher threads and the stream device
pool before any teardown, mirroring the success path. Each wait is
independent so one wedged pool cannot skip the others or mask the original
exception.

Issue #4595.
@Qubitium
Qubitium merged commit 42484be into main Nov 17, 2026
9 checks passed
@Qubitium
Qubitium deleted the fix/issue-4595-fence-failed-run branch November 17, 2026 05:53
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