From 069da055c35d857a62531cbc8c2fba5e643547f6 Mon Sep 17 00:00:00 2001 From: Xumin <100666470+StarlightIbuki@users.noreply.github.com> Date: Tue, 20 Feb 2024 12:53:33 +0000 Subject: [PATCH] fix(pluginserver): properly restart messagepack-based instances The bug was introduced when refactoring/cherry-picking. Fix #12364 Co-authored-by: Guilherme Salazar --- .../unreleased/kong/plugin_server_restart.yml | 3 +++ kong/runloop/plugin_servers/mp_rpc.lua | 18 +++++++------- kong/runloop/plugin_servers/pb_rpc.lua | 24 ++++++++----------- 3 files changed, 23 insertions(+), 22 deletions(-) create mode 100644 changelog/unreleased/kong/plugin_server_restart.yml diff --git a/changelog/unreleased/kong/plugin_server_restart.yml b/changelog/unreleased/kong/plugin_server_restart.yml new file mode 100644 index 00000000000..ed46b92bb16 --- /dev/null +++ b/changelog/unreleased/kong/plugin_server_restart.yml @@ -0,0 +1,3 @@ +message: "**Plugin Server**: fix an issue where Kong fails to properly restart MessagePack-based pluginservers (used in Python and Javascript plugins, for example)" +type: bugfix +scope: Core diff --git a/kong/runloop/plugin_servers/mp_rpc.lua b/kong/runloop/plugin_servers/mp_rpc.lua index ebd0943b265..118c3694c05 100644 --- a/kong/runloop/plugin_servers/mp_rpc.lua +++ b/kong/runloop/plugin_servers/mp_rpc.lua @@ -1,5 +1,7 @@ local kong_global = require "kong.global" local cjson = require "cjson.safe" +local _ + local msgpack do msgpack = require "MessagePack" local nil_pack = msgpack.packers["nil"] @@ -326,20 +328,20 @@ end function Rpc:handle_event(plugin_name, conf, phase) - local instance_id = self.get_instance_id(plugin_name, conf) - local _, err = bridge_loop(self, instance_id, phase) + local instance_id, err = self.get_instance_id(plugin_name, conf) + if not err then + _, err = bridge_loop(self, instance_id, phase) + end if err then - local ok, err2 = kong.worker_events.post("plugin_server", "reset_instances", - { plugin_name = plugin_name, conf = conf }) - if not ok then - kong.log.err("failed to post plugin_server reset_instances event: ", err2) - end + local err_lowered = err:lower() - if str_find(err:lower(), "no plugin instance") then + if str_find(err_lowered, "no plugin instance") then + self.reset_instance(plugin_name, conf) kong.log.warn(err) return self:handle_event(plugin_name, conf, phase) end + kong.log.err(err) end end diff --git a/kong/runloop/plugin_servers/pb_rpc.lua b/kong/runloop/plugin_servers/pb_rpc.lua index 8aae88de866..b94aca313ec 100644 --- a/kong/runloop/plugin_servers/pb_rpc.lua +++ b/kong/runloop/plugin_servers/pb_rpc.lua @@ -392,8 +392,8 @@ end function Rpc:handle_event(plugin_name, conf, phase) - local instance_id, res, err - instance_id, err = self.get_instance_id(plugin_name, conf) + local instance_id, err = self.get_instance_id(plugin_name, conf) + local res if not err then res, err = self:call("cmd_handle_event", { instance_id = instance_id, @@ -402,20 +402,16 @@ function Rpc:handle_event(plugin_name, conf, phase) end if not res or res == "" then - if err then - local err_lowered = err and err:lower() or "" - - kong.log.err(err_lowered) + local err_lowered = err and err:lower() or "unknown error" - if err_lowered == "not ready" then - self.reset_instance(plugin_name, conf) - end - if str_find(err_lowered, "no plugin instance") - or str_find(err_lowered, "closed") then - self.reset_instance(plugin_name, conf) - return self:handle_event(plugin_name, conf, phase) - end + if str_find(err_lowered, "no plugin instance", nil, true) + or str_find(err_lowered, "closed", nil, true) then + self.reset_instance(plugin_name, conf) + kong.log.warn(err) + return self:handle_event(plugin_name, conf, phase) end + + kong.log.err(err) end end