From 504d2936ecf1a5520ca20f83792a94b219e84f53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 23 Jul 2022 00:40:42 +0200 Subject: fix: unhandledrejection handling for sync throw in top level (#15279) Fixes an edge in "unhandledrejection" event that prevent synchronous errors being surfaced when throw from a top-level scope. --- core/bindings.rs | 2 ++ core/runtime.rs | 10 +++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/bindings.rs b/core/bindings.rs index 6fa9f745b..a93a3e746 100644 --- a/core/bindings.rs +++ b/core/bindings.rs @@ -363,6 +363,8 @@ pub extern "C" fn promise_reject_callback(message: v8::PromiseRejectMessage) { } } + // TODO(bartlomieju): remove this whole block, `js_uncaught_exception_cb` is + // not needed anymore if let Some(exception) = tc_scope.exception() { if let Some(js_uncaught_exception_cb) = js_uncaught_exception_cb { tc_scope.reset(); // Cancel pending exception. diff --git a/core/runtime.rs b/core/runtime.rs index 64e7f635c..4568f2018 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1335,7 +1335,15 @@ impl JsRuntime { .expect("Expected to get promise as module evaluation result"); let promise_global = v8::Global::new(tc_scope, promise); let mut state = state_rc.borrow_mut(); - state.pending_promise_exceptions.remove(&promise_global); + { + let pending_mod_evaluate = state.pending_mod_evaluate.as_ref().unwrap(); + let pending_rejection_was_already_handled = pending_mod_evaluate + .handled_promise_rejections + .contains(&promise_global); + if !pending_rejection_was_already_handled { + state.pending_promise_exceptions.remove(&promise_global); + } + } let promise_global = v8::Global::new(tc_scope, promise); state.pending_mod_evaluate.as_mut().unwrap().promise = Some(promise_global); -- cgit v1.2.3