← Writing

A Thread Waiting on Its Own Lock

3 min read#Python#Concurrency#Autarkia#EdgeSched

Autarkia hung at Starting with no error; only a faulthandler dump showed it waiting on a lock that the same thread already held, three imports deep. Later came lazy singletons calling each other and a write lock requested under a read lock: four hangs in all.

On August 13 Autarkia started up and stayed on the Starting screen: no traceback, no exit, no output. I added faulthandler.dump_traceback_later(60) at the entry point and waited for it to dump every thread’s stack after a minute; only then could I see it was waiting on a lock.

The logging module protects its logger cache with a plain threading.Lock. get_logger() holds the lock while initializing, initialization reads the config, the config module imports a shared utilities module, and that module calls get_logger() at the top level. The same thread comes back for the lock three imports later.

threading.Lock doesn’t remember who holds it. If the holder calls acquire() again, it queues like any other thread, waiting on itself; without a timeout it waits forever and raises nothing.

It happened again in EdgeSched, my scheduling research prototype, in the same shape but much shorter:

python
_singleton_lock = threading.Lock()

def get_runtime_metrics():
    with _singleton_lock:
        ...

def get_admission_controller():
    with _singleton_lock:
        metrics = get_runtime_metrics()   # 同一个线程,同一把锁
        ...

Two singletons load lazily and share one lock. Each function looks fine on its own; the re-entry happens along the call chain. Both places switched to threading.RLock, which records its owner and lets the same thread enter again.

01Beyond “the write lock is reentrant”

The memory module uses a hand-written read-write lock. In April it deadlocked because the write lock wasn’t reentrant; after the fix, the class docstring said the write lock is reentrant and the same thread can take it several times without deadlocking.

On August 27 it hung again. The first keyword search froze the process, and the log printed write lock waiting >5s every five seconds. The search function held the read lock and called an ensure-the-index-is-ready function, which takes the write lock when it needs to rebuild.

Reading “reentrant” makes it easy to assume the same thread can take it any way it likes. I copied the class as-is and ran three scenarios, counting anything that didn’t return within 6.5 seconds as hung:

  • Write lock inside a write lock: returns normally.
  • Write lock inside a read lock: hangs. The write lock waits for the reader count to reach zero, and the reader is itself.
  • Read lock inside a read lock while another thread starts waiting for the write lock: hangs. The lock favors writers, so new readers wait while a writer is queued; the writer waits for the first read to release, and the second read waits for the writer.

The second is the August 27 hang, fixed by moving the index check outside the read lock. I haven’t found code that triggers the third directly, and the lock does nothing to prevent it. The docstring still mentions only the first.