All problems

Later dye sample inspections overtake a queued revision

Level 3 · node · Concurrency · Reader-writer lock violates queue priority

Scenario

In a fictional dye workshop, several workers can inspect the current color of a sample board at the same time, but no other operation may run during a revision. When inspections take longer, a revision request enters the queue. However, inspections requested afterwards can start first and receive the previous color. Trace the studio entry point, board service, and the immediate admission and queue processing paths of the asynchronous lock together.

Requirements

createStudio(initial) exposes inspect(visit) and revise(next, prepare), both returning promises. inspect passes the current color to visit and returns that color after the callback completes. revise passes the current color to prepare, changes it to next only after callback success, and returns next. Multiple inspections may run concurrently, but revisions run exclusively. Once a queue exists, new requests must not be admitted ahead of existing requests. Consecutive inspections at the front of the queue may be admitted together. Propagate callback failures to the caller and release the lock. Preserve the public interface.

Acceptance criteria

  • With no revision waiting, multiple inspections run concurrently and return the same current color.
  • When a revision is waiting, subsequent inspections receive the updated color after that revision completes.
  • Consecutive queued inspections run together; the following revision and later inspections preserve arrival order.
  • A failed revision preparation callback preserves the previous color and lets the next request run normally.