Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/c-api: fix env_finalizer being ran everytime WrapperEnv was dropped #2872

Merged
merged 1 commit into from
May 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/c-api/src/wasm_c_api/externals/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::CApiExternTag;
use std::convert::TryInto;
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::sync::Arc;
use std::sync::{Arc, Mutex};
use wasmer_api::{Function, RuntimeError, Val};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -110,7 +110,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
#[repr(C)]
struct WrapperEnv {
env: *mut c_void,
env_finalizer: Arc<Option<wasm_env_finalizer_t>>,
env_finalizer: Arc<Mutex<Option<wasm_env_finalizer_t>>>,
}

impl wasmer_api::WasmerEnv for WrapperEnv {}
Expand All @@ -122,8 +122,12 @@ pub unsafe extern "C" fn wasm_func_new_with_env(

impl Drop for WrapperEnv {
fn drop(&mut self) {
if let Some(env_finalizer) = *self.env_finalizer {
unsafe { (env_finalizer)(self.env as _) }
if let Ok(mut guard) = self.env_finalizer.lock() {
if Arc::strong_count(&self.env_finalizer) == 1 {
if let Some(env_finalizer) = guard.take() {
unsafe { (env_finalizer)(self.env as _) };
}
}
}
}
}
Expand Down Expand Up @@ -166,7 +170,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
func_sig,
WrapperEnv {
env,
env_finalizer: Arc::new(env_finalizer),
env_finalizer: Arc::new(Mutex::new(env_finalizer)),
},
trampoline,
);
Expand Down