Skip to content

Commit

Permalink
fix(core): fix an issue where Kong do not handle properly when Python…
Browse files Browse the repository at this point in the history
… and Javascript plugin server crashes

The bug was introduced when refactoring/cherry-picking.

Fix #12364

Co-authored-by: Guilherme Salazar <gsz@acm.org>
  • Loading branch information
StarlightIbuki and gszr committed Feb 20, 2024
1 parent acffb9d commit 3fa6c98
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/plugin_server_restart.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 10 additions & 8 deletions kong/runloop/plugin_servers/mp_rpc.lua
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down Expand Up @@ -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
Expand Down
24 changes: 10 additions & 14 deletions kong/runloop/plugin_servers/pb_rpc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand Down

0 comments on commit 3fa6c98

Please sign in to comment.