Skip to content

Commit 2d8ee3f

Browse files
committed
Fix panic when using tray Exit menu
The tray thread was terminating after receiving the menu event, causing the SSE thread panic when trying to notify the thread to stop. Now, the tray thread will notify the SSE thread to stop but continue running until the SSE thread notifies it.
1 parent acfedb1 commit 2d8ee3f

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

src/main.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use tray_item::{IconSource, TrayItem};
1111

1212
enum Message {
1313
Exit,
14+
Done,
1415
}
1516
fn main() {
1617
let logger = Box::new(SseLogger::new());
@@ -29,7 +30,8 @@ fn main() {
2930
let tray_stop = sse_clock.get_stop_notify();
3031

3132
ctrlc::set_handler(move || {
32-
info!("Received terminate event");
33+
info!("Received Ctrl-C event");
34+
// Notify the SSE clock thread to stop
3335
ctrlc_stop.stop();
3436
})
3537
.expect("Unable to set terminate handler");
@@ -58,17 +60,17 @@ fn tray_loop(tx: SyncSender<Message>, rx: Receiver<Message>, tray_stop: StopNoti
5860
})
5961
.expect("Failed to create Exit menu item");
6062

61-
// Currently, all results lead to exiting. If we add new types of messages
62-
// we may need to add a loop
63-
match rx.recv() {
64-
Ok(Message::Exit) => {}
65-
Err(err) => {
66-
warn!("{err:?}");
63+
while let Ok(msg) = rx.recv() {
64+
match msg {
65+
Message::Exit => {
66+
info!("Received menu Exit event");
67+
// Notify the SSE clock thread to stop
68+
tray_stop.stop();
69+
}
70+
71+
Message::Done => break, // SSE clock thread is done => terminate tray thread
6772
}
6873
}
69-
70-
// notify the SSE clock thread to stop
71-
tray_stop.stop();
7274
}
7375

7476
fn sse_loop(mut sse_clock: SseClock, tx: SyncSender<Message>) {
@@ -77,6 +79,6 @@ fn sse_loop(mut sse_clock: SseClock, tx: SyncSender<Message>) {
7779
}
7880

7981
// Notify the tray thread to stop
80-
tx.send(Message::Exit)
82+
tx.send(Message::Done)
8183
.expect("Failed to send Exit message from SSE loop");
8284
}

0 commit comments

Comments
 (0)