diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-12-20 00:34:22 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-20 00:34:22 +0100 |
commit | e924bbdf3606e83ff9eef3a8ed640c4ecc34444f (patch) | |
tree | 19847fac129a678c1e5efa5f3495c8414146fa22 /runtime/web_worker.rs | |
parent | 660f75e066226a635375b70225df165bcf759077 (diff) |
fix: TLA in web worker (#8809)
Implementors of `deno_core::JsRuntime` might want to do additional actions
during each turn of event loop, eg. `deno_runtime::Worker` polls inspector,
`deno_runtime::WebWorker` receives/dispatches messages from/to worker host.
Previously `JsRuntime::mod_evaluate` was implemented in such fashion that it
only polled `JsRuntime`'s event loop. This behavior turned out to be wrong
in the example of `WebWorker` which couldn't receive/dispatch messages because
its implementation of event loop was never called.
This commit rewrites "mod_evaluate" to return a handle to receiver that resolves
when module's promise resolves. It is now implementors responsibility to poll
event loop after calling `mod_evaluate`.
Diffstat (limited to 'runtime/web_worker.rs')
-rw-r--r-- | runtime/web_worker.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/runtime/web_worker.rs b/runtime/web_worker.rs index c1713f815..235cb2c7e 100644 --- a/runtime/web_worker.rs +++ b/runtime/web_worker.rs @@ -315,7 +315,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. @@ -374,6 +395,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 |