Code Room
Vibe codingHardvc-g187
Subject Ai code reviewLevel Senior–Staff~22 minCommon in Concurrency · Distributed systems interviewsIndustries Software development, Technology

Question

You asked an AI for a distributed lock in Node using Redis so only one worker runs a daily reconciliation. It produced this and called it safe mutual exclusion. Review it.

typescript
async function withLock(key: string, fn: () => Promise<void>) {  const token = crypto.randomUUID();  const ok = await redis.set(key, token, 'NX', 'EX', 30);  if (!ok) return;                      // someone else holds it  try {    await fn();                          // reconciliation can take >30s  } finally {    await redis.del(key);                // release  }}

Where does mutual exclusion break, and what's the correct approach?

What a strong answer looks like

Treat the AI’s output as a draft to verify, not an answer to trust. Name the specific flaw and the input that triggers it, say how you’d catch it — tests, edge cases, reading critically — and how you’d re-prompt or decompose to get it right.

Describe your solution

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.