From 15521fb90f565894898ddc3aa936fb03b9185be6 Mon Sep 17 00:00:00 2001 From: Louis Date: Fri, 22 Mar 2024 14:03:19 +0000 Subject: [PATCH] fix(napi): no panic when caller stops listening (#2010) This fix is similar to the one in https://github.com/napi-rs/napi-rs/commit/5b5f616d81ff2f0e154ff8b1e1edee1dfc087eb4. In both the then_callback & catch_callback, expect was being called in case the send failed. This means that if we call a function that returns a promise and the received gets closed (the calling thread stopped at the wrong time), this will panic. In such scenarios, it is fine not to panic. If the receiver doesn't care about the output, we should just let it be. --- .../src/bindgen_runtime/js_values/promise.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/napi/src/bindgen_runtime/js_values/promise.rs b/crates/napi/src/bindgen_runtime/js_values/promise.rs index 0647000e..976c1027 100644 --- a/crates/napi/src/bindgen_runtime/js_values/promise.rs +++ b/crates/napi/src/bindgen_runtime/js_values/promise.rs @@ -215,9 +215,9 @@ unsafe extern "C" fn then_callback( return this; } let resolve_value_t = Box::new(unsafe { T::from_napi_value(env, resolved_value[0]) }); - sender - .send(Box::into_raw(resolve_value_t)) - .expect("Send Promise resolved value error"); + // The only reason for send to return Err is if the receiver isn't listening + // Not hiding the error would result in a panic, it's safe to ignore it instead. + let _ = sender.send(Box::into_raw(resolve_value_t)); this } @@ -249,10 +249,10 @@ unsafe extern "C" fn catch_callback( if aborted.load(Ordering::SeqCst) { return this; } - sender - .send(Box::into_raw(Box::new(Err(Error::from(unsafe { - JsUnknown::from_raw_unchecked(env, rejected_value) - }))))) - .expect("Send Promise resolved value error"); + // The only reason for send to return Err is if the receiver isn't listening + // Not hiding the error would result in a panic, it's safe to ignore it instead. + let _ = sender.send(Box::into_raw(Box::new(Err(Error::from(unsafe { + JsUnknown::from_raw_unchecked(env, rejected_value) + }))))); this }