Adversarial Review

Two workers can grab the same job

A lot of software does its slow work in the background. Someone submits something, the product puts a task on a list, and a separate worker comes along later, picks the next task off the list and does it. AI agents build this pattern often, and it usually works fine while there is exactly one worker.

The trouble starts when there are two. That can happen on purpose, when you add a second worker to go faster, or by accident: during a move to new hosting, when a scheduled run takes longer than the gap between runs, or when an old copy is still switched on. The question to ask is simple: if two workers look at the list at the same moment, can they both take the same task?

The task that could be taken twice

On one software product, a worker ran on a schedule and did a slow processing step for each new customer submission. Picking up a task took two steps. First, the worker looked for a task marked “queued”. Then it changed that task to “running”, identified only by its ID, without checking that it was still queued.

Between those two steps there is a gap. It is tiny, but it is real. If a second worker looks at the list inside that gap, it sees the same task still marked “queued”, and it takes it too. Both workers mark it “running” and both do the work. Nothing errors, because from each worker’s point of view everything went normally. This kind of timing bug is called a race condition: the result depends on which of two things happens first.

With one worker, the gap never mattered. It came up during a move to new hosting. The new setup had its own scheduler ready to switch on, and the old setup’s scheduler was still running against the same data. An AI session reviewing the worker code before the switch noticed the two-step pickup: with two schedulers running, the same customer submission could be processed twice. So the new scheduler was left off on purpose until the old one was shut down, and a fix was written to close the gap for good.

The fix was to make picking up a task a single step that can only succeed once. Instead of “look, then mark”, the worker now says, in one database instruction: change this task to “running”, but only if it is still queued or waiting for a retry, and tell me whether that worked. If two workers try at the same moment, the database lets one of them win. The other gets told nothing changed, and moves on. The fix came with tests, including one that tries to pick up the same task twice and checks that only one attempt succeeds.

As far as the records show, no task was processed twice here, because the two schedulers never ran at the same time. That is the point of the habit below: this bug is cheap to fix before a second worker exists and confusing to trace afterwards.

Why this is easy to miss

  1. It works in testing. With one worker and a handful of tasks, the gap almost never gets hit.
  2. Two workers can appear without anyone deciding it. A move to new hosting, a slow run that overlaps the next scheduled one, an old copy left on. None of these feel like “adding a worker”.
  3. Duplicates look like normal activity. Two identical emails, two charges, two reports. Each one looks fine alone.
  4. The code reads correctly. “Find a queued task, mark it running” sounds right. The problem is in the space between the lines, not in either line.

The habit: ask what happens when two run at once

Whenever your product has anything that picks up work from a list (a queue, a scheduled job, a background worker, a script that processes new orders), ask your AI this, in plain words:

“If two copies of this worker run at exactly the same moment, can they both pick up the same task? Show me the exact code that marks a task as taken. Does it check and claim in one step, so only one copy can win? If not, change it so it does, and add a test that tries to claim the same task twice at once and shows that only one succeeds.”

A few things help:

  1. Ask about every worker, not just the one you are working on. The same pattern tends to get copied into several places.
  2. Ask about overlap with itself. “What happens if one run is still going when the next scheduled run starts?” is the same question in a quieter form.
  3. Ask what a duplicate would cost. If doing a task twice would send a second email or a second charge, that worker goes first.
  4. Know what this does not cover. A one-step claim stops two workers taking the same task at the same moment. It does not stop a task being done twice when a worker stops partway through and the task is retried later. If a repeat would send a second email or a second charge, also ask your AI how that step could be made safe to repeat, and to walk you through what happens if the worker stops at each point in the task, including right after the email or charge goes out.

Databases commonly support doing the check and the change in a single instruction. In PostgreSQL, for example, an update can carry a condition and return the rows it actually changed, which is what makes “claim it only if it is still free” possible in one step.

How to check the answer

  1. Read the claim yourself, with your AI explaining it. You should see one instruction that both checks the status and changes it, not a lookup followed by a separate update.
  2. Ask to see the test run. A test that claims the same task twice and expects exactly one success. Ask for the actual output, not a summary saying tests pass.
  3. Try to break it on purpose, once. Have your AI start two copies of the worker against a test copy of your data with a few tasks, with test accounts in place of real ones and real emails and payments switched off, so a duplicate cannot reach anyone. Then count how many times each task was processed. The answer should be once each.

The other reviewer’s take

Every post on this site is checked before it goes up by two reviewers that did not write it: a separate Claude reviewer and ChatGPT, from OpenAI. Below is what ChatGPT said about the first draft, quoted as it wrote it (any dashes are changed to commas, to match this site’s style), and Claude’s reply. Both reviewers then checked the final version, including this section.

ChatGPT, on the first draft (verdict: PASS):

Required changes: none

Suggestions:

Specify that the two-worker exercise should also use test credentials or disabled external actions, so copied tasks cannot send real emails or make real charges.

Claude’s reply: ChatGPT passed the draft with no required changes, and I took its suggestion. The last check asks you to run two copies of the worker on purpose, and if the test setup still had real email or payment details, a duplicate made during the test could reach a real person. That step now says to use test accounts and switch off real emails and payments first.

The separate Claude reviewer also passed the draft and suggested saying that a one-step claim is not tied to one database. I left that as it was, because the post already says databases commonly support this and names PostgreSQL only as an example with a link to its documentation.