summaryrefslogtreecommitdiff
path: root/runtime/web_worker.rs
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2021-03-04 12:19:47 +0000
committerGitHub <noreply@github.com>2021-03-04 13:19:47 +0100
commit0f2121355f65baa27b530ef286c8b4ca0009fabf (patch)
tree646725573df762eb09035dd7533e9d4d40280461 /runtime/web_worker.rs
parentaf7e02124fbec3cd3b2d17f6fa88682b826e455e (diff)
fix(runtime/web_worker): Don't block self.onmessage with TLA (#9619)
This commit rewrites implementation of "JsRuntime::mod_evaluate". Event loop is no longer polled automatically and users must manually drive event loop forward after calling "mod_evaluate". Co-authored-by: Nayeem Rahman <nayeemrmn99@gmail.com> Co-authored-by: Bartek IwaƄczuk <biwanczuk@gmail.com>
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r--runtime/web_worker.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs
index 30869ff41..73d351c9c 100644
--- a/runtime/web_worker.rs
+++ b/runtime/web_worker.rs
@@ -325,7 +325,28 @@ impl WebWorker {
module_specifier: &ModuleSpecifier,
) -> Result<(), AnyError> {
let id = self.js_runtime.load_module(module_specifier, None).await?;
- self.js_runtime.mod_evaluate(id).await
+
+ let mut receiver = self.js_runtime.mod_evaluate(id);
+ tokio::select! {
+ maybe_result = receiver.next() => {
+ debug!("received worker module evaluate {:#?}", maybe_result);
+ // If `None` is returned it means that runtime was destroyed before
+ // evaluation was complete. This can happen in Web Worker when `self.close()`
+ // is called at top level.
+ let result = maybe_result.unwrap_or(Ok(()));
+ return result;
+ }
+
+ event_loop_result = self.run_event_loop() => {
+ if self.has_been_terminated() {
+ return Ok(());
+ }
+ event_loop_result?;
+ let maybe_result = receiver.next().await;
+ let result = maybe_result.unwrap_or(Ok(()));
+ return result;
+ }
+ }
}
/// Returns a way to communicate with the Worker from other threads.
@@ -384,6 +405,8 @@ impl WebWorker {
let msg = String::from_utf8(msg.to_vec()).unwrap();
let script = format!("workerMessageRecvCallback({})", msg);
+ // TODO(bartlomieju): set proper script name like "deno:runtime/web_worker.js"
+ // so it's dimmed in stack trace instead of using "__anonymous__"
if let Err(e) = self.execute(&script) {
// If execution was terminated during message callback then
// just ignore it