Skip to content

Commit 3da464f

Browse files
authored
fix(app): Handle manual disconnect for serial transport (#55)
* Properly cancel the port read process when manually disconnected
1 parent 42f4966 commit 3da464f

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src-tauri/src/transport/serial.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,11 @@ pub async fn serial_connect(
2424

2525
let (mut reader, mut writer) = tokio::io::split(port);
2626

27+
let ahc = app_handle.clone();
2728
let (send, mut recv) = channel(5);
2829
*state.conn.lock().await = Some(Box::new(send));
29-
tauri::async_runtime::spawn(async move {
30-
while let Some(data) = recv.next().await {
31-
let _res = writer.write(&data).await;
32-
}
33-
});
3430

35-
tauri::async_runtime::spawn(async move {
31+
let read_process = tauri::async_runtime::spawn(async move {
3632
use tauri::Manager;
3733
use tauri::Emitter;
3834

@@ -51,6 +47,18 @@ pub async fn serial_connect(
5147
app_handle.emit("connection_disconnected", ());
5248
});
5349

50+
tauri::async_runtime::spawn(async move {
51+
use tauri::Manager;
52+
53+
while let Some(data) = recv.next().await {
54+
let _res = writer.write(&data).await;
55+
}
56+
57+
let state = ahc.state::<super::commands::ActiveConnection>();
58+
read_process.abort();
59+
*state.conn.lock().await = None;
60+
});
61+
5462
Ok(true)
5563
}
5664
Err(e) => {

0 commit comments

Comments
 (0)