SKY-Q403 · Quality, structure and performance
Inconsistent lock acquisition order
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
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
"""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
"""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
- Runs with
--qualityon Python files and reads the whole module, across all its functions, methods and classes. - Treats a
withorasync withitem as a lock when its text contains "lock", "mutex" or "semaphore" (ledger_lock,self.mutex). Locks are told apart by their exact text. - Records every pair "A held while B is taken", from nested
withblocks and from several locks in onewith a, b:statement. - 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.
- Reports HIGH on the
withstatement 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.
"""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
withline.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.