Skip to content

Commit ef59c04

Browse files
committed
convert base handle to weak ptr
1 parent 08aef9c commit ef59c04

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

include/proxy-wasm/wasm.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
304304
// is not enforced.
305305
AllowedCapabilitiesMap allowed_capabilities_;
306306

307-
std::shared_ptr<WasmHandleBase> base_wasm_handle_;
307+
std::weak_ptr<WasmHandleBase> base_wasm_handle_weak_;
308308

309309
// Used by the base_wasm to enable non-clonable thread local Wasm(s) to be constructed.
310310
std::string module_bytecode_;
@@ -448,8 +448,9 @@ std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin(
448448
void clearWasmCachesForTesting();
449449

450450
inline const std::string &WasmBase::vm_configuration() const {
451-
if (base_wasm_handle_)
452-
return base_wasm_handle_->wasm()->vm_configuration_;
451+
auto base_wasm_handle = base_wasm_handle_weak_.lock();
452+
if (base_wasm_handle)
453+
return base_wasm_handle->wasm()->vm_configuration_;
453454
return vm_configuration_;
454455
}
455456

src/wasm.cc

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ WasmBase::WasmBase(const std::shared_ptr<WasmHandleBase> &base_wasm_handle,
221221
started_from_(base_wasm_handle->wasm()->wasm_vm()->cloneable()),
222222
envs_(base_wasm_handle->wasm()->envs()),
223223
allowed_capabilities_(base_wasm_handle->wasm()->allowed_capabilities_),
224-
base_wasm_handle_(base_wasm_handle) {
224+
base_wasm_handle_weak_(base_wasm_handle) {
225225
if (started_from_ != Cloneable::NotCloneable) {
226226
wasm_vm_ = base_wasm_handle->wasm()->wasm_vm()->clone();
227227
} else {
@@ -338,17 +338,27 @@ bool WasmBase::initialize() {
338338
}
339339

340340
if (started_from_ == Cloneable::NotCloneable) {
341-
auto ok = wasm_vm_->load(base_wasm_handle_->wasm()->moduleBytecode(),
342-
base_wasm_handle_->wasm()->modulePrecompiled(),
343-
base_wasm_handle_->wasm()->functionNames());
341+
auto base_wasm_handle = base_wasm_handle_weak_.lock();
342+
if (!base_wasm_handle) {
343+
fail(FailState::UnableToInitializeCode, "Base wasm handle is null");
344+
return false;
345+
}
346+
auto ok = wasm_vm_->load(base_wasm_handle->wasm()->moduleBytecode(),
347+
base_wasm_handle->wasm()->modulePrecompiled(),
348+
base_wasm_handle->wasm()->functionNames());
344349
if (!ok) {
345350
fail(FailState::UnableToInitializeCode, "Failed to load Wasm module from base Wasm");
346351
return false;
347352
}
348353
}
349354

350355
if (started_from_.has_value()) {
351-
abi_version_ = base_wasm_handle_->wasm()->abiVersion();
356+
auto base_wasm_handle = base_wasm_handle_weak_.lock();
357+
if (!base_wasm_handle) {
358+
fail(FailState::UnableToInitializeCode, "Base wasm handle is null");
359+
return false;
360+
}
361+
abi_version_ = base_wasm_handle->wasm()->abiVersion();
352362
}
353363

354364
if (started_from_ != Cloneable::InstantiatedModule) {

0 commit comments

Comments
 (0)