From 18ac7d40c87b380ce5ed617bd7c59e344730a883 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Thu, 21 Jan 2021 16:44:48 +0900 Subject: fix(runtime): fix recursive dispatches of unload event (#9207) --- runtime/js/30_os.js | 9 ++++++--- runtime/js/99_main.js | 9 +++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'runtime/js') diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js index 1d700b561..5a3c990b0 100644 --- a/runtime/js/30_os.js +++ b/runtime/js/30_os.js @@ -24,9 +24,12 @@ } function exit(code = 0) { - // Invokes the `unload` hooks before exiting - // ref: https://github.com/denoland/deno/issues/3603 - window.dispatchEvent(new Event("unload")); + // Dispatches `unload` only when it's not dispatched yet. + if (!window[Symbol.for("isUnloadDispatched")]) { + // Invokes the `unload` hooks before exiting + // ref: https://github.com/denoland/deno/issues/3603 + window.dispatchEvent(new Event("unload")); + } core.jsonOpSync("op_exit", { code }); throw new Error("Code not reachable"); } diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index a6abc8d27..fd846af20 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -305,6 +305,15 @@ delete Object.prototype.__proto__; defineEventHandler(window, "load", null); defineEventHandler(window, "unload", null); + const isUnloadDispatched = Symbol.for("isUnloadDispatched"); + // Stores the flag for checking whether unload is dispatched or not. + // This prevents the recursive dispatches of unload events. + // See https://github.com/denoland/deno/issues/9201. + window[isUnloadDispatched] = false; + window.addEventListener("unload", () => { + window[isUnloadDispatched] = true; + }); + runtimeStart(runtimeOptions); const { args, -- cgit v1.2.3