Repository navigation
Description
What version of Effect is running?
effect 4.0.5, @effect/platform-node 4.0.5, redis (node-redis) 6.3.0, Redis 8.2.2, Node 24.21.0. PersistedQueue.ts is unchanged on main.
What steps can reproduce the bug?
In the Redis store, settling a claim (complete, retry, requeue on interrupt, fail) deletes the element's lock and pending entry unconditionally, and the lock key is ${prefix}${id}:lock, which isn't scoped to the queue. So a settlement can release a different, still-running claim on the same id, and the element is delivered again while that claim is processing it. Two ways to hit it:
1. Two queues with the same custom id. No lock has to expire (default lockExpiration):
import { NodeRedis, NodeRuntime } from "@effect/platform-node"
import { Deferred, Effect, Fiber, Layer, Schema } from "effect"
import { PersistedQueue } from "effect/persistence"
const Job = Schema.Struct({ n: Schema.Number })
const program = Effect.gen(function*() {
const emails = yield* PersistedQueue.make({ name: "emails", schema: Job })
const invoices = yield* PersistedQueue.make({ name: "invoices", schema: Job })
yield* emails.offer({ n: 1 }, { id: "order-1" })
yield* invoices.offer({ n: 2 }, { id: "order-1" })
// worker 1 is still processing invoices/order-1
const started = yield* Deferred.make<void>()
const worker1 = yield* invoices.take(() => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)))
.pipe(Effect.forkChild)
yield* Deferred.await(started)
// emails/order-1 completes
yield* emails.take(() => Effect.void)
// worker 2 receives invoices/order-1 while worker 1 is still processing it
const again = yield* invoices.take((_, meta) => Effect.succeed(meta))
console.log("redelivered while in flight:", again)
yield* Fiber.interrupt(worker1)
})
NodeRuntime.runMain(program.pipe(
Effect.provide(PersistedQueue.layer.pipe(
Layer.provide(PersistedQueue.layerStoreRedis({ lockRefreshInterval: "1 second" })),
Layer.provide(NodeRedis.layer({ url: process.env.REDIS_URL }))
))
))2. A worker whose lock expired (event-loop stall, process pause, or Redis unreachable for longer than lockExpiration) and was taken over by another worker, and whose handler then fails or is interrupted. Worker A was paused with SIGSTOP past lockExpiration (1s), worker B took the element over, then A was resumed and its handler failed:
B processing attempts=2
state before A settles: lock=<B's worker id> pending=1 attempts=2
A resumed; A settled (outcome=fail)
state after A settled: lock=(none) pending=0 attempts=2
C processing attempts=3 while B is still processing job-1
The same sequence on the SQL store leaves B's claim intact, and C gets nothing.
Test for case 2 (NodeRedis.integration.test.ts; fails on main, passes when ported to the SQLite store)
for (const outcome of ["failure", "interruption"] as const) {
it.effect(
`${outcome}: a worker whose lock expired does not release an element another worker took over`,
() =>
Effect.gen(function*() {
const prefix = `effectq-stale-${outcome}:`
const makeQueue = Effect.fnUntraced(function*(lockRefreshInterval: Duration.Input) {
const store = yield* PersistedQueue.makeStoreRedis({
prefix,
pollInterval: "50 millis",
lockRefreshInterval,
lockExpiration: "1 second"
})
const factory = yield* PersistedQueue.makeFactory.pipe(
Effect.provideService(PersistedQueue.PersistedQueueStore, store)
)
return yield* factory.make({ name: "stale-owner", schema: RedisItem, retrySchedule: Schedule.spaced(0) })
})
// worker A never refreshes its lock, like a worker that stalls for
// longer than lockExpiration
const a = yield* makeQueue("1 hour")
const b = yield* makeQueue("100 millis")
const c = yield* makeQueue("100 millis")
yield* a.offer({ n: 1 })
const aTaken = yield* Deferred.make<void>()
const aRelease = yield* Deferred.make<void>()
const aFiber = yield* a.take(() =>
Deferred.succeed(aTaken, undefined).pipe(
Effect.andThen(Deferred.await(aRelease)),
Effect.andThen(Effect.fail("boom"))
)
).pipe(Effect.forkScoped)
yield* Deferred.await(aTaken)
// A's lock expires and B takes the element over
const bTaken = yield* Deferred.make<number>()
yield* b.take((_, { attempts }) => Deferred.succeed(bTaken, attempts).pipe(Effect.andThen(Effect.never)))
.pipe(
Effect.forkScoped
)
assert.strictEqual(yield* Deferred.await(bTaken), 2)
// A's handler ends while B is still processing the element
if (outcome === "failure") {
yield* Deferred.succeed(aRelease, undefined)
yield* Fiber.await(aFiber)
} else {
yield* Fiber.interrupt(aFiber)
}
const taken = yield* c.take((_, { attempts }) => Effect.succeed(attempts)).pipe(
Effect.timeoutOption("1 second")
)
assert.deepStrictEqual(taken, Option.none())
}).pipe(TestClock.withLive),
{ timeout: 20000 }
)
}What is the expected behavior?
While a claim is live, its element is not handed to another worker. A settlement only affects the claim that is settling. This is what the SQL store does, and what does not redeliver in-flight elements in the shared suite expects. Queues with the same custom id stay independent (#7006). At-least-once delivery covers a crash before the ack, not a live claim released by another claim.
What do you see instead?
- Case 1 prints
redelivered while in flight: { id: 'order-1', attempts: 2 }within onelockRefreshInterval(30s by default). Withorder-2as the invoices id, the second take gets nothing within 5s. - Case 2: C receives the element (attempt 3) while B is still processing it. If A is interrupted instead, the requeue pushes the element straight back and decrements
attempts, so C runs at once and is told attempt 2, the same number B has.
Additional information
At 4.0.5 (7f524e2):
-
The lock key has no queue name: L731. The other per-queue keys, including
:ids, are scoped by name. -
The take script stores
worker_idas the lock value (L1277), but no script reads it. -
The settlement scripts
DELthe lock andHDELthe pending entry unconditionally:- requeue: L1146-L1148, which also decrements
attempts; - complete: L1170;
- retry: L1192;
- failed: L1222.
In case 1 the reset then finds the other queue's pending entry unlocked and requeues it (L1109, L1123). In case 2 the stale retry or requeue puts the element back itself (L1194, L1152).
- requeue: L1146-L1148, which also decrements
-
The SQL store guards refresh and every settlement with
acquired_by = workerId: L1492, L1515, L1527, L1539, L1549. Removing that predicate fromretryandinterruptmakes the SQLite port of the test above fail the same way.
Fix idea: put the queue name in the lock key, and have the settlement and refresh scripts act only while the lock still holds the claimant's value. Both are needed. An owner check alone doesn't cover case 1, because one worker can hold both claims.
I have failing tests for both cases and can open a PR with them. Case 1 is a shared-suite test that fails on Redis and passes on memory and SQLite. Case 2 is the test above, which fails on Redis and passes on its SQLite port.
Activity