Skip to content

Commit

Permalink
fix(plugin-server) clone ngx.ctx to prevent a race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed Jul 6, 2022
1 parent c42ae9c commit 5695255
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions kong/runloop/plugin_servers/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

local proc_mgmt = require "kong.runloop.plugin_servers.process"
local cjson = require "cjson.safe"
local clone = require "table.clone"
local ngx_ssl = require "ngx.ssl"
local SIGTERM = 15

Expand All @@ -9,6 +10,7 @@ local kong = kong
local ngx_var = ngx.var
local coroutine_running = coroutine.running
local get_plugin_info = proc_mgmt.get_plugin_info
local get_ctx_table = require("resty.core.ctx").get_ctx_table
local subsystem = ngx.config.subsystem

--- keep request data a bit longer, into the log timer
Expand Down Expand Up @@ -240,29 +242,24 @@ local function build_phases(plugin)
for _, phase in ipairs(plugin.phases) do
if phase == "log" then
plugin[phase] = function(self, conf)
local saved = {
_G.native_timer_at(0, function(premature, saved)
if premature then
return
end
get_ctx_table(saved.ngx_ctx)
local co = coroutine_running()
save_for_later[co] = saved
server_rpc:handle_event(self.name, conf, phase)
save_for_later[co] = nil
end, {
plugin_name = self.name,
serialize_data = kong.log.serialize(),
ngx_ctx = ngx.ctx,
ngx_ctx = clone(ngx.ctx),
ctx_shared = kong.ctx.shared,
request_headers = subsystem == "http" and ngx.req.get_headers(100) or nil,
response_headers = subsystem == "http" and ngx.resp.get_headers(100) or nil,
response_status = ngx.status,
}

_G.native_timer_at(0, function()
local co = coroutine_running()
save_for_later[co] = saved

-- recover KONG_PHASE so check phase works properly
-- for functions not supported by log phase
if ngx.ctx then
ngx.ctx.KONG_PHASE = saved.ngx_ctx.KONG_PHASE
end
server_rpc:handle_event(self.name, conf, phase)

save_for_later[co] = nil
end)
})
end

else
Expand Down

0 comments on commit 5695255

Please sign in to comment.