Opposing transfers between planetarium lens cabinets stop completing
Level 3 · node · Concurrency · Deadlock from inconsistent acquisition order of multiple resource locks
Scenario
A fictional planetarium maintenance room tracks educational lens counts in each cabinet. A transfer exclusively holds both the source and destination cabinets before changing counts. Individual transfers work, but when two staff members request opposite transfers at the same time, neither finishes. Trace the interaction between the entry point, transfer service, lock queues, and stock store.
Requirements
createWorkshop(initial) creates an independent cabinet store and lock manager. move(from, to, count) receives two distinct registered cabinets and a positive integer quantity, and checks feasibility while holding both cabinets exclusively. It completes with true after moving sufficient stock, or false without changes when stock is insufficient. Concurrent transfers sharing a cabinet must serialize without deadlock while preserving directions and quantities. Disjoint transfers must also complete. Every transfer releases its acquired locks, and read() returns an independent object of current counts. Do not serialize all work behind one global lock.
Acceptance criteria
- Individual transfers and insufficient-stock requests return correct results and maintain consistent cabinet counts.
- Two simultaneous opposing transfers both complete, preserving each request's direction and quantity as well as the total lens count.
- Concurrent transfers spanning multiple cabinets all complete while preserving each requested direction.
- Disjoint transfers and subsequent transfers after insufficient stock complete, and modifying a returned snapshot does not change internal counts.