diff --git a/source/extensions/common/wasm/wasm.cc b/source/extensions/common/wasm/wasm.cc index 1c1d09a2bf..e8eea749a8 100644 --- a/source/extensions/common/wasm/wasm.cc +++ b/source/extensions/common/wasm/wasm.cc @@ -352,6 +352,16 @@ bool Wasm::initialize(const std::string& code, bool allow_precompiled) { return true; } +Context* Wasm::getOrCreateRootContext(const PluginSharedPtr& plugin) { + auto root_context = getRootContext(plugin->root_id_); + if (!root_context) { + auto context = std::make_unique(this, plugin); + root_context = context.get(); + root_contexts_[plugin->root_id_] = std::move(context); + } + return root_context; +} + void Wasm::startVm(Context* root_context) { /* Call "_start" function, and fallback to "__wasm_call_ctors" if the former is not available. */ if (_start_) { @@ -611,7 +621,7 @@ WasmHandleSharedPtr getOrCreateThreadLocalWasm(WasmHandleSharedPtr base_wasm, Event::Dispatcher& dispatcher) { auto wasm_handle = getThreadLocalWasmPtr(base_wasm->wasm()->vm_key()); if (wasm_handle) { - auto root_context = wasm_handle->wasm()->start(plugin); + auto root_context = wasm_handle->wasm()->getOrCreateRootContext(plugin); if (!wasm_handle->wasm()->configure(root_context, plugin, configuration)) { throw WasmException("Failed to configure WASM code"); } diff --git a/source/extensions/common/wasm/wasm.h b/source/extensions/common/wasm/wasm.h index 40455144d6..020408b20e 100644 --- a/source/extensions/common/wasm/wasm.h +++ b/source/extensions/common/wasm/wasm.h @@ -76,6 +76,7 @@ class Wasm : public Logger::Loggable, public std::enable_share Context* vm_context() const { return vm_context_.get(); } Stats::StatNameSetSharedPtr stat_name_set() const { return stat_name_set_; } Context* getRootContext(absl::string_view root_id) { return root_contexts_[root_id].get(); } + Context* getOrCreateRootContext(const PluginSharedPtr& plugin); Context* getContext(uint32_t id) { auto it = contexts_.find(id); if (it != contexts_.end())