From c4fe58d8df51e8f07142803ec0522f862fd30a3f Mon Sep 17 00:00:00 2001 From: Yiyu Lin Date: Sun, 17 May 2020 12:50:38 +0800 Subject: [PATCH] Return error if more than one listener calls `WorkerHandle::get_event()` (#5461) --- cli/ops/worker_host.rs | 2 +- cli/tsc.rs | 2 +- cli/web_worker.rs | 8 ++++---- cli/worker.rs | 10 +++++----- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs index 479fcceafc..d1b4bb80f1 100644 --- a/cli/ops/worker_host.rs +++ b/cli/ops/worker_host.rs @@ -306,7 +306,7 @@ fn op_host_get_message( }; let state_ = state.clone(); let op = async move { - let response = match worker_handle.get_event().await { + let response = match worker_handle.get_event().await? { Some(event) => { // Terminal error means that worker should be removed from worker table. if let WorkerEvent::TerminalError(_) = &event { diff --git a/cli/tsc.rs b/cli/tsc.rs index 483d849632..4d1d2368cd 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -794,7 +794,7 @@ async fn execute_in_thread( })?; let handle = handle_receiver.recv().unwrap()?; handle.post_message(req)?; - let event = handle.get_event().await.expect("Compiler didn't respond"); + let event = handle.get_event().await?.expect("Compiler didn't respond"); let buf = match event { WorkerEvent::Message(buf) => Ok(buf), WorkerEvent::Error(error) => Err(error), diff --git a/cli/web_worker.rs b/cli/web_worker.rs index 490d9a5f32..46f03da363 100644 --- a/cli/web_worker.rs +++ b/cli/web_worker.rs @@ -300,13 +300,13 @@ mod tests { let r = handle.post_message(msg.clone()); assert!(r.is_ok()); - let maybe_msg = handle.get_event().await; + let maybe_msg = handle.get_event().await.unwrap(); assert!(maybe_msg.is_some()); let r = handle.post_message(msg.clone()); assert!(r.is_ok()); - let maybe_msg = handle.get_event().await; + let maybe_msg = handle.get_event().await.unwrap(); assert!(maybe_msg.is_some()); match maybe_msg { Some(WorkerEvent::Message(buf)) => { @@ -321,7 +321,7 @@ mod tests { .into_boxed_bytes(); let r = handle.post_message(msg); assert!(r.is_ok()); - let event = handle.get_event().await; + let event = handle.get_event().await.unwrap(); assert!(event.is_none()); handle.sender.close_channel(); }); @@ -348,7 +348,7 @@ mod tests { let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes(); let r = handle.post_message(msg.clone()); assert!(r.is_ok()); - let event = handle.get_event().await; + let event = handle.get_event().await.unwrap(); assert!(event.is_none()); handle.sender.close_channel(); }); diff --git a/cli/worker.rs b/cli/worker.rs index d52d965944..b66c1cb06b 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -51,11 +51,11 @@ impl WorkerHandle { sender.try_send(buf).map_err(ErrBox::from) } - // TODO: should use `try_lock` and return error if - // more than one listener tries to get event - pub async fn get_event(&self) -> Option { - let mut receiver = self.receiver.lock().await; - receiver.next().await + /// Get the event with lock. + /// Return error if more than one listener tries to get event + pub async fn get_event(&self) -> Result, ErrBox> { + let mut receiver = self.receiver.try_lock()?; + Ok(receiver.next().await) } }