diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-01-28 16:30:05 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-28 16:30:05 +0100 |
commit | 9cd271a9a531083dbf4f20d88fcdbcd49ee9f2ac (patch) | |
tree | 6cc9fd70e5fd4fea05c396581230be1ab88396a6 | |
parent | fe11df09b15088f5d33a086cc416ae9eaa68f728 (diff) |
fix(napi): guard threadsafe function counters behind a mutex (#17552)
-rw-r--r-- | ext/napi/lib.rs | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/ext/napi/lib.rs b/ext/napi/lib.rs index b4f38fd0a..789aec5eb 100644 --- a/ext/napi/lib.rs +++ b/ext/napi/lib.rs @@ -11,6 +11,7 @@ use deno_core::error::AnyError; use deno_core::futures::channel::mpsc; use deno_core::futures::StreamExt; use deno_core::op; +use deno_core::parking_lot::Mutex; use deno_core::serde_v8; use deno_core::Extension; use deno_core::OpState; @@ -338,7 +339,7 @@ pub struct NapiState { mpsc::UnboundedSender<ThreadSafeFunctionStatus>, pub env_cleanup_hooks: Rc<RefCell<Vec<(extern "C" fn(*const c_void), *const c_void)>>>, - pub tsfn_ref_counters: Rc<RefCell<ThreadsafeFunctionRefCounters>>, + pub tsfn_ref_counters: Arc<Mutex<ThreadsafeFunctionRefCounters>>, } impl Drop for NapiState { @@ -415,7 +416,7 @@ pub struct Env { mpsc::UnboundedSender<ThreadSafeFunctionStatus>, pub cleanup_hooks: Rc<RefCell<Vec<(extern "C" fn(*const c_void), *const c_void)>>>, - pub tsfn_ref_counters: Rc<RefCell<ThreadsafeFunctionRefCounters>>, + pub tsfn_ref_counters: Arc<Mutex<ThreadsafeFunctionRefCounters>>, pub last_error: napi_extended_error_info, } @@ -431,7 +432,7 @@ impl Env { cleanup_hooks: Rc< RefCell<Vec<(extern "C" fn(*const c_void), *const c_void)>>, >, - tsfn_ref_counters: Rc<RefCell<ThreadsafeFunctionRefCounters>>, + tsfn_ref_counters: Arc<Mutex<ThreadsafeFunctionRefCounters>>, ) -> Self { let sc = sender.clone(); ASYNC_WORK_SENDER.with(|s| { @@ -498,13 +499,13 @@ impl Env { id: usize, counter: Arc<AtomicUsize>, ) { - let mut counters = self.tsfn_ref_counters.borrow_mut(); + let mut counters = self.tsfn_ref_counters.lock(); assert!(!counters.iter().any(|(i, _)| *i == id)); counters.push((id, counter)); } pub fn remove_threadsafe_function_ref_counter(&mut self, id: usize) { - let mut counters = self.tsfn_ref_counters.borrow_mut(); + let mut counters = self.tsfn_ref_counters.lock(); let index = counters.iter().position(|(i, _)| *i == id).unwrap(); counters.remove(index); } @@ -533,7 +534,7 @@ pub fn init<P: NapiPermissions + 'static>(unstable: bool) -> Extension { maybe_scheduling = true; } - let tsfn_ref_counters = napi_state.tsfn_ref_counters.borrow().clone(); + let tsfn_ref_counters = napi_state.tsfn_ref_counters.lock().clone(); for (_id, counter) in tsfn_ref_counters.iter() { if counter.load(std::sync::atomic::Ordering::SeqCst) > 0 { maybe_scheduling = true; @@ -572,7 +573,7 @@ pub fn init<P: NapiPermissions + 'static>(unstable: bool) -> Extension { threadsafe_function_receiver, active_threadsafe_functions: 0, env_cleanup_hooks: Rc::new(RefCell::new(vec![])), - tsfn_ref_counters: Rc::new(RefCell::new(vec![])), + tsfn_ref_counters: Arc::new(Mutex::new(vec![])), }); state.put(Unstable(unstable)); Ok(()) |