fix(napi): no panic when caller stops listening (#2010)

This fix is similar to the one in 5b5f616d81.

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.
This commit is contained in:
Louis 2024-03-22 14:03:19 +00:00 committed by GitHub
parent 35b9637151
commit 15521fb90f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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
}
@ -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
}