Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: release ctx when the client aborts #4405

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions apisix/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions t/error_page/error_page.t
Original file line number Diff line number Diff line change
Expand Up @@ -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