PgQue icon indicating copy to clipboard operation
PgQue copied to clipboard

fix(partition-keys): refuse lease release while a batch is open

Open NikolayS opened this issue 4 months ago • 1 comments

Severity

High — correctness / duplicate processing.

Problem

pgque.release_slot() clears the current worker's lease without checking whether the slot subscription still has an open batch. A second worker can immediately claim the slot and receive the same batch while the first worker may still be processing it.

This violates the server-enforced single-processor guarantee in blueprints/partition-keys/SPEC.md and bypasses the intended lease-expiry/takeover fencing window.

Reproduction

select pgque.create_queue('q');
select pgque.subscribe_slot('q', 'c', 0, 1);
select pgque.send('q', 'job', '{}', 'k');
select pgque.force_next_tick('q');
select pgque.ticker();

select pgque.claim_slot('q', 'c', 0, 'worker-a');
select msg_id from pgque.receive_partitioned('q', 'c', 0, 1, 'worker-a', 10);
-- 1

select pgque.release_slot('q', 'c', 0, 'worker-a');
-- true, although the slot batch is still open

select pgque.claim_slot('q', 'c', 0, 'worker-b');
select msg_id from pgque.receive_partitioned('q', 'c', 0, 1, 'worker-b', 10);
-- the same message is returned again

Affected implementation: devel/sql/pgque-api/partition_keys.sql, release_slot.

Expected fix

While holding the slot-row lock, resolve the slot subscription and refuse owner release when sub_batch is not null. Abandoned open batches should recover only through lease expiry/takeover. Add regression coverage proving:

  • release with an open batch is rejected;
  • another worker cannot claim through that attempted release;
  • after ack_partitioned, release succeeds.

NikolayS avatar Jul 10 '26 09:07 NikolayS

Shipped in v0.3.0-rc.2 (merged to main via #395 at f4c7457): https://github.com/NikolayS/PgQue/releases/tag/v0.3.0-rc.2

NikolayS avatar Nov 21 '26 16:11 NikolayS