From 1edb16b1349c796bfe55ae0553a4712d4a703305 Mon Sep 17 00:00:00 2001 From: Michael Martin Date: Mon, 29 Jan 2024 15:53:23 -0600 Subject: [PATCH] fix(wasm): do not call attach() on re-entrancy (#12402) (cherry picked from commit 4d03ca45edfa54c76e5d2d4271892f37b7524ddd) --- changelog/unreleased/kong/wasm-attach.yml | 5 +++++ kong/runloop/wasm.lua | 25 ++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 changelog/unreleased/kong/wasm-attach.yml diff --git a/changelog/unreleased/kong/wasm-attach.yml b/changelog/unreleased/kong/wasm-attach.yml new file mode 100644 index 00000000000..99ae358d401 --- /dev/null +++ b/changelog/unreleased/kong/wasm-attach.yml @@ -0,0 +1,5 @@ +message: > + **proxy-wasm**: Fixed "previous plan already attached" error thrown when a + filter triggers re-entrancy of the access handler. +type: bugfix +scope: Core diff --git a/kong/runloop/wasm.lua b/kong/runloop/wasm.lua index 70f36b798ad..9bb697cdda1 100644 --- a/kong/runloop/wasm.lua +++ b/kong/runloop/wasm.lua @@ -922,17 +922,22 @@ function _M.attach(ctx) ctx.ran_wasm = true - local ok, err = proxy_wasm.attach(chain.c_plan) - if not ok then - log(CRIT, "failed attaching ", chain.label, " filter chain to request: ", err) - return kong.response.error(500) - end + local ok, err + if not ctx.wasm_attached then + ctx.wasm_attached = true - ok, err = proxy_wasm.set_host_properties_handlers(properties.get, - properties.set) - if not ok then - log(CRIT, "failed setting host property handlers: ", err) - return kong.response.error(500) + ok, err = proxy_wasm.attach(chain.c_plan) + if not ok then + log(CRIT, "failed attaching ", chain.label, " filter chain to request: ", err) + return kong.response.error(500) + end + + ok, err = proxy_wasm.set_host_properties_handlers(properties.get, + properties.set) + if not ok then + log(CRIT, "failed setting host property handlers: ", err) + return kong.response.error(500) + end end jit.off(proxy_wasm.start)