Skip to content

Commit 8fb3a75

Browse files
committed
fix: replace FuturesUnordered from futures with JoinSet from tokio
FuturesUnordered is likely buggy and iroh previously switched to JoinSet in <n0-computer/iroh#1647>. We also have reports with logs of background_fetch getting stuck so apparently task cancellation after timeout does not work as intended with FuturesUnordered.
1 parent 846c8e7 commit 8fb3a75

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/accounts.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ use std::future::Future;
55
use std::path::{Path, PathBuf};
66

77
use anyhow::{bail, ensure, Context as _, Result};
8-
use futures::stream::FuturesUnordered;
9-
use futures::StreamExt;
108
use serde::{Deserialize, Serialize};
119
use tokio::fs;
1210
use tokio::io::AsyncWriteExt;
13-
use tokio::task::JoinHandle;
11+
use tokio::task::{JoinHandle, JoinSet};
1412
use uuid::Uuid;
1513

1614
#[cfg(not(target_os = "ios"))]
@@ -304,24 +302,22 @@ impl Accounts {
304302
/// This is an auxiliary function and not part of public API.
305303
/// Use [Accounts::background_fetch] instead.
306304
async fn background_fetch_no_timeout(accounts: Vec<Context>, events: Events) {
307-
async fn background_fetch_and_log_error(account: Context) {
308-
if let Err(error) = account.background_fetch().await {
309-
warn!(account, "{error:#}");
310-
}
311-
}
312-
313305
events.emit(Event {
314306
id: 0,
315307
typ: EventType::Info(format!(
316308
"Starting background fetch for {} accounts.",
317309
accounts.len()
318310
)),
319311
});
320-
let mut futures_unordered: FuturesUnordered<_> = accounts
321-
.into_iter()
322-
.map(background_fetch_and_log_error)
323-
.collect();
324-
while futures_unordered.next().await.is_some() {}
312+
let mut set = JoinSet::new();
313+
for account in accounts {
314+
set.spawn(async move {
315+
if let Err(error) = account.background_fetch().await {
316+
warn!(account, "{error:#}");
317+
}
318+
});
319+
}
320+
set.join_all().await;
325321
}
326322

327323
/// Auxiliary function for [Accounts::background_fetch].

0 commit comments

Comments
 (0)