Skip to content

Commit 2639cf0

Browse files
committed
bluetooth: try to cleanup running tasks before next connection attempts
1 parent 0462549 commit 2639cf0

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/bluetooth.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ async fn power_up_and_wait_for_connection(
205205
}
206206

207207
// handling connection to headset profile in own task
208-
let task_hsp = {
208+
let mut task_hsp = {
209209
if let Some(mut handle_hsp) = handle_hsp {
210210
Some(tokio::spawn(async move {
211211
let req = handle_hsp
@@ -226,15 +226,37 @@ async fn power_up_and_wait_for_connection(
226226
}
227227
};
228228

229+
/// we might have a tasks which should be aborted on errors
230+
/// this function is taking care of this before next bluetooth
231+
/// connection attempts
232+
fn abort_tasks(
233+
t1: &mut Option<JoinHandle<Result<()>>>,
234+
t2: &mut Option<JoinHandle<Result<ProfileHandle>>>,
235+
) {
236+
if let Some(task) = t1 {
237+
task.abort();
238+
}
239+
if let Some(task) = t2 {
240+
task.abort();
241+
}
242+
}
243+
229244
let req = timeout(bt_timeout, handle_aa.next())
230-
.await?
245+
.await
246+
.map_err(|e| {
247+
abort_tasks(&mut connect_task, &mut task_hsp);
248+
e
249+
})?
231250
.expect("received no connect request");
232251
info!(
233252
"{} 📱 AA Wireless Profile: connect from: <b>{}</>",
234253
NAME,
235254
req.device()
236255
);
237-
let stream = req.accept()?;
256+
let stream = req.accept().map_err(|e| {
257+
abort_tasks(&mut connect_task, &mut task_hsp);
258+
e
259+
})?;
238260

239261
// we have a connection from phone, stop connect_task
240262
if let Some(task) = connect_task {

0 commit comments

Comments
 (0)