@@ -45,7 +45,6 @@ thread_local std::unordered_map<std::string, std::weak_ptr<PluginHandleBase>> lo
4545// Map from Wasm Key to the base Wasm instance, using a pointer to avoid the initialization fiasco.
4646std::mutex base_wasms_mutex;
4747std::unordered_map<std::string, std::weak_ptr<WasmHandleBase>> *base_wasms = nullptr ;
48- std::unordered_map<std::string, WasmForeignFunction> *foreign_functions = nullptr ;
4948
5049std::vector<uint8_t > Sha256 (const std::vector<std::string_view> parts) {
5150 uint8_t sha256[SHA256_DIGEST_LENGTH];
@@ -85,13 +84,6 @@ class WasmBase::ShutdownHandle {
8584 std::shared_ptr<WasmBase> wasm_;
8685};
8786
88- RegisterForeignFunction::RegisterForeignFunction (std::string name, WasmForeignFunction f) {
89- if (!foreign_functions) {
90- foreign_functions = new std::remove_reference<decltype (*foreign_functions)>::type;
91- }
92- (*foreign_functions)[name] = f;
93- }
94-
9587void WasmBase::registerCallbacks () {
9688#define _REGISTER (_fn ) \
9789 wasm_vm_->registerCallback ( \
@@ -208,7 +200,7 @@ WasmBase::WasmBase(const std::shared_ptr<WasmHandleBase> &base_wasm_handle, Wasm
208200 if (!wasm_vm_) {
209201 failed_ = FailState::UnableToCreateVm;
210202 } else {
211- wasm_vm_->setFailCallback ([this ](FailState fail_state) { failed_ = fail_state; });
203+ wasm_vm_->addFailCallback ([this ](FailState fail_state) { failed_ = fail_state; });
212204 }
213205}
214206
@@ -222,7 +214,7 @@ WasmBase::WasmBase(std::unique_ptr<WasmVm> wasm_vm, std::string_view vm_id,
222214 if (!wasm_vm_) {
223215 failed_ = FailState::UnableToCreateVm;
224216 } else {
225- wasm_vm_->setFailCallback ([this ](FailState fail_state) { failed_ = fail_state; });
217+ wasm_vm_->addFailCallback ([this ](FailState fail_state) { failed_ = fail_state; });
226218 }
227219}
228220
@@ -454,14 +446,6 @@ void WasmBase::finishShutdown() {
454446 }
455447}
456448
457- WasmForeignFunction WasmBase::getForeignFunction (std::string_view function_name) {
458- auto it = foreign_functions->find (std::string (function_name));
459- if (it != foreign_functions->end ()) {
460- return it->second ;
461- }
462- return nullptr ;
463- }
464-
465449std::shared_ptr<WasmHandleBase> createWasm (std::string vm_key, std::string code,
466450 std::shared_ptr<PluginBase> plugin,
467451 WasmHandleFactory factory,
@@ -559,6 +543,14 @@ getOrCreateThreadLocalWasm(std::shared_ptr<WasmHandleBase> base_handle,
559543 return nullptr ;
560544 }
561545 local_wasms[vm_key] = wasm_handle;
546+ wasm_handle->wasm ()->wasm_vm ()->addFailCallback ([vm_key](proxy_wasm::FailState fail_state) {
547+ if (fail_state == proxy_wasm::FailState::RuntimeError) {
548+ // If VM failed, erase the entry so that:
549+ // 1) we can recreate the new thread local VM from the same base_wasm.
550+ // 2) we wouldn't reuse the failed VM for new plugins accidentally.
551+ local_wasms.erase (vm_key);
552+ };
553+ });
562554 return wasm_handle;
563555}
564556
@@ -594,6 +586,14 @@ std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin(
594586 }
595587 auto plugin_handle = plugin_factory (wasm_handle, plugin);
596588 local_plugins[key] = plugin_handle;
589+ wasm_handle->wasm ()->wasm_vm ()->addFailCallback ([key](proxy_wasm::FailState fail_state) {
590+ if (fail_state == proxy_wasm::FailState::RuntimeError) {
591+ // If VM failed, erase the entry so that:
592+ // 1) we can recreate the new thread local plugin from the same base_wasm.
593+ // 2) we wouldn't reuse the failed VM for new plugin configs accidentally.
594+ local_plugins.erase (key);
595+ };
596+ });
597597 return plugin_handle;
598598}
599599
0 commit comments