Repository navigation
fix(consul): run the catalog and health watchers as independent timers - #14032
Merged
Merged
Conversation
connect() spawned the catalog and health watchers as light threads, waited for the first one and killed both. resty.consul reads the response body through resty.http, which does that in a child coroutine. When the killed watcher was reading a body, ngx.thread.kill does not cancel that child's pending socket read; its completion later resumes connect() at an unrelated yield point, e.g. the receive of the following catalog fetch, which then fails with "bad argument #1 to 'str_sub' (string expected, got boolean)". Each index now has its own watcher timer, and one run is one blocking query, so nothing is killed and every run frees its memory when it ends. The watcher that sees a change runs the round itself: validate the result, then fetch all services, backing off on errors as before. Rounds never overlap: a watcher that sees a change while another round runs parks, and is re-armed together with the round's watcher when it ends, so it queries again against the updated indexes.
4 of 5 tasks
There was a problem hiding this comment.
Warning
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Copilot review overview
5 open findings
watch_result_is_validreturns false both for error indexes (index <= 0) and for 'no longer new'… · Newwatch_catalog/watch_healthnow conceptually run 'one blocking query per timer run', but they… · New The PR description says: 'if a timer can't be created, only that watcher stops instead of the whole… · New This test relies on several wall-clock sleeps (mock server timing +ngx.sleep(6)) but only allows… · New This test relies on several wall-clock sleeps (mock server timing +ngx.sleep(6)) but only allows… · New
What changed in this PR
Fixes a Consul discovery race where killing one watcher mid-body-read can corrupt subsequent fetches, by running catalog/health watches as independent timer-driven blocking queries and avoiding ngx.thread.kill.
Changes:
- Reworked Consul discovery loop to use two independent timer-based watchers that serialize fetch “rounds” (
in_round+ parking). - Updated Consul client watchers (
watch_catalog/watch_health) to perform a single blocking query per run and return “no change” asnil. - Added an integration test reproducing the prior race (
consul-watch-race.t).
| File | Description |
|---|---|
| t/discovery/consul-watch-race.t | Adds a regression test that reproduces the watcher-kill/body-read race and asserts it no longer breaks fetches. |
| apisix/discovery/consul/init.lua | Refactors the polling loop to two timer-driven watchers and introduces “round” serialization/parking logic. |
| apisix/discovery/consul/client.lua | Changes watchers to run a single blocking query and return nil when indexes don’t change. |
🧠 Review effort: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Copilot stopped reviewing on behalf of
nic-6443 due to an error
October 13, 2026 09:30
AlinsRan
approved these changes
Oct 14, 2026
membphis
approved these changes
Oct 14, 2026
shreemaan-abhishek
approved these changes
Oct 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


Description
Consul discovery can fail a whole fetch round with a bogus resty.http error when the catalog and health indexes change together. This PR stops killing the watchers, which is what causes it.
connect()spawns the catalog and health watchers as light threads, waits for the first one and kills both. The watchers go throughresty.consul, which reads the response body via resty.http, and resty.http reads a body in a child coroutine. If the watcher that gets killed is still reading its body (typically the health watch streaming a large/v1/health/state/anyresponse while the catalog watch returns first),ngx.thread.killdoesn't cancel the read pending in that child coroutine. When it completes, it resumesconnect()at whatever it's waiting on, usually the status-line receive of the next catalog fetch, and that fetch fails with:Now each index has its own watcher timer, and one run is one blocking query, so nothing gets killed and every run frees its memory when it ends. The watcher that sees a change runs the round itself: validate, fetch, back off on errors, same code as before. Rounds don't overlap: a watcher that sees a change while another round is running parks, and gets re-armed together with the round's watcher when it ends, so it queries again against the updated indexes and only triggers another fetch if something is still new.
watch_catalog/watch_healthnow run a single query and return nothing when the index didn't change; the retry moved to the timer, sostop_flagand worker exit are checked between queries.What stays the same: indexes are only committed after a successful fetch, the retry/backoff progression is still one per server, and without keepalive the rounds are still paced by
fetch_interval. What changes: a watcher that didn't report a change keeps its blocking query open across rounds instead of being killed and restarted; each server runs two timers instead of one timer with two light threads; and if a timer can't be created, only that watcher stops instead of the whole loop.t/discovery/consul-watch-race.treproduces the race with a mock Consul that bumps both indexes together, answers the catalog watch shortly after the health watch starts streaming a slow chunked body, and delays the non-blocking catalog read so the health body finishes while the fetch is waiting. It fails on master with thegot booleanerror and a 503, and passes with this change.Which issue(s) this PR fixes:
N/A
Checklist