Skip to content

CPU family read lock can deadlock against a writer through its shared virtual alias #3878

Closed

Description

@Qubitium

Deadlock in the shipped CPU pool (current main, daa5dae)

The default pool config includes both cpu and model_loader:cpu. These keys share one _RWLock, but family read_lock("cpu") includes both keys and acquires that same lock twice. If a writer queues between the two acquisitions, the second read waits behind the writer, while the writer waits for the first read: a permanent cycle.

This standalone script controls only that interleaving and exits after observing the blocked daemon threads:

import os, threading, time, torch
from gptqmodel.utils.threadx import DeviceThreadPool

pool = DeviceThreadPool(
    devices=[torch.device("cpu")],
    workers={"cpu": 1, "model_loader:cpu": 1},
    empty_cache_every_n=0,
)
lock = pool._locks["cpu"]
first, resume = threading.Event(), threading.Event()
original_acquire = lock.acquire_read
count = 0

def controlled_acquire():
    global count
    original_acquire()
    count += 1
    if count == 1:
        first.set()
        resume.wait(2)

lock.acquire_read = controlled_acquire

def reader():
    with pool.read_lock("cpu"):
        pass

def writer():
    with pool.device_lock("cpu"):
        pass

r = threading.Thread(target=reader, daemon=True)
w = threading.Thread(target=writer, daemon=True)
r.start()
assert first.wait(2)
w.start()
for _ in range(200):
    if lock._writers_waiting:
        break
    time.sleep(0.005)
assert lock._writers_waiting == 1
resume.set()
r.join(0.2)
w.join(0.2)
print("alias shares lock:", lock is pool._locks["model_loader:cpu"], flush=True)
print("both blocked:", r.is_alive() and w.is_alive(), flush=True)
os._exit(0)
# alias shares lock: True
# both blocked: True

_RWLock.acquire_read prioritizes waiting writers and does not recognize a read held by the same thread. The global CPU pool config is in gptqmodel/__init__.py, and CPU input capture uses this family read lock.

Expected

Deduplicate lock identities before acquiring a group, or make read reentrancy compatible with writer priority. Add a deterministic alias/read/write interleaving test.

Activity

  1. added a commit that references this issue on Nov 18, 2026
    8ecc295
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