You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by sh...@apache.org on 2021/06/10 19:10:03 UTC

[apisix] branch master updated: fix: release ctx when the client aborts (#4405)

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

shuyangw 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 4106ef5  fix: release ctx when the client aborts (#4405)
4106ef5 is described below

commit 4106ef5b585c0c981ae518ffdf7ce40677bd4e43
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Fri Jun 11 03:09:51 2021 +0800

    fix: release ctx when the client aborts (#4405)
---
 apisix/init.lua           | 32 ++++++++++++++++++++++++++------
 t/error_page/error_page.t | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 6 deletions(-)

diff --git a/apisix/init.lua b/apisix/init.lua
index 92d3451..6a012cf 100644
--- a/apisix/init.lua
+++ b/apisix/init.lua
@@ -481,7 +481,10 @@ function _M.http_access_phase()
 
     set_upstream_headers(api_ctx, server)
 
-    ngx_var.ctx_ref = ctxdump.stash_ngx_ctx()
+    local ref = ctxdump.stash_ngx_ctx()
+    core.log.info("stash ngx ctx: ", ref)
+    ngx_var.ctx_ref = ref
+
     local up_scheme = api_ctx.upstream_scheme
     if up_scheme == "grpcs" or up_scheme == "grpc" then
         return ngx.exec("@grpc_pass")
@@ -493,15 +496,22 @@ function _M.http_access_phase()
 end
 
 
-function _M.dubbo_access_phase()
-    ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
+local function fetch_ctx()
+    local ref = ngx_var.ctx_ref
+    core.log.info("fetch ngx ctx: ", ref)
+    local ctx = ctxdump.apply_ngx_ctx(ref)
     ngx_var.ctx_ref = ''
+    return ctx
+end
+
+
+function _M.dubbo_access_phase()
+    ngx.ctx = fetch_ctx()
 end
 
 
 function _M.grpc_access_phase()
-    ngx.ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
-    ngx_var.ctx_ref = ''
+    ngx.ctx = fetch_ctx()
 
     local api_ctx = ngx.ctx.api_ctx
     if not api_ctx then
@@ -543,7 +553,7 @@ end
 function _M.http_header_filter_phase()
     if ngx_var.ctx_ref ~= '' then
         -- prevent for the table leak
-        local stash_ctx = ctxdump.apply_ngx_ctx(ngx_var.ctx_ref)
+        local stash_ctx = fetch_ctx()
 
         -- internal redirect, so we should apply the ctx
         if ngx_var.from_error_page == "true" then
@@ -636,6 +646,16 @@ end
 
 
 function _M.http_log_phase()
+    if ngx_var.ctx_ref ~= '' then
+        -- prevent for the table leak
+        local stash_ctx = fetch_ctx()
+
+        -- internal redirect, so we should apply the ctx
+        if ngx_var.from_error_page == "true" then
+            ngx.ctx = stash_ctx
+        end
+    end
+
     local api_ctx = common_phase("log")
     if not api_ctx then
         return
diff --git a/t/error_page/error_page.t b/t/error_page/error_page.t
index 6f154c1..6eb0fa7 100644
--- a/t/error_page/error_page.t
+++ b/t/error_page/error_page.t
@@ -139,3 +139,50 @@ GET /t
 [delete] code: 200 message: passed
 --- no_error_log
 [error]
+
+
+
+=== TEST 8: set route which upstream is blocking
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/routes/1',
+                 ngx.HTTP_PUT,
+                 [[{
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/mysleep"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 9: client abort
+--- request
+GET /mysleep?seconds=3
+--- abort
+--- timeout: 0.5
+--- ignore_response
+--- grep_error_log eval
+qr/(stash|fetch) ngx ctx/
+--- grep_error_log_out
+stash ngx ctx
+fetch ngx ctx