Skip to main content

SKY-Q403 · Quality, structure and performance

Inconsistent lock acquisition order

High

Two locks are taken in one order in one place (ledger_lock, then audit_lock) and in the opposite order somewhere else in the same file, while the first is still held. Two threads that run both paths at the same time can each hold one lock and wait forever for the other.

Languages
Python
Quality gate
counted toward the gate limits (full and diff scans)
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

This is a deadlock: the threads stop, nothing crashes, and nothing is logged. It only happens when the timing lines up, so it passes tests and appears in production under load, freezing transfers until the process is restarted.

What Skylos flags

ledger/transfers.py · PythonReported on line 19
"""Move money between the ledger and the audit log.""" import threading ledger_lock = threading.Lock()audit_lock = threading.Lock()  def post_transfer(ledger, audit, transfer):    with ledger_lock:        ledger.apply(transfer)        with audit_lock:            audit.record(transfer)  def reconcile(ledger, audit):    with audit_lock:        entries = audit.pending()        with ledger_lock:            ledger.mark_reconciled(entries)

What Skylos leaves alone

ledger/transfers.py · Python
"""Move money between the ledger and the audit log.""" import threading ledger_lock = threading.Lock()audit_lock = threading.Lock()  def post_transfer(ledger, audit, transfer):    with ledger_lock:        ledger.apply(transfer)        with audit_lock:            audit.record(transfer)  def reconcile(ledger, audit):    with audit_lock:        entries = audit.pending()    with ledger_lock:        ledger.mark_reconciled(entries)

Why Skylos leaves it alone

reconcile still takes audit_lock first, but it releases it before taking ledger_lock. It never holds both at once, so it cannot wait on post_transfer while blocking it.

How Skylos decides

  1. Runs with --quality on Python files and reads the whole module, across all its functions, methods and classes.
  2. Treats a with or async with item as a lock when its text contains "lock", "mutex" or "semaphore" (ledger_lock, self.mutex). Locks are told apart by their exact text.
  3. Records every pair "A held while B is taken", from nested with blocks and from several locks in one with a, b: statement.
  4. Reports when a pair B then A follows an earlier pair A then B, unless both were taken while holding a common outer lock, which keeps the two paths from running at once.
  5. Reports HIGH on the with statement that takes the second lock in the reversed order.

How to fix it

Pick one order for the two locks and use it everywhere. Here both functions take ledger_lock before audit_lock.

ledger/transfers.py · Python · the fix, against the flagged file
"""Move money between the ledger and the audit log.""" import threading ledger_lock = threading.Lock()audit_lock = threading.Lock()  def post_transfer(ledger, audit, transfer):    with ledger_lock:        ledger.apply(transfer)        with audit_lock:            audit.record(transfer)  def reconcile(ledger, audit):Removed:     with audit_lock:Removed:         entries = audit.pending()Removed:         with ledger_lock:Removed:             ledger.mark_reconciled(entries)Added:     with ledger_lock:Added:         with audit_lock:Added:             entries = audit.pending()Added:         ledger.mark_reconciled(entries)

When it can be wrong

  • Two code paths that can never run at the same time (for example, one runs only at start-up) are still reported.
  • It misses locks whose names do not contain "lock", "mutex" or "semaphore", locks taken with .acquire(), and orders that differ across files.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Put the marker on the reported with line.

    Python
    with ledger_lock:  # skylos: ignore[SKY-Q403] reconcile runs only at startup
  • [tool.skylos] ignore = ["SKY-Q403"] turns the rule off for the project.

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.