From 0765c92df3577d79d7c9bd5bd9a30bd62d8d73b5 Mon Sep 17 00:00:00 2001 From: Ioannis Karachristos Date: Thu, 19 Nov 2026 10:08:28 +0200 Subject: [PATCH] fix: don't ack a message from before a reconnect on the new channel (DS-3721) Since 5.13.0 a reconnect moves every queue to the new consume channel, and the ack handed to a handler used whatever channel the queue had when it was called. A handler still running across the reconnect therefore acked on the new channel with a delivery tag from the old one. Tags count per channel, so that settled another message or made the broker close the channel with "PRECONDITION_FAILED - unknown delivery tag". onMessage now remembers the channel that delivered the message and skips the ack, with a warning, once the queue has moved to another one. The broker requeued the message when its channel closed. Not changed: an ack on a channel that closed but has not been replaced yet still throws, as before. Co-Authored-By: Claude Fable 5.2 --- README.md | 1 + test/rabbit.test.ts | 54 +++++++++++++++++++++++++++++++++++++++++++++ ts/queue.ts | 19 ++++++++++++---- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ba2402c..8001839 100644 --- a/README.md +++ b/README.md @@ -297,6 +297,7 @@ const rabbit = new Rabbit(process.env.RABBIT_URL, { cancelConsumersOnSigterm: fa ### New in v5.13.x - A reconnect subscribes the queues again on its own. Before, every consumer stayed gone after a reconnect unless the `connected` handler created the queues again, so a process that didn't kept running and reported healthy with no consumers. The new consume channel gets each queue back (declared again, bindings added again, handler subscribed with the queue's own prefetch) before `connected` is emitted. A queue that fails to subscribe is retried like a consumer the broker cancelled. Creating a queue with a handler for a name that already has one on the same channel cancels the old consumer first, so a `connected` handler that creates the queues again doesn't leave two. Pass `resubscribeOnReconnect: false` to turn it off and manage the queues yourself on `connected`, as before. +- v5.13.1: a message that was still being handled when the connection dropped is no longer acked on the new channel. Delivery tags count per channel, so that ack settled another message or made the broker close the channel (`PRECONDITION_FAILED - unknown delivery tag`). The ack is now skipped with a warning; the broker requeued the message when its channel closed. ### New in v5.12.x diff --git a/test/rabbit.test.ts b/test/rabbit.test.ts index c08af38..6f2c470 100644 --- a/test/rabbit.test.ts +++ b/test/rabbit.test.ts @@ -747,6 +747,60 @@ describe('Test rabbit class', function() { (await consumerCount()).should.equal(1); }); + async function waitFor(condition: () => boolean, attempts = 50) { + for (let attempt = 0; attempt < attempts && !condition(); attempt++) { + await new Promise(r => setTimeout(r, 20)); + } + condition().should.be.true(); + } + + // Delivery tags count per channel, and a reconnect moves the queue to a new one. A round trip on the consume + // channel after an ack shows whether the broker accepted it: an unknown tag closes the channel. + it('should not ack a message from before the reconnect on the new channel', async function() { + const acks = []; + rabbit = new Rabbit(this.url, { replyPattern: false, prefetch: 2 }); + reconnectOnDisconnect(); + await rabbit.createQueue(queueName, {}, (msg, ack) => acks.push(ack)); + await waitForConsumers(1); + await rabbit.publish(queueName, 'first'); + await rabbit.publish(queueName, 'second'); + await waitFor(() => acks.length === 2); + await dropConnections([rabbit.consumeConnection, rabbit.publishConnection]); + await waitFor(() => acks.length === 4); + const ackOnNewChannel = sandbox.spy(rabbit.consumeChannel, 'ack'); + + // Both redelivered messages are settled, so tag 2 of the old channel is unknown on the new one. + acks[2](); + acks[3](); + acks[1](); + const { messageCount } = await rabbit.consumeChannel.checkQueue(queueName); + + ackOnNewChannel.calledTwice.should.be.true(); + messageCount.should.equal(0); + }); + + it('should leave the redelivered message to its own ack', async function() { + const acks = []; + rabbit = new Rabbit(this.url, { replyPattern: false }); + reconnectOnDisconnect(); + await rabbit.createQueue(queueName, {}, (msg, ack) => acks.push(ack)); + await waitForConsumers(1); + await rabbit.publish(queueName, 'only'); + await waitFor(() => acks.length === 1); + await dropConnections([rabbit.consumeConnection, rabbit.publishConnection]); + await waitFor(() => acks.length === 2); + const ackOnNewChannel = sandbox.spy(rabbit.consumeChannel, 'ack'); + + // Both deliveries have tag 1: the first run's ack must not settle the second. + acks[0](); + await rabbit.consumeChannel.checkQueue(queueName); + ackOnNewChannel.called.should.be.false(); + + acks[1](); + await rabbit.consumeChannel.checkQueue(queueName); + ackOnNewChannel.calledOnce.should.be.true(); + }); + it('should subscribe again with the queue\'s own prefetch', async function() { rabbit = new Rabbit(this.url, { replyPattern: false, prefetch: 1 }); reconnectOnDisconnect(); diff --git a/ts/queue.ts b/ts/queue.ts index 61acd7e..122a80e 100644 --- a/ts/queue.ts +++ b/ts/queue.ts @@ -243,10 +243,21 @@ export default class Queue { } onMessage(msg: amqp.Message) { + // A reconnect moves the queue to a new channel (rebind) while handlers are still running. Delivery tags count per + // channel, so on the new one this tag is another message's or unknown, and an unknown tag makes the broker close + // the channel. The broker requeued the message when its own channel closed, so there is nothing left to ack. + const channel = this.channel; const ack = () => { - if (!this.options.noAck) { - this.channel.ack(msg); + if (this.options.noAck) { + return; + } + if (channel !== this.channel) { + logger.warn( + `[${msg.properties.correlationId}] Not acking a message of queue ${this.name}: its channel was replaced` + ); + return; } + channel.ack(msg); }; if (!msg) { return; @@ -254,10 +265,10 @@ export default class Queue { // Deliveries can arrive between the consume-ok and an unsubscribe cancelling it; hand them back to the broker. if (!this.handler) { if (!this.options.noAck) { - this.channel.nack(msg, false, true); + channel.nack(msg, false, true); } // A consumer a second subscribe left behind would otherwise keep getting the message back. - this.channel.cancel(msg.fields.consumerTag).catch(() => undefined); + channel.cancel(msg.fields.consumerTag).catch(() => undefined); return; } this.handler(msg, async (error, reply) => {