Two Locks Taken in Opposite Order
drag-lint reports two places in a Delphi project that take the same two locks in opposite order, the classic deadlock that hangs a program with no exception and no log. One thread holds the first lock and waits for the second; another thread holds the second and waits for the first. The rule lock-order-inversion finds the pair from the index, even when one of the two locks is taken inside a called routine.
The rule arrived in drag-lint 1.56.0-alpha (2026-11-27). It is on by default, severity warning, category bug-patterns, weakness label CWE-833 (deadlock). Every output below is real output of 1.58.0-alpha on the small unit in section 01 (paths shortened; left out: the indexer's lines before its summary, the note lint prints about project rules, the progress lines of lint-all and the report path at the end of its summary).
A deadlock like this does not show up in a test run. Both orders work alone; the program hangs only when two threads hit the two places at the same moment, which on a customer's machine may be once a week. drag-lint comes from the team that builds Micronite, quality and production-control software for manufacturers.
Payments and the day close, two locks each
The unit Transfers.pas keeps a balance and a ledger total, each guarded by its own TCriticalSection. PostPayment takes the accounts lock and then, through WriteLedgerLine, the ledger lock. CloseDay takes the ledger lock first and then the accounts lock. Nothing in PostPayment mentions the ledger lock: it hides inside the call.
var
GAccountsLock: TCriticalSection;
GLedgerLock: TCriticalSection;
GBalance: Currency;
GLedgerTotal: Currency;
GClosedBalance: Currency;
procedure WriteLedgerLine(pAmount: Currency);
begin
GLedgerLock.Enter;
try
GLedgerTotal := GLedgerTotal + pAmount;
finally
GLedgerLock.Leave;
end;
end;
procedure PostPayment(pAmount: Currency);
begin
GAccountsLock.Enter; // line 32
try
GBalance := GBalance + pAmount;
WriteLedgerLine(pAmount); // line 35
finally
GAccountsLock.Leave;
end;
end;
procedure CloseDay;
begin
GLedgerLock.Enter; // line 43
try
GAccountsLock.Enter; // line 45
try
GClosedBalance := GBalance;
GLedgerTotal := 0;
finally
GAccountsLock.Leave;
end;
finally
GLedgerLock.Leave;
end;
end;
The two locks are created in the unit's initialization section and freed in finalization. The line comments are added here for reading; the unit itself has none.
Index, lint the project, read the two findings
Step 1. Index the folder into a database of its own. Nothing else is touched.
drag-lint index . --db fix.sqlite
Done. Files: 1, Symbols: 13, Refs: 55, 0.21s, parse-errors=0
Step 2. Lint the one file. The rule does not report here, and that is by design: the two places of an inversion are in different routines, often in different units, so the rule is project-wide and runs in lint-all only.
drag-lint lint Transfers.pas --db fix.sqlite
Transfers.pas:5:1 [info] unused-public-symbol: Exported routine PostPayment has no references in the index -- possible dead public API
Transfers.pas:6:1 [info] unused-public-symbol: Exported routine CloseDay has no references in the index -- possible dead public API
2 finding(s)
The two info findings only say that nothing in this one-unit fixture calls the routines. In a real project the callers are indexed and they go away.
Step 3. Lint the whole index, limited to this rule.
drag-lint lint-all --db fix.sqlite --rule lock-order-inversion
lint-all: scanning 1 .pas file(s)
Transfers.pas:35:5 [warning] lock-order-inversion: Lock order inversion: Transfers.WriteLedgerLine, called here while Transfers.GAccountsLock is held (taken at line 32), takes Transfers.GLedgerLock (in Transfers.WriteLedgerLine, Transfers.pas:22), but Transfers.CloseDay takes Transfers.GLedgerLock first and then Transfers.GAccountsLock (Transfers.pas:45) -- two threads can deadlock; take the two locks in one order everywhere
Transfers.pas:45:19 [warning] lock-order-inversion: Lock order inversion: Transfers.GAccountsLock is taken here while Transfers.GLedgerLock is held (taken at line 43), but Transfers.PostPayment takes Transfers.GAccountsLock first and then Transfers.GLedgerLock (Transfers.pas:35) -- two threads can deadlock; take the two locks in one order everywhere
lint-all: 2 finding(s) -- 0 error(s), 2 warning(s), 0 info, 0 hint -- 1 file(s) scanned
Both places get a finding, and each one names the other. The first is on the call WriteLedgerLine(pAmount) at line 35: the lock it takes, the line where the accounts lock was taken, and where CloseDay takes the two the other way. The second is on GAccountsLock.Enter at line 45, taken directly while the ledger lock from line 43 is held. A finding through a call follows called routines up to four calls deep.
Take the two locks in one order everywhere
The message says what to do. PostPayment must take the accounts lock first, because the ledger lock is taken inside a helper it calls. So CloseDay follows the same order: accounts, then ledger, released the other way round.
procedure CloseDay;
begin
GAccountsLock.Enter;
try
GLedgerLock.Enter;
try
GClosedBalance := GBalance;
GLedgerTotal := 0;
finally
GLedgerLock.Leave;
end;
finally
GAccountsLock.Leave;
end;
end;
Step 4. Reindex the folder, because the lint reads the index, then lint again.
drag-lint index . --db fix.sqlite
Done. Files: 1, Symbols: 13, Refs: 55, 0.16s, parse-errors=0
drag-lint lint-all --db fix.sqlite --rule lock-order-inversion
lint-all: scanning 1 .pas file(s)
lint-all: 0 finding(s) -- 0 error(s), 0 warning(s), 0 info, 0 hint -- 1 file(s) scanned
Both findings are gone because the order is now the same in both places, not because anything was hidden. Nothing in the unit carries a suppression marker.
Is the lock held wherever the total is written?
Lock order is one question about locks; another is whether a value is ever written without its lock. The verb happens-before answers it on demand, from the same index. The preset lock-held-when asks whether lock A is held at every site of event B.
drag-lint happens-before --preset lock-held-when --a Transfers.GLedgerLock --b write:Transfers.GLedgerTotal --db fix.sqlite
happens-before A = held:Transfers.GLedgerLock (2 sites) B = write:Transfers.GLedgerTotal (2 sites) preset lock-held-when
verdict YES (proven: A is in force at every site of B) question: is A in force when B runs?
B Transfers.pas:24:5 in Transfers.WriteLedgerLine in-force A is in force here on every path that reaches the site: a site of A ran and nothing can have undone it since
B Transfers.pas:48:7 in Transfers.CloseDay in-force A is in force here on every path that reaches the site: a site of A ran and nothing can have undone it since
limits: callbacks-not-modelled
To see the other answer, a copy of the unit gets one more routine, ResetLedger, that sets GLedgerTotal := 0 without taking the lock. Same question, after a reindex of the copy:
verdict MAY (not proven: A is not shown to be in force at every site of B) question: is A in force when B runs?
B Transfers.pas:25:5 in Transfers.WriteLedgerLine in-force ...
B Transfers.pas:49:7 in Transfers.CloseDay in-force ...
B Transfers.pas:60:3 in Transfers.ResetLedger b-may-run-after-a A has no certain site in this routine: whether it is in force when the routine is entered is not known
The answer is YES, NO or MAY, never a guess dressed as YES: every site it cannot prove is MAY with a reason, here the unguarded write at line 60. The exit code is 0 for YES and 1 for NO or MAY, so a build step can ask the question.
Pairs it cannot prove are the same two locks
The locks are the lock sites of the index: TCriticalSection, TMonitor.Enter, EnterCriticalSection, read-write locks, TMutex, TSpinLock, TThreadList.LockList and project classes that descend from a lock class. Two routines are compared only on a lock known exactly: a field of the routine's own object, a unit-level variable, or the object itself. The rule stays silent when:
- the order is the same everywhere, or the first lock is released before the second is taken;
- a lock is a local variable, a parameter, a field of another object, a property or an expression such as
FLocks[I]; - a lock is taken with a try-enter (
TryEnter,TMonitor.Enterwith a timeout): a failed try does not wait; - the lock is already held where it is taken again;
- a read-write lock is taken for reading at both places (one writer is enough for a finding);
- both orders run under one common third lock, so they cannot interleave.
A deterministic Delphi algorithm, not AI and not manual work
The answer comes from the index drag-lint builds by parsing your source: each Enter and Leave is a lock site, and each call is resolved to the routine it reaches, whose own lock sites the index also holds. The rule builds the order of every pair from those facts. No model is asked, nothing is guessed, and nobody reads the code by hand: the same source and the same index give the same findings on every run.
When the rule is wrong for you
- A field lock is assumed to belong to the same object at both places. Two objects of one class, each locking its own field, are reported as one lock.
- Source order stands in for control flow: a lock released in one branch of an
ifcounts as released from that line on. - A lock taken inside an anonymous method counts under the routine that declares it, although the body may run later on another thread.
- A pair of wrapper routines (
Lock/Unlock) is seen only through the call rule; the stretch the caller holds the lock over is not known. - The rule has no automatic fix. Which order is right is a design decision, and changing it can move work under a lock.
- The run above used a single unit. The index is what lets the rule see through calls and across units, so index the whole project for a real check.