From fdb69be6070cbc1934b34adc2c3f91305b27d866 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Tue, 22 Dec 2020 18:47:54 +0800 Subject: [PATCH 1/4] fix: the bug that traffic is forwarded to wrong upstream due to ctx being contaminated --- apisix/init.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/apisix/init.lua b/apisix/init.lua index 01dc7729d3c4..1027329ceffb 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -185,6 +185,7 @@ function _M.http_ssl_phase() end ngx_exit(-1) end + ngx.ctx = nil end @@ -334,12 +335,8 @@ end function _M.http_access_phase() local ngx_ctx = ngx.ctx - local api_ctx = ngx_ctx.api_ctx - - if not api_ctx then - api_ctx = core.tablepool.fetch("api_ctx", 0, 32) - ngx_ctx.api_ctx = api_ctx - end + local api_ctx = core.tablepool.fetch("api_ctx", 0, 32) + ngx_ctx.api_ctx = api_ctx core.ctx.set_vars_meta(api_ctx) From 6475721cad67e0c24c39c4ce82b9bd90a93807cc Mon Sep 17 00:00:00 2001 From: nic-chen Date: Tue, 22 Dec 2020 22:43:22 +0800 Subject: [PATCH 2/4] chore: add comment about changes --- apisix/init.lua | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apisix/init.lua b/apisix/init.lua index 1027329ceffb..20d94c881266 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -185,6 +185,8 @@ function _M.http_ssl_phase() end ngx_exit(-1) end + + -- clear the ctx of the ssl phase, avoid affecting other phases ngx.ctx = nil end @@ -335,6 +337,7 @@ end function _M.http_access_phase() local ngx_ctx = ngx.ctx + -- always fetch table from the table pool, we don't need a reused api_ctx local api_ctx = core.tablepool.fetch("api_ctx", 0, 32) ngx_ctx.api_ctx = api_ctx From c12b559ebaa8d0bd27a9d858703ed9748b8aadf2 Mon Sep 17 00:00:00 2001 From: nic-chen Date: Thu, 24 Dec 2020 00:00:07 +0800 Subject: [PATCH 3/4] fix: clear ngx.ctx --- apisix/init.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/apisix/init.lua b/apisix/init.lua index 20d94c881266..19c2e227c3df 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -183,6 +183,8 @@ function _M.http_ssl_phase() if err then core.log.error("failed to fetch ssl config: ", err) end + -- clear the ctx of the ssl phase, avoid affecting other phases + ngx.ctx = nil ngx_exit(-1) end From 7f000047f74811cd4b8521139e6e51f5401b4a5d Mon Sep 17 00:00:00 2001 From: nic-chen Date: Thu, 24 Dec 2020 11:25:52 +0800 Subject: [PATCH 4/4] fix lint --- apisix/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apisix/init.lua b/apisix/init.lua index 19c2e227c3df..38f5f75adce3 100644 --- a/apisix/init.lua +++ b/apisix/init.lua @@ -184,7 +184,7 @@ function _M.http_ssl_phase() core.log.error("failed to fetch ssl config: ", err) end -- clear the ctx of the ssl phase, avoid affecting other phases - ngx.ctx = nil + ngx.ctx = nil ngx_exit(-1) end