mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
fix(workers): silently ignore non-existent worker IDs (#11417)
Fixes #11416
This commit is contained in:
parent
91bbd1a6d5
commit
240545282a
5 changed files with 42 additions and 19 deletions
|
@ -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()
|
||||
|
|
14
cli/tests/worker_close_race.js
Normal file
14
cli/tests/worker_close_race.js
Normal 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();
|
||||
};
|
0
cli/tests/worker_close_race.js.out
Normal file
0
cli/tests/worker_close_race.js.out
Normal file
6
cli/tests/workers/close_race_worker.js
Normal file
6
cli/tests/workers/close_race_worker.js
Normal file
|
@ -0,0 +1,6 @@
|
|||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
||||
|
||||
setTimeout(() => {
|
||||
self.postMessage("");
|
||||
self.close();
|
||||
}, 500);
|
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue