From 1442cdd98cbcfeb725d75465e4473b14eb882211 Mon Sep 17 00:00:00 2001 From: samugi Date: Tue, 6 Sep 2022 13:06:46 +0200 Subject: [PATCH] fix(external-plugins): handle failing instance creation This is a fix for https://github.com/Kong/kong/issues/9301 when a request made the plugin fail, the creation of a new instance was not handling a failed connection properly. Upon failure to connect to the socket, it was possible to end up in a state where the instance existed without an instance id, causing an infinite loop and high CPU utilization. --- kong/runloop/plugin_servers/mp_rpc.lua | 2 +- kong/runloop/plugin_servers/pb_rpc.lua | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/kong/runloop/plugin_servers/mp_rpc.lua b/kong/runloop/plugin_servers/mp_rpc.lua index 35db3ca1581..9f8c9b60857 100644 --- a/kong/runloop/plugin_servers/mp_rpc.lua +++ b/kong/runloop/plugin_servers/mp_rpc.lua @@ -255,7 +255,7 @@ function Rpc:call_start_instance(plugin_name, conf) }) if status == nil then - return err + return nil, err end return { diff --git a/kong/runloop/plugin_servers/pb_rpc.lua b/kong/runloop/plugin_servers/pb_rpc.lua index 52859a9b891..0012fcc82c7 100644 --- a/kong/runloop/plugin_servers/pb_rpc.lua +++ b/kong/runloop/plugin_servers/pb_rpc.lua @@ -298,7 +298,11 @@ end function Rpc:call(method, data, do_bridge_loop) self.msg_id = self.msg_id + 1 local msg_id = self.msg_id - local c = assert(ngx.socket.connect("unix:" .. self.socket_path)) + local c, err = ngx.socket.connect("unix:" .. self.socket_path) + if not c then + kong.log.err("trying to connect: ", err) + return nil, err + end msg_id = msg_id + 1 --kong.log.debug("will encode: ", pp{sequence = msg_id, [method] = data}) @@ -361,7 +365,7 @@ function Rpc:call_start_instance(plugin_name, conf) }) if status == nil then - return err + return nil, err end return {