diff options
author | Luca Casonato <hello@lcas.dev> | 2021-08-18 15:19:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-18 15:19:22 +0200 |
commit | a66218d457859fc3498db9fbc61228c74f490cc2 (patch) | |
tree | cf099a0d7cfaeb5e6c10d55473bfc0b4ceb9c8d9 /runtime/web_worker.rs | |
parent | 480cfda8d5a5560fe0cc3ac2d9a3b798bf23f060 (diff) |
fix(runtime): event loop panics in classic workers (#11756)
Classic worker scripts are now executed in the context of a Tokio
runtime. This does mean we can not spawn more tokio runtimes in
"op_worker_sync_fetch". We instead spawn a new thread there, that can
create a new Tokio runtime that we can use to block the worker thread.
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r-- | runtime/web_worker.rs | 55 |
1 files changed, 28 insertions, 27 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 240d79d1f..2594b9049 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -11,7 +11,6 @@ use deno_core::error::AnyError; use deno_core::error::JsError; use deno_core::futures::channel::mpsc; use deno_core::futures::future::poll_fn; -use deno_core::futures::future::FutureExt; use deno_core::futures::stream::StreamExt; use deno_core::located_script_name; use deno_core::serde::Deserialize; @@ -568,36 +567,38 @@ pub fn run_web_worker( // TODO(bartlomieju): run following block using "select!" // with terminate - // Execute provided source code immediately - let result = if let Some(source_code) = maybe_source_code { - worker.execute_script(&located_script_name!(), &source_code) - } else { - // TODO(bartlomieju): add "type": "classic", ie. ability to load - // script instead of module - let load_future = worker.execute_module(&specifier).boxed_local(); + let fut = async move { + // Execute provided source code immediately + let result = if let Some(source_code) = maybe_source_code { + worker.execute_script(&located_script_name!(), &source_code) + } else { + // TODO(bartlomieju): add "type": "classic", ie. ability to load + // script instead of module + worker.execute_module(&specifier).await + }; - rt.block_on(load_future) - }; + let internal_handle = worker.internal_handle.clone(); - let internal_handle = worker.internal_handle.clone(); + // If sender is closed it means that worker has already been closed from + // within using "globalThis.close()" + if internal_handle.is_terminated() { + return Ok(()); + } - // If sender is closed it means that worker has already been closed from - // within using "globalThis.close()" - if internal_handle.is_terminated() { - return Ok(()); - } + if let Err(e) = result { + print_worker_error(e.to_string(), &name); + internal_handle + .post_event(WorkerControlEvent::TerminalError(e)) + .expect("Failed to post message to host"); - if let Err(e) = result { - print_worker_error(e.to_string(), &name); - internal_handle - .post_event(WorkerControlEvent::TerminalError(e)) - .expect("Failed to post message to host"); + // Failure to execute script is a terminal error, bye, bye. + return Ok(()); + } - // Failure to execute script is a terminal error, bye, bye. - return Ok(()); - } + let result = worker.run_event_loop(true).await; + debug!("Worker thread shuts down {}", &name); + result + }; - let result = rt.block_on(worker.run_event_loop(true)); - debug!("Worker thread shuts down {}", &name); - result + rt.block_on(fut) } |