nic-6443 commented on code in PR #13364:
URL: https://github.com/apache/apisix/pull/13364#discussion_r3225591716


##########
t/core/config_etcd.t:
##########
@@ -567,3 +567,48 @@ passed
 qr/etcd watch timeout, upgrade revision to/
 --- grep_error_log_out eval
 qr/(etcd watch timeout, upgrade revision to\n){2,}/
+
+
+
+=== TEST 15: missing X-Etcd-Index header should not crash init worker
+--- yaml_config
+deployment:
+  role: traditional
+  role_traditional:
+    config_provider: etcd
+  etcd:
+    host:
+      - "http://127.0.0.1:2379";
+    prefix: /apisix
+--- extra_init_by_lua
+    -- Inject a fake loaded_configuration entry with missing X-Etcd-Index
+    -- header so do_run_watch exercises the fallback path.
+    local config_etcd = require("apisix.core.config_etcd")
+    for i = 1, 256 do
+        local name, val = debug.getupvalue(config_etcd.new, i)
+        if not name then
+            break
+        end
+        if name == "loaded_configuration" and type(val) == "table" then
+            val["__test_fake"] = {
+                body = { nodes = {} },
+                headers = {},
+            }
+            break
+        end

Review Comment:
   This is a test — if the upvalue isn't found or next() picks a different 
entry, the test fails (no warning log emitted), which correctly surfaces the 
issue. In practice, loaded_configuration is empty in a fresh test env, so the 
injected entry is the only one.



##########
t/core/config_etcd.t:
##########
@@ -567,3 +567,48 @@ passed
 qr/etcd watch timeout, upgrade revision to/
 --- grep_error_log_out eval
 qr/(etcd watch timeout, upgrade revision to\n){2,}/
+
+
+
+=== TEST 15: missing X-Etcd-Index header should not crash init worker
+--- yaml_config
+deployment:
+  role: traditional
+  role_traditional:
+    config_provider: etcd
+  etcd:
+    host:
+      - "http://127.0.0.1:2379";
+    prefix: /apisix
+--- extra_init_by_lua
+    -- Inject a fake loaded_configuration entry with missing X-Etcd-Index
+    -- header so do_run_watch exercises the fallback path.
+    local config_etcd = require("apisix.core.config_etcd")
+    for i = 1, 256 do
+        local name, val = debug.getupvalue(config_etcd.new, i)
+        if not name then
+            break
+        end

Review Comment:
   This is a test — if the upvalue isn't found or next() picks a different 
entry, the test fails (no warning log emitted), which correctly surfaces the 
issue. In practice, loaded_configuration is empty in a fresh test env, so the 
injected entry is the only one.



##########
apisix/core/config_etcd.lua:
##########
@@ -150,7 +149,11 @@ local function do_run_watch(premature)
             local _, res = next(loaded_configuration)
             if res then
                 rev = tonumber(res.headers["X-Etcd-Index"])
-                assert(rev > 0, 'invalid res.headers["X-Etcd-Index"]')
+                if not rev or rev <= 0 then
+                    log.warn("invalid or missing X-Etcd-Index header, ",
+                             "will fetch revision from etcd directly")

Review Comment:
   The warning only fires once per worker startup — the `watch_ctx.started == 
false` block runs exactly once, then `started` is set to `true` and never 
re-entered. No repeated emission.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to