From 208c91b68f4ae1b59e65acbde3de729e7058bb5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 23 Dec 2022 19:46:24 +0100 Subject: fix(core): run macrotasks and next ticks after polling dynamic imports (#17173) This commit fixes handling of rejected promises in dynamic imports evaluation. Previously we were running callbacks for next ticks and macrotasks _before_ polling dynamic imports and checked for unhandled rejections immediately after. This is wrong, as `unhandledrejection` event is dispatched and its callbacks are run as macrotasks. This commit changes order of actions performed by the event loop to following: - poll async ops - poll dynamic imports - run next tick callbacks - run macrotask callbacks - check for unhandled promise rejections --- core/runtime.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'core/runtime.rs') diff --git a/core/runtime.rs b/core/runtime.rs index 2e417bb9d..f735f5575 100644 --- a/core/runtime.rs +++ b/core/runtime.rs @@ -1127,12 +1127,7 @@ impl JsRuntime { self.pump_v8_message_loop()?; // Ops - { - self.resolve_async_ops(cx)?; - self.drain_nexttick()?; - self.drain_macrotasks()?; - self.check_promise_exceptions()?; - } + self.resolve_async_ops(cx)?; // Dynamic module loading - ie. modules loaded using "import()" { // Run in a loop so that dynamic imports that only depend on another @@ -1157,9 +1152,13 @@ impl JsRuntime { break; } } - - self.check_promise_exceptions()?; } + // Run all next tick callbacks and macrotasks callbacks and only then + // check for any promise exceptions (`unhandledrejection` handlers are + // run in macrotasks callbacks so we need to let them run first). + self.drain_nexttick()?; + self.drain_macrotasks()?; + self.check_promise_exceptions()?; // Event loop middlewares let mut maybe_scheduling = false; -- cgit v1.2.3