diff --git a/kong/globalpatches.lua b/kong/globalpatches.lua index 99c128560df1..a4f8e7c7b9c9 100644 --- a/kong/globalpatches.lua +++ b/kong/globalpatches.lua @@ -596,20 +596,29 @@ return function(options) local hook_called = false _G.ngx.log = function(...) if hook_called then - -- avoid recursive loops: relies on the hook handler to NOT yield + -- detect recursive loops and yielding from the hook: old_ngx_log(ngx.ERR, debug.traceback("concurrent execution detected for: ngx.log", 2)) return old_ngx_log(...) end - -- stack level = 5: + -- The pcall surrounding `run_hook` is to ensure `hook_called` is always + -- reset to `false` after hook execution, even in case an error is thrown + -- by `run_hook`. + -- + -- stack level = 6: -- 1: maybe_push -- 2: dynamic_hook.pcall -- 3: dynamic_hook.run_hook -- 4: patched function - -- 5: caller + -- 5: pcall + -- 6: caller hook_called = true - dynamic_hook.run_hook("observability_logs", "push", 5, ...) + local ok, err = pcall(dynamic_hook.run_hook, "observability_logs", "push", 6, ...) hook_called = false + + if not ok then + old_ngx_log(ngx.ERR, debug.traceback("observability_logs hook failed: " .. err, 2)) + end return old_ngx_log(...) end -- export native ngx.log to be used where