runMatchTick ran on a 1s interval per API instance with no coordination
(flagged in DEVELOPMENT.md/matcher.ts as the blocker before running >1
instance). Two instances racing the same tick could both read the queue
before either removed the matched players, pairing the same players twice
and allocating two separate sessions for one match.
Add lib/lock.ts: a Redis SET NX PX lock with a compare-and-delete release
script, so a lock whose TTL already expired can't be released out from
under a newer holder. runMatchTick now acquires this lock before touching
the queue and releases it in a finally block; a losing instance just skips
that cycle instead of racing.
Tested: unit tests for the lock primitive and the matcher's lock-skip/
acquire/release-on-throw behavior (mocked Redis), plus real-Redis
integration tests (mutual exclusion, release-after-TTL-expiry, stale-holder
can't delete a newer lock) and an end-to-end test proving three concurrent
runMatchTick() calls against live Postgres+Redis pair a queued pair exactly
once with exactly one allocated session.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>