Fix KV watcher consumer recreation for empty buckets #175
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When a NATS server restarts, the KV watcher's heartbeat task (
hb_task) detects that the consumer is gone and attempts to recreate it. However, the current implementation unconditionally sets:Issue: If the KV bucket is empty or no KV updates have been observed yet,
watcher._sseqremains0. JetStream rejects consumer creation withopt_start_seq=0, returningerr_code=10094("optional start sequence is not set").This causes the watcher to enter an infinite loop of
ConsumerNotFound -> BadRequest (10094)errors, never recovering automatically.Solution
Check if
watcher._sseq > 0before forcing theby_start_sequencedeliver policy:When
_sseqis 0, the originaldeliver_policyfrom the initial subscription is preserved (typicallylast_per_subject), allowing the consumer to be recreated successfully.Testing
Added a new test case
"should reconnect watches on server restart with empty bucket"that:_sseqstays at 0)BadRequest (10094)errors occurReproduction
Without this fix, the following scenario fails:
kv.watchallerror_cb:ConsumerNotFoundBadRequest err_code=10094Impact