From e9fd5d3d40107492443d419eb9a07a6a892b7469 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 12 Nov 2020 10:47:33 +0800 Subject: [PATCH] feat(napi): add abort guard in tsfn methods --- napi/src/threadsafe_function.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/napi/src/threadsafe_function.rs b/napi/src/threadsafe_function.rs index 0de8bc22..2fc88cd2 100644 --- a/napi/src/threadsafe_function.rs +++ b/napi/src/threadsafe_function.rs @@ -163,12 +163,24 @@ impl ThreadsafeFunction { /// /// "ref" is a keyword so that we use "refer" here. pub fn refer(&mut self, env: &Env) -> Result<()> { + if self.aborted.load(Ordering::Acquire) { + return Err(Error::new( + Status::Closing, + format!("Can not ref, Thread safe function already aborted"), + )); + } check_status(unsafe { sys::napi_ref_threadsafe_function(env.0, self.raw_tsfn) }) } /// See [napi_unref_threadsafe_function](https://nodejs.org/api/n-api.html#n_api_napi_unref_threadsafe_function) /// for more information. pub fn unref(&mut self, env: &Env) -> Result<()> { + if self.aborted.load(Ordering::Acquire) { + return Err(Error::new( + Status::Closing, + format!("Can not unref, Thread safe function already aborted"), + )); + } check_status(unsafe { sys::napi_unref_threadsafe_function(env.0, self.raw_tsfn) }) } @@ -191,7 +203,7 @@ impl ThreadsafeFunction { if self.aborted.load(Ordering::Acquire) { return Err(Error::new( Status::Closing, - format!("Thread safe function already aborted"), + format!("Can not clone, Thread safe function already aborted"), )); } check_status(unsafe { sys::napi_acquire_threadsafe_function(self.raw_tsfn) })?;