This is an automated email from the ASF dual-hosted git repository.

ashishtiwari pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new a6c7f4711 fix: release healthchecker on 0 nodes (#12126)
a6c7f4711 is described below

commit a6c7f4711a6e018428267573f9abe06ba6f8d24c
Author: Ashish Tiwari <[email protected]>
AuthorDate: Fri Apr 11 09:42:15 2025 +0530

    fix: release healthchecker on 0 nodes (#12126)
---
 apisix/upstream.lua               |  4 +++
 t/discovery/reset-healthchecker.t | 55 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+)

diff --git a/apisix/upstream.lua b/apisix/upstream.lua
index c5a8cccaf..f51c1712f 100644
--- a/apisix/upstream.lua
+++ b/apisix/upstream.lua
@@ -82,6 +82,9 @@ _M.set = set_directly
 
 
 local function release_checker(healthcheck_parent)
+    if not healthcheck_parent or not healthcheck_parent.checker then
+        return
+    end
     local checker = healthcheck_parent.checker
     core.log.info("try to release checker: ", tostring(checker))
     checker:delayed_clear(3)
@@ -327,6 +330,7 @@ function _M.set_by_route(route, api_ctx)
 
     local nodes_count = up_conf.nodes and #up_conf.nodes or 0
     if nodes_count == 0 then
+        release_checker(up_conf.parent)
         return HTTP_CODE_UPSTREAM_UNAVAILABLE, "no valid upstream node"
     end
 
diff --git a/t/discovery/reset-healthchecker.t 
b/t/discovery/reset-healthchecker.t
index 1ace8a33b..8612f1fd7 100644
--- a/t/discovery/reset-healthchecker.t
+++ b/t/discovery/reset-healthchecker.t
@@ -112,3 +112,58 @@ ok
 --- timeout: 22
 --- no_error_log
 unhealthy TCP increment (10/30)
+
+
+
+=== TEST 2: Validate healthchecker deletion on node count reduces to 0
+--- http_config
+server {
+    listen 3000 ;
+    location / {
+      return 200 'ok';
+    }
+}
+--- apisix_yaml
+routes:
+  -
+    uris:
+        - /
+    upstream_id: 1
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local discovery = require("apisix.discovery.init").discovery
+            discovery.mock = {
+                nodes = function()
+                    return {
+                        {host = "127.0.0.1", port = 3000, weight = 50},
+                        {host = "127.0.0.1", port = 8000, weight = 50},
+                    }
+                end
+            }
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:"; .. ngx.var.server_port .. "/"
+            local httpc = http.new()
+            local res, err = httpc:request_uri(uri, {method = "GET", keepalive 
= false})
+            ngx.sleep(5)
+            discovery.mock = {
+                nodes = function()
+                    return {
+                    }
+                end
+            }
+            local http = require "resty.http"
+            local uri = "http://127.0.0.1:"; .. ngx.var.server_port .. "/"
+            local httpc = http.new()
+            local res, err = httpc:request_uri(uri, {method = "GET", keepalive 
= false})
+            ngx.status = res.status
+            ngx.sleep(5)
+        }
+    }
+--- request
+GET /t
+--- timeout: 22
+--- no_error_log
+unhealthy TCP increment (10/30)
+--- error_code: 503

Reply via email to