shashank created CAMEL-27358:
--------------------------------
Summary: camel-nats - the JetStream push consumer can skip
messages after it subscribes again: old-stream messages still dispatched after
the stream was created again, and an unsettled message forgotten before the new
consumer delivered it
Key: CAMEL-27358
URL: https://issues.apache.org/jira/browse/CAMEL-27358
Project: Camel
Issue Type: Bug
Components: camel-nats
Reporter: shashank
Assignee: shashank
h3. What happens
When the server deleted the ephemeral consumer of a JetStream push
subscription, the consumer subscribes again from the first message it delivered
and that is not settled, or after the highest one delivered (CAMEL-25845). Two
cases make it start too late, so messages are skipped without any error:
* *The stream was created again* (for example a memory stream after a server
restart). The keeper clears what it tracked, but jnats keeps the queue of its
dispatcher across a reconnect and keeps dispatching the messages of the old
subscription until it is unsubscribed. Each old-stream message that is
dispatched after the clear (while the keeper works out the start, or while it
waits for the server to create the new consumer) sets the highest delivered
sequence back to an old-stream sequence. The new consumer then starts at that
sequence, or, more often, the next consumer loss (an ordinary disconnect of
more than 5 s) resumes after it, and every new-stream message below it is
skipped. On a busy stream that can be a large range.
* *An unsettled message is forgotten too early.* Right after the new subscribe
returns, the keeper drops every tracked message of the old subscription ("the
new consumer delivers those again"), without waiting for that delivery. If the
new consumer is lost too before its copy reached the route (a second
disconnect, its copy still queued and then dropped with its subscription), and
the first delivery fails (its nak goes to the deleted consumer), the next
resubscribe starts after that message.
h3. How to reproduce
Push consumer without {{durableName}}, memory stream; a slow route with
messages queued in the dispatcher; restart the server and create the stream
again (as an application that defines its memory streams does at its start),
then disconnect for more than 5 s once more: the new consumer is created with a
start sequence above the new-stream messages.
Found with a TLA+ model of the push consumer keeper.
h3. Fix
The dispatcher thread tracks a delivered message under a small lock, and the
resubscribe clears the tracking under the same lock when the stream was created
again; it also remembers the subscription of the old stream, whose messages are
no longer tracked. A message of the previous subscription that is not settled
stays tracked until the new consumer delivers it again, or delivers a later
sequence (it delivers each message for the first time in stream order, so the
message is no longer in the stream).
_Filed with Claude Code on behalf of allthingssecurity._
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)