Repository navigation
fix(threadx): acquire each distinct lock once per lock scope (#3878) - #3915
Merged
Merged
Conversation
A virtual pool key reuses its parent's _RWLock, and the family map lists
both names, so read_lock("cpu") expanded to two pairs naming the same lock
when the default config contains cpu and model_loader:cpu. _ReadLockGroup
acquired that lock twice. _RWLock.acquire_read blocks new readers while a
writer waits, so the second acquisition waited for a writer that was
itself waiting for the reader's first acquisition: a permanent deadlock.
Deduplicate scope pairs by lock identity, keeping first-seen order, in the
shared _LockGroup and _ReadLockGroup constructors. Every multi-lock path
(family read scope, read_lock("all"), lock(), wait(lock=True)) routes
through them, so all of them now acquire each lock once. Distinct locks
keep their canonical order and release order stays the exact reverse, so
two scopes over the same locks still cannot interleave. Writer preference
is unchanged, and the alias still shares the parent lock.
Fixes #3878
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #3878
TL;DR
A lock scope is a list of
(device_key, _RWLock)pairs, but two keys can name thesame lock: a virtual pool key reuses its parent's lock.
read_lock("cpu")on theshipped default pool therefore acquired one lock twice, which deadlocks against a
queued writer.
_LockGroup/_ReadLockGroupnow deduplicate pairs by lockidentity, so every multi-lock path acquires each lock once. Net diff: +35/−2 in
gptqmodel/utils/threadx.py, one new test module, one new docs page.1. The problem
DeviceThreadPoolgives each device key a writer-preference_RWLock, and avirtual pool key (
alias:parent) reuses the parent's lock object so that twoworker groups on one physical device serialize against each other. The shipped
CPU config uses this for
model_loader:cpu.Scope expansion then produces one pair per key, not per lock:
sequenceDiagram participant R as reader thread participant L as _RWLock for cpu participant W as writer thread R->>L: acquire_read() for "cpu" W->>L: acquire_write() blocked, writers_waiting = 1 R->>L: acquire_read() for "model_loader:cpu" same lock Note over L: writer-preference loop blocks the new reader Note over R,W: reader waits for the writer, writer waits for the reader Note over R,W: permanent deadlock_RWLock.acquire_read(gptqmodel/utils/threadx.py:220-234) blocks new readerswhile any writer waits. It recognizes a writer re-entering as a reader, but a
reader re-entering behind a queued writer is treated as a brand-new reader.
origin/main(66d27f9)gptqmodel/utils/threadx.py:822self._locks[v_key] = self._locks[parent_key]_rebuild_family_keys,gptqmodel/utils/threadx.py:882-897read_lock("cpu")expands to both keys:1296,:1311pairs = [(k, self._locks[k]) for k in fam_keys]:256and:278self._pairs = ordered_pairs:229while self._writer is not None or self._writers_waiting > 0The same duplicate existed in
read_lock("all")(_ordered_keyscontains thealias key) and in
wait(lock=True); the writer pathlock()tolerated it onlybecause
acquire_writeis re-entrant.2. The new flow
flowchart TD A["read_lock(scope) / lock() / wait(lock=True)"] --> B["expand scope to key pairs"] B --> C["_dedupe_lock_pairs: keep first pair per lock identity"] C --> D{"more pairs?"} D -- Yes --> E["acquire this lock once"] E --> D D -- No --> F["body runs"] F --> G["release in reverse order"] G --> H["queued writer proceeds"] style C fill:#dfd,stroke:#060 style H fill:#dfd,stroke:#060_dedupe_lock_pairskeys onid(lock)and keeps first-seen order, soacquisition order stays deterministic and release order remains the exact
reverse. Because
_LockGroup,_ReadLockGroup, and_WaitAndLockall constructthrough it, one rule covers every multi-lock path.
3. Failure and ambiguity contract
read_lock("all")with an aliased keywait(lock=True)over an aliased scope_family_keysDeduplication is deliberately at acquisition time, not at pool construction: the
alias must keep sharing the parent lock, and
wait()inflight bookkeeping iskeyed by name.
4. Change inventory
gptqmodel/utils/threadx.py_dedupe_lock_pairs, used by both group constructors; docstring contractstests/test_threadx_cpu_locks.pydocs/cpu-pool-lock-scopes.mdREADME.md5. Tests / guarantees
test_virtual_alias_shares_its_parent_locktest_family_read_lock_acquires_the_shared_lock_onceread_lock("cpu")acquires one distinct locktest_all_read_lock_acquires_each_lock_onceread_lock("all")acquires every distinct pool lock oncetest_wait_lock_true_acquires_each_lock_oncewait(lock=True)uses the deduplicated grouptest_family_read_lock_does_not_deadlock_against_a_waiting_writertest_lock_group_still_acquires_every_distinct_lockEvery wait in the new module is bounded (
Event.wait,Thread.jointimeouts), soa regression fails rather than hanging the suite. The module is CPU-only and does
not inherit the CUDA skip mark in
tests/test_threadx.py.6. Validation
Hardware: RTX 4090 (SM 8.9), torch 2.16.0.dev20261102+cu130; R9700 (gfx1201),
torch 2.16.0.dev20261025+rocm10.1.
pytest tests/test_threadx_cpu_locks.py -q(CUDA venv)threadx.pychange stashed (unmodifiedorigin/main)pytest tests/test_threadx.py tests/test_threadx_cpu_locks.py tests/test_calibration_data_device.py -q(CUDA venv, one 4090)pytest tests/test_threadx_cpu_locks.py tests/test_calibration_data_device.py -q(ROCm venv, one R9700)alias shares lock: True,both blocked: FalseThe 28 skips are the existing
require_n_gpus(2)guards on a single visible GPU.7. Reviewer notes
tests/test_calibration_data_device.py::TestCalibrationDataDeviceIntegration::test_calibration_data_device_cpu_integrationfails on this R9700 host with
ValueError: model.layers.1.mlp.down_proj.scales: 6818 scale value(s) cannot be stored as fp16 ... largest finite source magnitude is 8.242e+36.I re-ran that single test with this diff stashed and it fails identically on
unmodified
origin/main, so it is an environment/model issue unrelated to locking.It passes in the CUDA run above.
deduplication because the defect is identity aliasing inside one scope, and
deduplication fixes
read_lock("cpu"),read_lock("all"),lock(), andwait(lock=True)in one place without changing_RWLocksemantics.read_lockscopes overthe same device still queues behind a waiting writer on the inner scope. No
shipped call site nests them (
gptqmodel/looper/stage_inputs_capture.pytakesa single family read lock), so this is not addressed here; it would require
per-thread read re-entrancy in
_RWLock.