Skip to content

Commit

Permalink
fix(napi): no panic when caller stops listening (#2010)
Browse files Browse the repository at this point in the history
This fix is similar to the one in 5b5f616.

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.
  • Loading branch information
lbarthon authored Mar 22, 2024
1 parent 35b9637 commit 15521fb
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/napi/src/bindgen_runtime/js_values/promise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ unsafe extern "C" fn then_callback<T: FromNapiValue>(
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
}

Expand Down Expand Up @@ -249,10 +249,10 @@ unsafe extern "C" fn catch_callback<T: FromNapiValue>(
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
}

0 comments on commit 15521fb

Please sign in to comment.