From 4a13c320d73b1d8a0da18effa81199ab2ea8ef55 Mon Sep 17 00:00:00 2001 From: Andreu Botella Date: Mon, 29 Nov 2021 13:37:44 +0100 Subject: fix(workers): Make `worker.terminate()` not immediately kill the isolate (#12831) Due to a bug in V8, terminating an isolate while a module with top-level await is being evaluated would crash the process. This change makes it so calling `worker.terminate()` will signal the worker to terminate at the next iteration of the event loop, and it schedules a proper termination of the worker's isolate after 2 seconds. --- cli/tests/testdata/workers/terminate_tla_crash.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 cli/tests/testdata/workers/terminate_tla_crash.js (limited to 'cli/tests/testdata/workers/terminate_tla_crash.js') diff --git a/cli/tests/testdata/workers/terminate_tla_crash.js b/cli/tests/testdata/workers/terminate_tla_crash.js new file mode 100644 index 000000000..f793b8c8e --- /dev/null +++ b/cli/tests/testdata/workers/terminate_tla_crash.js @@ -0,0 +1,21 @@ +// Test for https://github.com/denoland/deno/issues/12658 +// +// If a worker is terminated immediately after construction, and the worker's +// main module uses top-level await, V8 has a chance to crash. +// +// These crashes are so rare in debug mode that I've only seen them once. They +// happen a lot more often in release mode. + +const workerModule = ` + await new Promise(resolve => setTimeout(resolve, 1000)); +`; + +// Iterating 10 times to increase the likelihood of triggering the crash, at +// least in release mode. +for (let i = 0; i < 10; i++) { + const worker = new Worker( + `data:application/javascript;base64,${btoa(workerModule)}`, + { type: "module" }, + ); + worker.terminate(); +} -- cgit v1.2.3