OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug]: Memory and SQLite QueueFS settle a redelivered message on a stale ACK, releasing the serial successor early

Open PoisonGodv opened this issue 1 week ago • 3 comments

Issue Origin

Observed or reproduced in a real environment. Reproduced with Rust unit tests driving the real Memory and SQLite backends on current main (e6dc97146). Related to #8181: the same settlement-scoping gap exists in the two backends that open PR #8184 leaves on raw message IDs.

Bug Description

After a delivery is settled with RETRY and the message is dequeued again, a late ACK belonging to the earlier delivery still settles the message. The next message with the same serial_key then becomes eligible while the current delivery is still in flight, breaking the documented guarantee that later messages of a serial_key remain blocked until the original is ACKed.

QueueBackend::ack/retry settle by message ID alone, so the backends cannot tell a current ACK from a stale one:

  • MemoryBackend::ack removes the message from processing and clears its retry_serial entry (crates/ragfs/src/plugins/queuefs/backend.rs).
  • SQLiteQueueBackend::ack runs DELETE ... WHERE message_id = ? AND status = 'processing', which matches the redelivered row.

Steps to Reproduce

With two messages m1, m2 sharing serial_key = "k", on either backend:

  1. dequeue returns m1 (delivery 1).
  2. retry("m1") settles delivery 1 and requeues m1.
  3. dequeue returns m1 again (delivery 2).
  4. A late ack("m1") from delivery 1 arrives.
  5. dequeue again.

Unit tests driving exactly this sequence on the real backends print:

[memory] stale ack returned: true
[memory] next dequeue after stale ack: Some("m2")
[sqlite] stale ack returned: true
[sqlite] next dequeue after stale ack: Some("m2")

Expected Behavior

The late ACK is a duplicate settlement for an already-settled delivery: it should not change queue state, and m2 should stay blocked until the current delivery of m1 is ACKed. This matches the lifecycle doc ("Later messages of a serial_key remain blocked until the original is ACKed", "Duplicate ACKs do not affect other claims") and the delivery-scoped settlement approach proposed for Redis in open PR #8184: only settlement for the current delivery changes queue state.

Actual Behavior

The stale ACK returns true and fully settles m1, so m2 is delivered while delivery 2 of m1 is still being processed. Two messages of one serial_key can be with consumers at once.

OpenViking Version

e6dc97146 (main).

Python Version

N/A (Rust backend unit tests).

Operating System

macOS.

Additional Context

This sequence requires a consumer that retries or recovers delivery 1 and dequeues it again while delivery 1 can still emit an ACK; the standard NamedQueue path settles after its handler returns, so this is a backend/protocol-contract reproduction rather than a demonstrated race in that settlement path.

Settlement by message ID cannot distinguish the two ACKs, so closing this needs a caller-visible delivery receipt on these backends too, along the lines open PR #8184 proposes for Redis. If the intended contract is instead that memory/SQLite rely on consumer discipline, a note in the lifecycle doc would also resolve the ambiguity. Happy to implement whichever direction you prefer.

PoisonGodv avatar Nov 22 '26 13:11 PoisonGodv

Thanks @PoisonGodv for a sharp write-up — I independently traced the same settlement-scoping gap while reading through the QueueFS backends, and your point is exactly right: #8184 introduces delivery_receipt validation for Redis, but Memory (crates/ragfs/src/plugins/queuefs/backend.rs, ack ~L430-447) and SQLite (same file, ~L983-999) still settle on bare message_id, so a late ACK from a stale delivery can settle the current one.

I'm preparing an RFC that lifts #8184's receipt idea into a unified, trait-level contract across all three backends — per-delivery receipts, conditional ACK/RETRY settlement, serial-slot release only on current-delivery settlement, plus a cross-backend conformance test matrix (identical delivery/retry/stale-ACK scenarios on Memory/SQLite/Redis). That would close this issue and the #8181 serial-key early-release family in one stroke, instead of three divergent per-backend fixes.

Will post the RFC shortly and cross-link both threads. One question I'd welcome input on: would maintainers prefer this as a QueueBackend trait extension, or as a middleware-enforced invariant?

tanbopiao avatar Nov 23 '26 04:11 tanbopiao

A unified contract sounds right to me — Memory and SQLite show the same failure mode, and #8184 is already addressing the corresponding Redis path.

Happy to take the Memory/SQLite side once the RFC lands: receipt-scoped ack/retry in both backends, plus the stale-ACK scenarios from this issue turned into shared conformance cases. The retry → redeliver → late-ACK sequence above should cover the serial-slot release assertion too.

On trait vs middleware: the receipt has to be checked where settlement happens. Memory/SQLite settle inside the backend, so middleware-only enforcement would need a separate delivery ledger kept in sync with the backends' settlement state. Trait-level looks like the smaller and more enforceable change to me.

PoisonGodv avatar Nov 23 '26 06:11 PoisonGodv

Thanks @PoisonGodv — glad the failure mode lines up across backends.

Agreed on trait-level: settlement happens inside each backend, so the receipt check belongs at the same layer. A middleware-only ledger would reintroduce a second source of truth that has to stay in sync with the backends' own settlement state.

I've opened #8232 to draft the unified delivery-settlement contract (receipt-scoped ack/retry, serial-slot release, stale-ACK semantics). It's early on purpose — the stale-ACK scenarios from this issue are exactly the kind of shared conformance cases worth pinning down across Memory/SQLite/Redis. Would be great to have you co-drive the Memory/SQLite side there; I'll fold the retry → redeliver → late-ACK sequence into the RFC's conformance matrix.

tanbopiao avatar Nov 23 '26 06:11 tanbopiao