diff --git a/include/proxy-wasm/wasm.h b/include/proxy-wasm/wasm.h index d07de3e85..9d914b557 100644 --- a/include/proxy-wasm/wasm.h +++ b/include/proxy-wasm/wasm.h @@ -134,10 +134,14 @@ class WasmBase : public std::enable_shared_from_this { void addAfterVmCallAction(std::function f) { after_vm_call_actions_.push_back(f); } void doAfterVmCallActions() { - while (!after_vm_call_actions_.empty()) { - auto f = std::move(after_vm_call_actions_.front()); - after_vm_call_actions_.pop_front(); - f(); + // NB: this may be deleted by a delayed function unless prevented. + if (!after_vm_call_actions_.empty()) { + auto self = shared_from_this(); + while (!self->after_vm_call_actions_.empty()) { + auto f = std::move(self->after_vm_call_actions_.front()); + self->after_vm_call_actions_.pop_front(); + f(); + } } }