2023-01-02 21:00:42 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2021-04-13 11:25:21 +00:00
|
|
|
|
2021-08-16 12:29:54 +00:00
|
|
|
use crate::ops::TestingFeaturesEnabled;
|
2021-10-13 17:04:44 +00:00
|
|
|
use crate::permissions::create_child_permissions;
|
|
|
|
use crate::permissions::ChildPermissionsArg;
|
2023-01-07 16:25:34 +00:00
|
|
|
use crate::permissions::PermissionsContainer;
|
2020-12-07 03:30:40 +00:00
|
|
|
use crate::web_worker::run_web_worker;
|
2021-06-22 14:30:16 +00:00
|
|
|
use crate::web_worker::SendableWebWorkerHandle;
|
2020-11-26 14:17:45 +00:00
|
|
|
use crate::web_worker::WebWorker;
|
|
|
|
use crate::web_worker::WebWorkerHandle;
|
2021-08-16 12:29:54 +00:00
|
|
|
use crate::web_worker::WebWorkerType;
|
2021-06-22 14:30:16 +00:00
|
|
|
use crate::web_worker::WorkerControlEvent;
|
2021-05-11 19:09:09 +00:00
|
|
|
use crate::web_worker::WorkerId;
|
2022-04-26 23:06:10 +00:00
|
|
|
use crate::worker::FormatJsErrorFn;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2023-09-14 21:05:18 +00:00
|
|
|
use deno_core::op2;
|
2021-01-06 20:31:16 +00:00
|
|
|
use deno_core::serde::Deserialize;
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
use deno_core::CancelFuture;
|
|
|
|
use deno_core::CancelHandle;
|
2020-04-23 09:51:07 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-10 13:57:45 +00:00
|
|
|
use deno_core::OpState;
|
2021-06-22 14:30:16 +00:00
|
|
|
use deno_web::JsMessageData;
|
2021-03-26 16:34:25 +00:00
|
|
|
use log::debug;
|
2020-09-10 13:57:45 +00:00
|
|
|
use std::cell::RefCell;
|
2020-09-19 23:17:35 +00:00
|
|
|
use std::collections::HashMap;
|
2020-08-18 16:30:13 +00:00
|
|
|
use std::rc::Rc;
|
2020-12-11 17:49:26 +00:00
|
|
|
use std::sync::Arc;
|
2019-10-11 18:41:54 +00:00
|
|
|
|
2023-11-01 22:15:08 +00:00
|
|
|
pub const UNSTABLE_FEATURE_NAME: &str = "worker-options";
|
2023-10-12 15:55:50 +00:00
|
|
|
|
2020-12-11 17:49:26 +00:00
|
|
|
pub struct CreateWebWorkerArgs {
|
|
|
|
pub name: String,
|
2021-05-11 19:09:09 +00:00
|
|
|
pub worker_id: WorkerId,
|
2023-01-07 16:25:34 +00:00
|
|
|
pub parent_permissions: PermissionsContainer,
|
|
|
|
pub permissions: PermissionsContainer,
|
2020-12-11 17:49:26 +00:00
|
|
|
pub main_module: ModuleSpecifier,
|
2021-08-16 12:29:54 +00:00
|
|
|
pub worker_type: WebWorkerType,
|
2020-12-11 17:49:26 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 14:30:16 +00:00
|
|
|
pub type CreateWebWorkerCb = dyn Fn(CreateWebWorkerArgs) -> (WebWorker, SendableWebWorkerHandle)
|
|
|
|
+ Sync
|
|
|
|
+ Send;
|
2020-12-11 17:49:26 +00:00
|
|
|
|
|
|
|
/// A holder for callback that is used to create a new
|
|
|
|
/// WebWorker. It's a struct instead of a type alias
|
|
|
|
/// because `GothamState` used in `OpState` overrides
|
2022-02-11 12:41:56 +00:00
|
|
|
/// value if type aliases have the same underlying type
|
2020-12-11 17:49:26 +00:00
|
|
|
#[derive(Clone)]
|
2022-08-17 01:00:35 +00:00
|
|
|
struct CreateWebWorkerCbHolder(Arc<CreateWebWorkerCb>);
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2022-04-26 23:06:10 +00:00
|
|
|
#[derive(Clone)]
|
2022-08-17 01:00:35 +00:00
|
|
|
struct FormatJsErrorFnHolder(Option<Arc<FormatJsErrorFn>>);
|
|
|
|
|
2021-01-06 20:31:16 +00:00
|
|
|
pub struct WorkerThread {
|
|
|
|
worker_handle: WebWorkerHandle,
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
cancel_handle: Rc<CancelHandle>,
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
|
|
|
|
// A WorkerThread that hasn't been explicitly terminated can only be removed
|
|
|
|
// from the WorkersTable once close messages have been received for both the
|
|
|
|
// control and message channels. See `close_channel`.
|
|
|
|
ctrl_closed: bool,
|
|
|
|
message_closed: bool,
|
2021-01-06 20:31:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 16:02:15 +00:00
|
|
|
impl WorkerThread {
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
fn terminate(self) {
|
|
|
|
// Cancel recv ops when terminating the worker, so they don't show up as
|
|
|
|
// pending ops.
|
|
|
|
self.cancel_handle.cancel();
|
2021-09-22 16:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for WorkerThread {
|
|
|
|
fn drop(&mut self) {
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
self.worker_handle.clone().terminate();
|
2021-09-22 16:02:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 19:09:09 +00:00
|
|
|
pub type WorkersTable = HashMap<WorkerId, WorkerThread>;
|
2021-01-06 20:31:16 +00:00
|
|
|
|
2023-03-17 18:22:15 +00:00
|
|
|
deno_core::extension!(
|
|
|
|
deno_worker_host,
|
|
|
|
ops = [
|
|
|
|
op_create_worker,
|
|
|
|
op_host_terminate_worker,
|
|
|
|
op_host_post_message,
|
|
|
|
op_host_recv_ctrl,
|
|
|
|
op_host_recv_message,
|
|
|
|
],
|
2023-03-17 22:15:27 +00:00
|
|
|
options = {
|
2023-03-17 18:22:15 +00:00
|
|
|
create_web_worker_cb: Arc<CreateWebWorkerCb>,
|
|
|
|
format_js_error_fn: Option<Arc<FormatJsErrorFn>>,
|
|
|
|
},
|
2023-03-17 22:15:27 +00:00
|
|
|
state = |state, options| {
|
2023-03-17 18:22:15 +00:00
|
|
|
state.put::<WorkersTable>(WorkersTable::default());
|
|
|
|
state.put::<WorkerId>(WorkerId::default());
|
|
|
|
|
|
|
|
let create_web_worker_cb_holder =
|
2023-03-17 22:15:27 +00:00
|
|
|
CreateWebWorkerCbHolder(options.create_web_worker_cb);
|
2023-03-17 18:22:15 +00:00
|
|
|
state.put::<CreateWebWorkerCbHolder>(create_web_worker_cb_holder);
|
|
|
|
let format_js_error_fn_holder =
|
2023-03-17 22:15:27 +00:00
|
|
|
FormatJsErrorFnHolder(options.format_js_error_fn);
|
2023-03-17 18:22:15 +00:00
|
|
|
state.put::<FormatJsErrorFnHolder>(format_js_error_fn_holder);
|
2023-03-18 22:30:04 +00:00
|
|
|
},
|
2023-03-17 18:22:15 +00:00
|
|
|
);
|
2020-02-11 09:04:59 +00:00
|
|
|
|
2019-08-26 12:50:21 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-03-18 18:42:01 +00:00
|
|
|
pub struct CreateWorkerArgs {
|
2019-08-26 12:50:21 +00:00
|
|
|
has_source_code: bool,
|
2021-01-06 20:31:16 +00:00
|
|
|
name: Option<String>,
|
2021-10-13 17:04:44 +00:00
|
|
|
permissions: Option<ChildPermissionsArg>,
|
2019-08-26 12:50:21 +00:00
|
|
|
source_code: String,
|
2021-01-06 20:31:16 +00:00
|
|
|
specifier: String,
|
2021-08-16 12:29:54 +00:00
|
|
|
worker_type: WebWorkerType,
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create worker as the host
|
2023-09-14 21:05:18 +00:00
|
|
|
#[op2]
|
|
|
|
#[serde]
|
2019-10-11 18:41:54 +00:00
|
|
|
fn op_create_worker(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-09-14 21:05:18 +00:00
|
|
|
#[serde] args: CreateWorkerArgs,
|
2021-04-05 16:40:24 +00:00
|
|
|
) -> Result<WorkerId, AnyError> {
|
2020-02-03 23:08:44 +00:00
|
|
|
let specifier = args.specifier.clone();
|
2020-04-16 21:40:29 +00:00
|
|
|
let maybe_source_code = if args.has_source_code {
|
|
|
|
Some(args.source_code.clone())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2020-02-03 23:08:44 +00:00
|
|
|
let args_name = args.name;
|
2021-08-16 12:29:54 +00:00
|
|
|
let worker_type = args.worker_type;
|
|
|
|
if let WebWorkerType::Classic = worker_type {
|
|
|
|
if let TestingFeaturesEnabled(false) = state.borrow() {
|
|
|
|
return Err(
|
|
|
|
deno_webstorage::DomExceptionNotSupportedError::new(
|
|
|
|
"Classic workers are not supported.",
|
|
|
|
)
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-10-13 17:04:44 +00:00
|
|
|
|
|
|
|
if args.permissions.is_some() {
|
2023-10-12 15:55:50 +00:00
|
|
|
super::check_unstable(
|
|
|
|
state,
|
|
|
|
UNSTABLE_FEATURE_NAME,
|
|
|
|
"Worker.deno.permissions",
|
|
|
|
);
|
2021-10-13 17:04:44 +00:00
|
|
|
}
|
2023-01-07 16:25:34 +00:00
|
|
|
let parent_permissions = state.borrow_mut::<PermissionsContainer>();
|
2021-10-13 17:04:44 +00:00
|
|
|
let worker_permissions = if let Some(child_permissions_arg) = args.permissions
|
|
|
|
{
|
2023-01-07 16:25:34 +00:00
|
|
|
let mut parent_permissions = parent_permissions.0.lock();
|
|
|
|
let perms =
|
|
|
|
create_child_permissions(&mut parent_permissions, child_permissions_arg)?;
|
|
|
|
PermissionsContainer::new(perms)
|
2021-01-06 20:31:16 +00:00
|
|
|
} else {
|
|
|
|
parent_permissions.clone()
|
|
|
|
};
|
2021-10-13 17:04:44 +00:00
|
|
|
let parent_permissions = parent_permissions.clone();
|
2020-09-19 23:17:35 +00:00
|
|
|
let worker_id = state.take::<WorkerId>();
|
2023-09-26 01:01:56 +00:00
|
|
|
let create_web_worker_cb = state.borrow::<CreateWebWorkerCbHolder>().clone();
|
|
|
|
let format_js_error_fn = state.borrow::<FormatJsErrorFnHolder>().clone();
|
2021-05-11 19:09:09 +00:00
|
|
|
state.put::<WorkerId>(worker_id.next().unwrap());
|
2020-02-08 19:34:31 +00:00
|
|
|
|
2021-02-17 18:47:18 +00:00
|
|
|
let module_specifier = deno_core::resolve_url(&specifier)?;
|
2022-11-18 01:59:10 +00:00
|
|
|
let worker_name = args_name.unwrap_or_default();
|
2020-12-07 03:30:40 +00:00
|
|
|
|
2021-06-22 14:30:16 +00:00
|
|
|
let (handle_sender, handle_receiver) = std::sync::mpsc::sync_channel::<
|
|
|
|
Result<SendableWebWorkerHandle, AnyError>,
|
|
|
|
>(1);
|
2020-12-07 03:30:40 +00:00
|
|
|
|
|
|
|
// Setup new thread
|
2023-01-27 15:43:16 +00:00
|
|
|
let thread_builder = std::thread::Builder::new().name(format!("{worker_id}"));
|
2020-12-07 03:30:40 +00:00
|
|
|
|
|
|
|
// Spawn it
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
thread_builder.spawn(move || {
|
2020-12-07 03:30:40 +00:00
|
|
|
// Any error inside this block is terminal:
|
|
|
|
// - JS worker is useless - meaning it throws an exception and can't do anything else,
|
|
|
|
// all action done upon it should be noops
|
|
|
|
// - newly spawned thread exits
|
2020-12-11 17:49:26 +00:00
|
|
|
|
2021-06-22 14:30:16 +00:00
|
|
|
let (worker, external_handle) =
|
2022-02-11 12:41:56 +00:00
|
|
|
(create_web_worker_cb.0)(CreateWebWorkerArgs {
|
2021-06-22 14:30:16 +00:00
|
|
|
name: worker_name,
|
|
|
|
worker_id,
|
|
|
|
parent_permissions,
|
|
|
|
permissions: worker_permissions,
|
|
|
|
main_module: module_specifier.clone(),
|
2021-08-16 12:29:54 +00:00
|
|
|
worker_type,
|
2021-06-22 14:30:16 +00:00
|
|
|
});
|
2020-12-07 03:30:40 +00:00
|
|
|
|
2021-05-11 19:09:09 +00:00
|
|
|
// Send thread safe handle from newly created worker to host thread
|
2021-06-22 14:30:16 +00:00
|
|
|
handle_sender.send(Ok(external_handle)).unwrap();
|
2020-12-07 03:30:40 +00:00
|
|
|
drop(handle_sender);
|
|
|
|
|
|
|
|
// At this point the only method of communication with host
|
|
|
|
// is using `worker.internal_channels`.
|
|
|
|
//
|
|
|
|
// Host can already push messages and interact with worker.
|
2022-02-11 12:41:56 +00:00
|
|
|
run_web_worker(
|
|
|
|
worker,
|
|
|
|
module_specifier,
|
|
|
|
maybe_source_code,
|
2022-04-26 23:06:10 +00:00
|
|
|
format_js_error_fn.0,
|
2022-02-11 12:41:56 +00:00
|
|
|
)
|
2020-12-07 03:30:40 +00:00
|
|
|
})?;
|
|
|
|
|
2021-05-11 19:09:09 +00:00
|
|
|
// Receive WebWorkerHandle from newly created worker
|
2020-12-07 03:30:40 +00:00
|
|
|
let worker_handle = handle_receiver.recv().unwrap()?;
|
|
|
|
|
|
|
|
let worker_thread = WorkerThread {
|
2021-06-22 14:30:16 +00:00
|
|
|
worker_handle: worker_handle.into(),
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
cancel_handle: CancelHandle::new_rc(),
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
ctrl_closed: false,
|
|
|
|
message_closed: false,
|
2020-12-07 03:30:40 +00:00
|
|
|
};
|
2020-01-17 23:43:53 +00:00
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
// At this point all interactions with worker happen using thread
|
2020-12-07 03:30:40 +00:00
|
|
|
// safe handler returned from previous function calls
|
2020-09-19 23:17:35 +00:00
|
|
|
state
|
|
|
|
.borrow_mut::<WorkersTable>()
|
2020-12-07 03:30:40 +00:00
|
|
|
.insert(worker_id, worker_thread);
|
2019-08-26 12:50:21 +00:00
|
|
|
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(worker_id)
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 21:05:18 +00:00
|
|
|
#[op2]
|
|
|
|
fn op_host_terminate_worker(state: &mut OpState, #[serde] id: WorkerId) {
|
2021-07-16 22:51:06 +00:00
|
|
|
if let Some(worker_thread) = state.borrow_mut::<WorkersTable>().remove(&id) {
|
2021-09-22 16:02:15 +00:00
|
|
|
worker_thread.terminate();
|
2021-07-16 22:51:06 +00:00
|
|
|
} else {
|
|
|
|
debug!("tried to terminate non-existent worker {}", id);
|
|
|
|
}
|
2020-01-17 23:43:53 +00:00
|
|
|
}
|
|
|
|
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
enum WorkerChannel {
|
|
|
|
Ctrl,
|
|
|
|
Messages,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Close a worker's channel. If this results in both of a worker's channels
|
|
|
|
/// being closed, the worker will be removed from the workers table.
|
|
|
|
fn close_channel(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
|
|
|
id: WorkerId,
|
|
|
|
channel: WorkerChannel,
|
|
|
|
) {
|
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
|
2020-12-07 03:30:40 +00:00
|
|
|
let mut s = state.borrow_mut();
|
|
|
|
let workers = s.borrow_mut::<WorkersTable>();
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
|
|
|
|
// `Worker.terminate()` might have been called already, meaning that we won't
|
|
|
|
// find the worker in the table - in that case ignore.
|
|
|
|
if let Entry::Occupied(mut entry) = workers.entry(id) {
|
|
|
|
let terminate = {
|
|
|
|
let worker_thread = entry.get_mut();
|
|
|
|
match channel {
|
|
|
|
WorkerChannel::Ctrl => {
|
|
|
|
worker_thread.ctrl_closed = true;
|
|
|
|
worker_thread.message_closed
|
|
|
|
}
|
|
|
|
WorkerChannel::Messages => {
|
|
|
|
worker_thread.message_closed = true;
|
|
|
|
worker_thread.ctrl_closed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if terminate {
|
2021-09-22 16:02:15 +00:00
|
|
|
entry.remove().terminate();
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
}
|
2020-12-07 03:30:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-22 14:30:16 +00:00
|
|
|
/// Get control event from guest worker as host
|
2023-09-14 21:05:18 +00:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2021-06-22 14:30:16 +00:00
|
|
|
async fn op_host_recv_ctrl(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 21:05:18 +00:00
|
|
|
#[serde] id: WorkerId,
|
2021-06-22 14:30:16 +00:00
|
|
|
) -> Result<WorkerControlEvent, AnyError> {
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
let (worker_handle, cancel_handle) = {
|
2021-06-22 14:30:16 +00:00
|
|
|
let state = state.borrow();
|
|
|
|
let workers_table = state.borrow::<WorkersTable>();
|
2020-09-10 13:57:45 +00:00
|
|
|
let maybe_handle = workers_table.get(&id);
|
|
|
|
if let Some(handle) = maybe_handle {
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
(handle.worker_handle.clone(), handle.cancel_handle.clone())
|
2020-09-10 13:57:45 +00:00
|
|
|
} else {
|
|
|
|
// If handle was not found it means worker has already shutdown
|
2021-06-22 14:30:16 +00:00
|
|
|
return Ok(WorkerControlEvent::Close);
|
2020-09-10 13:57:45 +00:00
|
|
|
}
|
2020-08-28 15:08:24 +00:00
|
|
|
};
|
|
|
|
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
let maybe_event = worker_handle
|
|
|
|
.get_control_event()
|
|
|
|
.or_cancel(cancel_handle)
|
|
|
|
.await;
|
|
|
|
match maybe_event {
|
|
|
|
Ok(Ok(Some(event))) => {
|
|
|
|
// Terminal error means that worker should be removed from worker table.
|
|
|
|
if let WorkerControlEvent::TerminalError(_) = &event {
|
|
|
|
close_channel(state, id, WorkerChannel::Ctrl);
|
|
|
|
}
|
|
|
|
Ok(event)
|
|
|
|
}
|
|
|
|
Ok(Ok(None)) => {
|
|
|
|
// If there was no event from worker it means it has already been closed.
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
close_channel(state, id, WorkerChannel::Ctrl);
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
Ok(WorkerControlEvent::Close)
|
|
|
|
}
|
|
|
|
Ok(Err(err)) => Err(err),
|
|
|
|
Err(_) => {
|
|
|
|
// The worker was terminated.
|
|
|
|
Ok(WorkerControlEvent::Close)
|
2020-08-28 15:08:24 +00:00
|
|
|
}
|
2020-12-07 03:30:40 +00:00
|
|
|
}
|
2021-06-22 14:30:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-14 21:05:18 +00:00
|
|
|
#[op2(async)]
|
|
|
|
#[serde]
|
2021-06-22 14:30:16 +00:00
|
|
|
async fn op_host_recv_message(
|
|
|
|
state: Rc<RefCell<OpState>>,
|
2023-09-14 21:05:18 +00:00
|
|
|
#[serde] id: WorkerId,
|
2021-06-22 14:30:16 +00:00
|
|
|
) -> Result<Option<JsMessageData>, AnyError> {
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
let (worker_handle, cancel_handle) = {
|
2021-06-22 14:30:16 +00:00
|
|
|
let s = state.borrow();
|
|
|
|
let workers_table = s.borrow::<WorkersTable>();
|
|
|
|
let maybe_handle = workers_table.get(&id);
|
|
|
|
if let Some(handle) = maybe_handle {
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
(handle.worker_handle.clone(), handle.cancel_handle.clone())
|
2021-06-22 14:30:16 +00:00
|
|
|
} else {
|
|
|
|
// If handle was not found it means worker has already shutdown
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
};
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
|
fix(workers): Make `worker.terminate()` not block the current thread (#13941)
Calling `worker.terminate()` used to kill the worker's isolate and
then block until the worker's thread finished. This blocks the calling
thread if the worker's event loop was blocked in a sync op (as with
`Deno.sleepSync`), which wasn't realized at the time, but since the
worker's isolate was killed at that moment, it would not block the
calling thread if the worker was in a JS endless loop.
However, in #12831, in order to work around a V8 bug, worker
termination was changed to first set a signal to let the worker event
loop know that termination has been requested, and only kill the
isolate if the event loop has not finished after 2 seconds. However,
this change kept the blocking, which meant that JS endless loops in
the worker now blocked the parent for 2 seconds.
As it turns out, after #12831 it is fine to signal termination and
even kill the worker's isolate without waiting for the thread to
finish, so this change does that. However, that might leave the async
ops that receive messages and control data from the worker pending
after `worker.terminate()`, which leads to odd results from the op
sanitizer. Therefore, we set up a `CancelHandler` to cancel those ops
when the worker is terminated.
2022-04-27 16:22:47 +00:00
|
|
|
let ret = worker_handle
|
|
|
|
.port
|
|
|
|
.recv(state.clone())
|
|
|
|
.or_cancel(cancel_handle)
|
|
|
|
.await;
|
|
|
|
match ret {
|
|
|
|
Ok(Ok(ret)) => {
|
|
|
|
if ret.is_none() {
|
|
|
|
close_channel(state, id, WorkerChannel::Messages);
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
Ok(Err(err)) => Err(err),
|
|
|
|
Err(_) => {
|
|
|
|
// The worker was terminated.
|
|
|
|
Ok(None)
|
|
|
|
}
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 09:05:02 +00:00
|
|
|
}
|
2019-08-26 12:50:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-14 15:03:02 +00:00
|
|
|
/// Post message to guest worker as host
|
2023-09-14 21:05:18 +00:00
|
|
|
#[op2]
|
2019-10-11 18:41:54 +00:00
|
|
|
fn op_host_post_message(
|
2020-09-10 13:57:45 +00:00
|
|
|
state: &mut OpState,
|
2023-09-14 21:05:18 +00:00
|
|
|
#[serde] id: WorkerId,
|
|
|
|
#[serde] data: JsMessageData,
|
2021-04-05 16:40:24 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2021-07-16 22:51:06 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-04-05 16:40:24 +00:00
|
|
|
Ok(())
|
2019-08-14 15:03:02 +00:00
|
|
|
}
|