Commit fd5653ac1d

fd5653ac1dfe7f6dcceee7e47d0267e8888721af

parent: 06f76c3c7f

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2019-11-16 20:07 UTC

executor: Return JoinHandles instead of dropping them.

Layout: unified · split

src/executor.rs +6 −5
@@ -1,4 +1,4 @@
1use async_task;
1use async_task::JoinHandle;
22use pipe_channel::{channel, Receiver, Sender};
33use std::collections::VecDeque;
44use std::future::Future;
@@ -33,10 +33,10 @@ static mut _FUTURE_QUEUE: Option<FutureQueue> = None;
3333
3434type FutureQueue = Arc<Mutex<VecDeque<Job>>>;
3535
36pub fn spawn_weechat<F, T>(future: F)
36pub fn spawn_weechat<F, R>(future: F) -> JoinHandle<R, ()>
3737where
38 F: Future<Output = T> + 'static,
39 T: 'static,
38 F: Future<Output = R> + 'static,
39 R: 'static,
4040{
4141 let weechat = unsafe { Weechat::weechat() };
4242
@@ -83,8 +83,9 @@ where
8383 weechat_notify.send(()).unwrap();
8484 };
8585
86 let (task, _handle) = async_task::spawn(future, schedule, ());
86 let (task, handle) = async_task::spawn(future, schedule, ());
8787 task.schedule();
88 handle
8889}
8990
9091pub fn cleanup_executor() {
src/server.rs +6 −2
@@ -1,5 +1,6 @@
11use async_std::sync::channel as async_channel;
22use async_std::sync::{Receiver, Sender};
3use async_task::JoinHandle;
34use std::collections::HashMap;
45use std::time::Duration;
56use tokio::runtime::Runtime;
@@ -50,6 +51,7 @@ pub struct LoginState {
5051
5152struct ConnectedState {
5253 client_channel: Sender<ServerMessage>,
54 sync_receiver: JoinHandle<(), ()>,
5355 homeserver: Url,
5456 runtime: Runtime,
5557}
@@ -95,13 +97,14 @@ impl MatrixServer {
9597
9698 let (tx, rx) = async_channel(1000);
9799 runtime.spawn(MatrixServer::sync_loop(client, tx));
98 spawn_weechat(MatrixServer::sync_receiver(rx));
100 let sync_receiver_handle =
101 spawn_weechat(MatrixServer::sync_receiver(rx));
99102
100103 let (client_sender, client_receiver) = async_channel(10);
101
102104 runtime.spawn(MatrixServer::send_loop(send_client, client_receiver));
103105
104106 self.connected_state = Some(ConnectedState {
107 sync_receiver: sync_receiver_handle,
105108 client_channel: client_sender,
106109 homeserver,
107110 runtime,
@@ -112,6 +115,7 @@ impl MatrixServer {
112115 let connected_state = self.connected_state.take();
113116 if let Some(s) = connected_state {
114117 s.runtime.shutdown_now();
118 s.sync_receiver.cancel();
115119 }
116120 }
117121