sidequest icon indicating copy to clipboard operation
sidequest copied to clipboard

Stale-job recovery can overwrite a newer execution and resurrect completed jobs

Open obckelbley opened this issue 3 months ago • 0 comments

Summary

In Sidequest 1.16.2, stale-job recovery can persist a transition computed from an obsolete job snapshot after another worker or executor has already changed that job. The stale write is matched only by job id, so it can overwrite the newer lifecycle state.

This can resurrect a completed job, replace a newer running attempt with the older attempt's retry state, or release a job that has already been re-claimed. The inverse race is also possible: after recovery makes a stale job available and another worker starts it, the old executor can still persist completion or failure over that newer execution.

Affected code

Observed on tag v1.16.2 (c42c6a63d5061f11ef5496456e72317ca9c6719b).

The race is in the shared lifecycle path used by the built-in backends:

  1. Backend.staleJobs() returns job snapshots.
  2. releaseStaleJobs() computes a retry/release from each returned snapshot.
  3. JobTransitioner.apply() persists through Backend.updateJob().
  4. The built-in update implementations identify the row/document by id, without checking that the execution observed in step 1 is still current.

There is no atomic precondition spanning the snapshot used to compute the transition and the update that persists it.

Deterministic reproduction

The race can be reproduced without sleeps by pausing recovery after the stale snapshot is obtained, committing a competing transition, and then returning the old snapshot to the sweep:

vi.spyOn(backend, "staleJobs").mockImplementationOnce(async () => {
  await backend.updateJob({
    ...staleSnapshot,
    state: "completed",
    completed_at: completedAt,
    result: { ok: true },
  });
  return [{ ...staleSnapshot }];
});

await releaseStaleJobs(backend, 1, 1);

On the unmodified v1.16.2 implementation, two focused regression tests fail deterministically:

does not resurrect a job completed after the stale scan
  expected: completed, completed_at retained, result retained
  actual:   waiting, completed_at/result overwritten by the stale running snapshot

does not overwrite a newer execution of the same running job
  expected: running, attempt 2, worker-b
  actual:   waiting, attempt 1, worker-a

The second case is important because checking only state = 'running' would not fix the race. Both the stale snapshot and the newer execution can be running; the attempt, owner, and execution timestamps distinguish them.

Expected behavior

A lifecycle transition should be persisted only if the exact execution snapshot on which it was based is still current. Losing that compare-and-update race during a stale sweep should be treated as a benign no-op; genuine backend errors should still propagate.

An old executor should likewise be unable to complete, fail, retry, snooze, or cancel a newer execution of the same job.

Impact

  • A successfully completed job can become eligible for delivery again.
  • A newer worker's ownership and attempt number can be overwritten.
  • Results, errors, and lifecycle timestamps from the winning execution can be replaced by stale data.
  • Side effects performed by the job are not rolled back, so replay can duplicate work even though the queue row appears internally consistent afterward.

This requires a narrow timing window, but normal worker completion and periodic stale recovery provide the competing operations in production. The impact is therefore correctness and possible duplicate execution, not merely inaccurate monitoring state.

Relationship to #155 / #157

#155 reported that stale recovery could bypass max_attempts. PR #157 fixed that by routing stale running jobs through JobTransitioner.

This is a separate concurrency defect. JobTransitioner in v1.16.2 still writes the old snapshot through an unconditional-by-id backend update, so using it fixes retry policy but does not fence the stale transition.

Proposed direction

Add an atomic backend compare-and-update operation and use it for lifecycle transitions. A no-schema-change execution fingerprint can be formed from:

  • state
  • attempt
  • claimed_by
  • claimed_at
  • attempted_at

The write must match id plus the complete fingerprint in one database operation. The precondition must cover every JobTransitioner write, not only stale recovery, to close both directions of the race.

Built-in backend conformance tests should cover:

  • a matching conditional update;
  • a matching no-op update (notably MySQL matched-row behavior);
  • rejection when a newer execution has the same state;
  • completed-after-scan resurrection;
  • claimed-after-scan release;
  • a stale executor attempting to complete a newer execution; and
  • propagation of actual backend failures.

No database migration is required for this approach. It does add a required method to the public Backend interface, so custom backend implementations would need to implement the same atomic compare-and-update guarantee.

obckelbley avatar Aug 26 '26 02:08 obckelbley