1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-26 00:59:24 -05:00

fix(workers): silently ignore non-existent worker IDs (#11417)

Fixes #11416
This commit is contained in:
Andreu Botella 2021-07-17 00:51:06 +02:00 committed by GitHub
parent 91bbd1a6d5
commit 240545282a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 19 deletions

View file

@ -1160,6 +1160,11 @@ itest!(worker_event_handler_test {
output: "worker_event_handler_test.js.out",
});
itest!(worker_close_race {
args: "run --quiet --reload --allow-read worker_close_race.js",
output: "worker_close_race.js.out",
});
#[test]
fn no_validate_asm() {
let output = util::deno_cmd()

View file

@ -0,0 +1,14 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
// https://github.com/denoland/deno/issues/11416
// Test for a race condition between a worker's `close()` and the main thread's
// `Worker.prototype.terminate()`.
const worker = new Worker(
new URL("./workers/close_race_worker.js", import.meta.url),
{ type: "module" },
);
worker.onmessage = () => {
worker.terminate();
};

View file

View file

@ -0,0 +1,6 @@
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
setTimeout(() => {
self.postMessage("");
self.close();
}, 500);

View file

@ -516,16 +516,16 @@ fn op_host_terminate_worker(
id: WorkerId,
_: (),
) -> Result<(), AnyError> {
let worker_thread = state
.borrow_mut::<WorkersTable>()
.remove(&id)
.expect("No worker handle found");
worker_thread.worker_handle.terminate();
worker_thread
.join_handle
.join()
.expect("Panic in worker thread")
.expect("Panic in worker event loop");
if let Some(worker_thread) = state.borrow_mut::<WorkersTable>().remove(&id) {
worker_thread.worker_handle.terminate();
worker_thread
.join_handle
.join()
.expect("Panic in worker thread")
.expect("Panic in worker event loop");
} else {
debug!("tried to terminate non-existent worker {}", id);
}
Ok(())
}
@ -602,14 +602,12 @@ fn op_host_post_message(
id: WorkerId,
data: JsMessageData,
) -> Result<(), AnyError> {
debug!("post message to worker {}", id);
let worker_handle = {
let worker_thread = state
.borrow::<WorkersTable>()
.get(&id)
.expect("No worker handle found");
worker_thread.worker_handle.clone()
};
worker_handle.port.send(state, data)?;
if let Some(worker_thread) = state.borrow::<WorkersTable>().get(&id) {
debug!("post message to worker {}", id);
let worker_handle = worker_thread.worker_handle.clone();
worker_handle.port.send(state, data)?;
} else {
debug!("tried to post message to non-existent worker {}", id);
}
Ok(())
}