diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-01-18 00:43:53 +0100 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2020-01-17 18:43:53 -0500 |
commit | 5fa056e53be6d17ab746629ea0eaa89fe817141b (patch) | |
tree | c32c4ab39577a2dc14da3843e3223395c5a7ea54 /cli/worker.rs | |
parent | d7203092039d3c46ca8480ff8eecf612deb2b2f6 (diff) |
workers: minimal error handling and async module loading (#3665)
Diffstat (limited to 'cli/worker.rs')
-rw-r--r-- | cli/worker.rs | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/cli/worker.rs b/cli/worker.rs index 2b335127f..7faf17e60 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -12,6 +12,7 @@ use futures::future::FutureExt; use futures::future::TryFutureExt; use futures::sink::SinkExt; use futures::stream::StreamExt; +use futures::task::AtomicWaker; use std::env; use std::future::Future; use std::pin::Pin; @@ -159,6 +160,11 @@ impl Worker { channels: self.external_channels.clone(), } } + + pub fn clear_exception(&mut self) { + let mut isolate = self.isolate.try_lock().unwrap(); + isolate.clear_exception(); + } } impl Future for Worker { @@ -166,8 +172,15 @@ impl Future for Worker { fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { let inner = self.get_mut(); - let mut isolate = inner.isolate.try_lock().unwrap(); - isolate.poll_unpin(cx) + let waker = AtomicWaker::new(); + waker.register(cx.waker()); + match inner.isolate.try_lock() { + Ok(mut isolate) => isolate.poll_unpin(cx), + Err(_) => { + waker.wake(); + Poll::Pending + } + } } } @@ -436,7 +449,7 @@ mod tests { let worker_ = worker.clone(); let worker_future = async move { - let result = worker.await; + let result = worker_.await; println!("workers.rs after resource close"); result.unwrap(); } @@ -446,10 +459,10 @@ mod tests { tokio::spawn(worker_future_); let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes(); - let r = block_on(worker_.post_message(msg)); + let r = block_on(worker.post_message(msg)); assert!(r.is_ok()); - block_on(worker_future); + block_on(worker_future) }) } |