Skip to content

fix(watch): do not drop events when a read carries multiple responses#224

Open
AlinsRan wants to merge 1 commit into
masterfrom
fix/read-watch-keep-events
Open

fix(watch): do not drop events when a read carries multiple responses#224
AlinsRan wants to merge 1 commit into
masterfrom
fix/read-watch-keep-events

Conversation

@AlinsRan

Copy link
Copy Markdown

Problem

read_watch() splits one body read into several watch responses, decodes the events of each into all_events, and then attaches all_events to body — which points at the last decoded response. The attach was guarded by #all_events > 1:

if #all_events > 1 then
    body.result.events = all_events
end

So a read holding [response with 1 event, response without events] returns the last response as is, and the event is silently lost. The caller cannot detect it.

Today etcd never produces that layout, because the only event-less responses are created and canceled and neither trails an event response. It does as soon as the watch is created with progress_notify: a notification can be coalesced right behind an event response on the same TCP read.

apache/apisix needs progress_notify to advance start_revision safely (apache/apisix#13067), so this has to be fixed first — otherwise the same class of silent event loss is reintroduced inside the library.

Fix

Attach the events whenever any were collected (> 0).

Also guard on body.result, which is nil when the last response is an error body with an http code below 500. That path is reachable today too and > 1 would already index nil there, so this is a strict improvement rather than a new behaviour.

Compatibility

No behaviour change for anything that exists today:

  • single-chunk reads never reach this line (#chunks == 1 returns early for event-less responses)
  • [notify, event response] order: body already is the event response, so the assignment is a no-op
  • [event, canceled]: both spellings end in the same compacted full-reload path on the apisix side

Test

t/v3/watch.t drives the client against a fake watch endpoint that writes an event response followed by a progress notification in a single body chunk, and asserts the event survives. A real etcd cannot be pushed into producing that coalescing reliably.

Not run locally — CI covers it.

Follow-up

A v1.10.7 release is needed before apache/apisix can pin it and merge its side.

read_watch() splits one body read into several watch responses, decodes the
events of each of them into all_events, and then attaches all_events to
`body`, which points at the last decoded response. The attach was guarded by
`#all_events > 1`, so a read holding [response with 1 event, response without
events] returned the last response as is and the event was lost, silently and
unrecoverably for the caller.

Today etcd never produces that layout because the only event-less responses
are `created` and `canceled`, which never trail an event response. It does as
soon as the watch is created with progress_notify, which apache/apisix needs
to advance start_revision safely (apache/apisix#13067).

Attach the events whenever any were collected. Also guard on body.result,
which is nil when the last response is an error body with an http code below
500 - reachable before this change too, and a nil index in the watch loop.
@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You’ve reached a temporary PR review limit under our Fair Usage Limits Policy.

Your recent review volume is higher than typical usage, so adaptive limits are currently applied.

Next review available in: 53 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f95bc0da-6790-4bec-837d-7374799231ed

📥 Commits

Reviewing files that changed from the base of the PR and between bbcaf8a and c463ff6.

📒 Files selected for processing (2)
  • lib/resty/etcd/v3.lua
  • t/v3/watch.t
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/read-watch-keep-events

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant