diff --git a/kong/runloop/plugin_servers/init.lua b/kong/runloop/plugin_servers/init.lua index e660a6561c52..088f5281118e 100644 --- a/kong/runloop/plugin_servers/init.lua +++ b/kong/runloop/plugin_servers/init.lua @@ -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 @@ -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 @@ -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