diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-03-11 22:18:03 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-11 23:18:03 +0100 |
commit | d69aab62b0789dd54b8c09b54af022a38f060b5b (patch) | |
tree | e99a5a3217d6aeee379bc592bfa33702dc2d6de8 /runtime/web_worker.rs | |
parent | 28b362adfc49324e20af5ecb1530f89eb91c4ed5 (diff) |
fix(ext/node): make worker setup synchronous (#22815)
This commit fixes race condition in "node:worker_threads" module were
the first message did a setup of "threadId", "workerData" and
"environmentData".
Now this data is passed explicitly during workers creation and is set up
before any user code is executed.
Closes https://github.com/denoland/deno/issues/22783
Closes https://github.com/denoland/deno/issues/22672
---------
Co-authored-by: Satya Rohith <me@satyarohith.com>
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r-- | runtime/web_worker.rs | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index 6571da6c2..f35d38921 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -48,6 +48,7 @@ use deno_terminal::colors; use deno_tls::RootCertStoreProvider; use deno_web::create_entangled_message_port; use deno_web::BlobStore; +use deno_web::JsMessageData; use deno_web::MessagePort; use log::debug; use std::cell::RefCell; @@ -331,6 +332,8 @@ pub struct WebWorker { pub main_module: ModuleSpecifier, poll_for_messages_fn: Option<v8::Global<v8::Value>>, bootstrap_fn_global: Option<v8::Global<v8::Function>>, + // Consumed when `bootstrap_fn` is called + maybe_worker_metadata: Option<JsMessageData>, } pub struct WebWorkerOptions { @@ -356,6 +359,7 @@ pub struct WebWorkerOptions { pub cache_storage_dir: Option<std::path::PathBuf>, pub stdio: Stdio, pub feature_checker: Arc<FeatureChecker>, + pub maybe_worker_metadata: Option<JsMessageData>, } impl WebWorker { @@ -601,6 +605,7 @@ impl WebWorker { main_module, poll_for_messages_fn: None, bootstrap_fn_global: Some(bootstrap_fn_global), + maybe_worker_metadata: options.maybe_worker_metadata, }, external_handle, ) @@ -616,6 +621,10 @@ impl WebWorker { let bootstrap_fn = self.bootstrap_fn_global.take().unwrap(); let bootstrap_fn = v8::Local::new(scope, bootstrap_fn); let undefined = v8::undefined(scope); + let mut worker_data: v8::Local<v8::Value> = v8::undefined(scope).into(); + if let Some(data) = self.maybe_worker_metadata.take() { + worker_data = deno_core::serde_v8::to_v8(scope, data).unwrap(); + } let name_str: v8::Local<v8::Value> = v8::String::new(scope, &self.name).unwrap().into(); let id_str: v8::Local<v8::Value> = @@ -623,7 +632,11 @@ impl WebWorker { .unwrap() .into(); bootstrap_fn - .call(scope, undefined.into(), &[args, name_str, id_str]) + .call( + scope, + undefined.into(), + &[args, name_str, id_str, worker_data], + ) .unwrap(); } // TODO(bartlomieju): this could be done using V8 API, without calling `execute_script`. |