Awesome Reviewers

Reviewer · Concurrency

Safe lock acquisition patterns

When code needs to coordinate multiple locks (whether in-process mutexes or cross-process file locks), design the acquisition/release sequence explicitly to avoid deadlocks and don't rely on behavior that varies across platforms.

Instruction

When code needs to coordinate multiple locks (whether in-process mutexes or cross-process file locks), design the acquisition/release sequence explicitly to avoid deadlocks and don’t rely on behavior that varies across platforms.

Key rules to apply:

  • Never hold one lock while blocking to acquire another. If a MutexGuard (or similar) must be held during fingerprinting/bookkeeping, release it before waiting on a second lock (e.g., a file lock); otherwise a thread can block indefinitely while holding a resource other threads need.
  • Don’t assume lock “upgrade” (shared → exclusive) works uniformly. It can silently succeed on one OS and block/deadlock on another (e.g., Linux allows SH→EX upgrade in-place, Windows does not). Prefer an explicit unlock() followed by a fresh lock() for the stronger mode instead of relying on upgrade semantics, and document why this is safe in context (e.g., “we hold an outer acquisition lock, so no other process can steal the lock between unlock and lock”).
  • When multiple independent actors (processes/threads) can each hold a shared lock and then try to upgrade to exclusive, recognize the classic deadlock: if two holders of a shared lock simultaneously try to escalate, neither can proceed. Mitigate this by introducing a narrow, short-lived coordinating lock (e.g., a dedicated .acquisition-lock) that serializes only the critical section where escalation happens, rather than serializing the entire operation.
  • Prefer integrating lock scheduling into existing control flow instead of introducing side-band/out-of-band signaling between components (e.g., special messages between a job scheduler and job workers) purely to manage lock contention — this complicates maintenance. Where possible, acquire the necessary locks before scheduling/dispatching work, so blocked work can be rescheduled by the existing scheduler rather than needing new coordination channels.
  • Clearly comment the chosen locking strategy at the point of use: why each lock type (shared vs. exclusive, read vs. write) was chosen, the invariant that guarantees it’s deadlock-free (e.g., “we hold the acquisition lock so no other process can steal the lock between unlock and lock”), and any platform caveats.

Example pattern to follow instead of relying on upgrade:

// Unlock before acquiring exclusive, since not all platforms support
// in-place lock upgrade (e.g., Windows blocks on SH -> EX upgrade).
// This is safe here because we hold the outer acquisition lock, so no
// other process can steal this lock between unlock() and lock().
lock_manager.unlock(&lock)?;
lock_manager.lock(&lock)?;

Applying these rules reduces subtle, hard-to-reproduce deadlocks and portability bugs in concurrent/lock-based code.</description> </invoke>