summaryrefslogtreecommitdiff
path: root/cli
diff options
context:
space:
mode:
authorAndreu Botella <abb@randomunok.com>2021-09-22 18:02:15 +0200
committerGitHub <noreply@github.com>2021-09-22 18:02:15 +0200
commit5c5f4ea1d6fe0e46ea535234bf9ae46b1a2a981b (patch)
tree96c4e07ab4236987d55a6d583d529ba85bf49093 /cli
parenteddae41482588f762e67cb232da3c3a30524c233 (diff)
fix(workers): Don't panic when a worker's parent thread stops running (#12156)
This panic could happen in the following cases: - A non-fatal error being thrown from a worker, that doesn't terminate the worker's execution, but propagates to the main thread without being handled, and makes the main thread terminate. - A nested worker being alive while its parent worker gets terminated. - A race condition if the main event loop terminates the worker as part of its last task, but the worker doesn't fully terminate before the main event loop stops running. This panic happens because a worker's event loop should have pending ops as long as the worker isn't closed or terminated – but if an event loop finishes running while it has living workers, its associated `WorkerThread` structs will be dropped, closing the channels that keep those ops pending. This change adds a `Drop` implementation to `WorkerThread`, which terminates the worker without waiting for a response. This fixes the panic, and makes it so nested workers are automatically terminated once any of their ancestors is closed or terminated. This change also refactors a worker's termination code into a `WorkerThread::terminate()` method. Closes #11342. Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
Diffstat (limited to 'cli')
-rw-r--r--cli/tests/integration/run_tests.rs6
-rw-r--r--cli/tests/testdata/worker_drop_handle_race.js12
-rw-r--r--cli/tests/testdata/worker_drop_handle_race.js.out8
-rw-r--r--cli/tests/testdata/workers/drop_handle_race.js3
4 files changed, 29 insertions, 0 deletions
diff --git a/cli/tests/integration/run_tests.rs b/cli/tests/integration/run_tests.rs
index df92ad422..5116db295 100644
--- a/cli/tests/integration/run_tests.rs
+++ b/cli/tests/integration/run_tests.rs
@@ -1188,6 +1188,12 @@ itest!(worker_close_race {
output: "worker_close_race.js.out",
});
+itest!(worker_drop_handle_race {
+ args: "run --quiet --reload --allow-read worker_drop_handle_race.js",
+ output: "worker_drop_handle_race.js.out",
+ exit_code: 1,
+});
+
itest!(worker_message_before_close {
args: "run --quiet --reload --allow-read worker_message_before_close.js",
output: "worker_message_before_close.js.out",
diff --git a/cli/tests/testdata/worker_drop_handle_race.js b/cli/tests/testdata/worker_drop_handle_race.js
new file mode 100644
index 000000000..d637ac8c2
--- /dev/null
+++ b/cli/tests/testdata/worker_drop_handle_race.js
@@ -0,0 +1,12 @@
+// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
+
+// https://github.com/denoland/deno/issues/11342
+// Test for a panic that happens when the main thread's event loop finishes
+// running while the worker's event loop is still spinning.
+
+// The exception thrown in the worker will not terminate the worker, but it will
+// propagate to the main thread and cause it to exit.
+new Worker(
+ new URL("./workers/drop_handle_race.js", import.meta.url).href,
+ { type: "module" },
+);
diff --git a/cli/tests/testdata/worker_drop_handle_race.js.out b/cli/tests/testdata/worker_drop_handle_race.js.out
new file mode 100644
index 000000000..71dfde620
--- /dev/null
+++ b/cli/tests/testdata/worker_drop_handle_race.js.out
@@ -0,0 +1,8 @@
+error: Uncaught (in worker "") Error
+ throw new Error();
+ ^
+ at [WILDCARD]/workers/drop_handle_race.js:2:9
+ at fire (deno:ext/timers/[WILDCARD])
+ at handleTimerMacrotask (deno:ext/timers/[WILDCARD])
+error: Uncaught (in promise) Error: Unhandled error event reached main worker.
+ at Worker.#pollControl (deno:runtime/js/11_workers.js:[WILDCARD])
diff --git a/cli/tests/testdata/workers/drop_handle_race.js b/cli/tests/testdata/workers/drop_handle_race.js
new file mode 100644
index 000000000..30676a600
--- /dev/null
+++ b/cli/tests/testdata/workers/drop_handle_race.js
@@ -0,0 +1,3 @@
+setTimeout(() => {
+ throw new Error();
+}, 1000);