Skip to content

Commit 39a25c2

Browse files
committed
Add addFailCallback to WasmBase.
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent 82633e3 commit 39a25c2

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

include/proxy-wasm/wasm_vm.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <optional>
2121
#include <string>
2222
#include <unordered_map>
23+
#include <vector>
2324

2425
#include "include/proxy-wasm/word.h"
2526

@@ -268,12 +269,12 @@ class WasmVm {
268269
void fail(FailState fail_state, std::string_view message) {
269270
integration()->error(message);
270271
failed_ = fail_state;
271-
if (fail_callback_) {
272-
fail_callback_(fail_state);
272+
for (auto &callback : fail_callbacks_) {
273+
callback(fail_state);
273274
}
274275
}
275-
void setFailCallback(std::function<void(FailState)> fail_callback) {
276-
fail_callback_ = fail_callback;
276+
void addFailCallback(std::function<void(FailState)> fail_callback) {
277+
fail_callbacks_.push_back(fail_callback);
277278
}
278279

279280
// Integrator operations.
@@ -283,7 +284,7 @@ class WasmVm {
283284
protected:
284285
std::unique_ptr<WasmVmIntegration> integration_;
285286
FailState failed_ = FailState::Ok;
286-
std::function<void(FailState)> fail_callback_;
287+
std::vector<std::function<void(FailState)>> fail_callbacks_;
287288
};
288289

289290
// Thread local state set during a call into a WASM VM so that calls coming out of the

src/wasm.cc

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ WasmBase::WasmBase(const std::shared_ptr<WasmHandleBase> &base_wasm_handle, Wasm
210210
if (!wasm_vm_) {
211211
failed_ = FailState::UnableToCreateVm;
212212
} else {
213-
wasm_vm_->setFailCallback([this](FailState fail_state) { failed_ = fail_state; });
213+
wasm_vm_->addFailCallback([this](FailState fail_state) { failed_ = fail_state; });
214214
}
215215
}
216216

@@ -224,7 +224,7 @@ WasmBase::WasmBase(std::unique_ptr<WasmVm> wasm_vm, std::string_view vm_id,
224224
if (!wasm_vm_) {
225225
failed_ = FailState::UnableToCreateVm;
226226
} else {
227-
wasm_vm_->setFailCallback([this](FailState fail_state) { failed_ = fail_state; });
227+
wasm_vm_->addFailCallback([this](FailState fail_state) { failed_ = fail_state; });
228228
}
229229
}
230230

@@ -561,6 +561,14 @@ getOrCreateThreadLocalWasm(std::shared_ptr<WasmHandleBase> base_handle,
561561
return nullptr;
562562
}
563563
local_wasms[vm_key] = wasm_handle;
564+
wasm_handle->wasm()->wasm_vm()->addFailCallback([vm_key](proxy_wasm::FailState fail_state) {
565+
if (fail_state == proxy_wasm::FailState::RuntimeError) {
566+
// If VM failed, erase the entry so that:
567+
// 1) we can recreate the new thread local VM from the same base_wasm.
568+
// 2) we wouldn't reuse the failed VM for new plugins accidentally.
569+
local_wasms.erase(vm_key);
570+
};
571+
});
564572
return wasm_handle;
565573
}
566574

@@ -596,6 +604,14 @@ std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin(
596604
}
597605
auto plugin_handle = plugin_factory(wasm_handle, plugin);
598606
local_plugins[key] = plugin_handle;
607+
wasm_handle->wasm()->wasm_vm()->addFailCallback([key](proxy_wasm::FailState fail_state) {
608+
if (fail_state == proxy_wasm::FailState::RuntimeError) {
609+
// If VM failed, erase the entry so that:
610+
// 1) we can recreate the new thread local plugin from the same base_wasm.
611+
// 2) we wouldn't reuse the failed VM for new plugin configs accidentally.
612+
local_plugins.erase(key);
613+
};
614+
});
599615
return plugin_handle;
600616
}
601617

0 commit comments

Comments
 (0)