Skip to content

Commit 65f3bd3

Browse files
feat(rpc): handle callback exception
KAG-6091
1 parent bd70457 commit 65f3bd3

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

kong/clustering/rpc/socket.lua

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,37 @@ function _M:_get_next_id()
5959
end
6060

6161

62+
function _M:_report_error(payload_id, code, err)
63+
local res, rpc_err = self.outgoing:push(
64+
new_error(payload_id, code, err)
65+
)
66+
67+
if not res then
68+
ngx_log(ngx_WARN, "[rpc] unable to push RPC call error: ", rpc_err)
69+
end
70+
end
71+
72+
6273
function _M._dispatch(premature, self, cb, payload)
6374
if premature then
6475
return
6576
end
6677

67-
local res, err = cb(self.node_id, unpack(payload.params))
78+
local res, except, err, trace
79+
xpcall(
80+
function() res, err = cb(self.node_id, unpack(payload.params)) end,
81+
function(err)
82+
except = err
83+
trace = debug.traceback(nil, 2)
84+
end
85+
)
86+
87+
if except then
88+
ngx_log(ngx_WARN, "[rpc] exception during callback: ", except, "\n", trace)
89+
90+
return self:_report_error(payload.id, jsonrpc.INTERNAL_ERROR, except)
91+
end
92+
6893
if not res then
6994
ngx_log(ngx_WARN, "[rpc] RPC callback failed: ", err)
7095

@@ -73,13 +98,7 @@ function _M._dispatch(premature, self, cb, payload)
7398
return
7499
end
75100

76-
res, err = self.outgoing:push(new_error(payload.id, jsonrpc.SERVER_ERROR,
77-
err))
78-
if not res then
79-
ngx_log(ngx_WARN, "[rpc] unable to push RPC call error: ", err)
80-
end
81-
82-
return
101+
return self:_report_error(payload.id, jsonrpc.SERVER_ERROR, err)
83102
end
84103

85104
-- notification has no response

0 commit comments

Comments
 (0)