forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(workers): Don't panic when a worker's parent thread stops running (…
…denoland#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 denoland#11342. Co-authored-by: Bartek Iwańczuk <[email protected]>
- Loading branch information
1 parent
d796eea
commit 2659a5f
Showing
6 changed files
with
65 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" }, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
setTimeout(() => { | ||
throw new Error(); | ||
}, 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters