From 3da464f892edfe3a459de78b5da862fa938cf3b4 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Wed, 9 Oct 2024 13:46:52 -0600 Subject: [PATCH] fix(app): Handle manual disconnect for serial transport (#55) * Properly cancel the port read process when manually disconnected --- src-tauri/src/transport/serial.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/transport/serial.rs b/src-tauri/src/transport/serial.rs index fefaf86..eefa4e2 100644 --- a/src-tauri/src/transport/serial.rs +++ b/src-tauri/src/transport/serial.rs @@ -24,15 +24,11 @@ pub async fn serial_connect( let (mut reader, mut writer) = tokio::io::split(port); + let ahc = app_handle.clone(); let (send, mut recv) = channel(5); *state.conn.lock().await = Some(Box::new(send)); - tauri::async_runtime::spawn(async move { - while let Some(data) = recv.next().await { - let _res = writer.write(&data).await; - } - }); - tauri::async_runtime::spawn(async move { + let read_process = tauri::async_runtime::spawn(async move { use tauri::Manager; use tauri::Emitter; @@ -51,6 +47,18 @@ pub async fn serial_connect( app_handle.emit("connection_disconnected", ()); }); + tauri::async_runtime::spawn(async move { + use tauri::Manager; + + while let Some(data) = recv.next().await { + let _res = writer.write(&data).await; + } + + let state = ahc.state::(); + read_process.abort(); + *state.conn.lock().await = None; + }); + Ok(true) } Err(e) => {