From ba0ffb712e6125a44b4b05047fbd0c8a1ebcbf37 Mon Sep 17 00:00:00 2001 From: Aly Raffauf Date: Sun, 2 Aug 2026 22:55:53 -0400 Subject: [PATCH] Bound sync I/O and stabilize local scans --- src/app/manifest.rs | 9 ++++- src/iroh.rs | 88 ++++++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/app/manifest.rs b/src/app/manifest.rs index e9034d2..5a0b3bd 100644 --- a/src/app/manifest.rs +++ b/src/app/manifest.rs @@ -164,9 +164,16 @@ async fn import_files( stream::iter(files) .map(|candidate| async move { let blob_hash = node.import_file(&candidate.file_path).await?.to_string(); - Ok::<_, anyhow::Error>((candidate, blob_hash)) + let current_metadata = fs::metadata(&candidate.file_path)?; + let current_fingerprint = FileFingerprint::from_metadata(¤t_metadata)?; + if current_fingerprint != candidate.fingerprint { + tracing::debug!(path = %candidate.path, "File changed during blob import; retrying on the next scan"); + return Ok(None); + } + Ok::<_, anyhow::Error>(Some((candidate, blob_hash))) }) .buffer_unordered(MAX_CONCURRENT_BLOB_TRANSFERS) + .try_filter_map(|candidate| async move { Ok(candidate) }) .try_collect() .await } diff --git a/src/iroh.rs b/src/iroh.rs index 2550f4e..46ed173 100644 --- a/src/iroh.rs +++ b/src/iroh.rs @@ -21,10 +21,7 @@ use iroh_mdns_address_lookup::{DiscoveryEvent, MdnsAddressLookup}; use tokio::sync::RwLock; use crate::{ - domain::{ - FolderId, FolderRoster, Manifest, ManifestMerkleIndex, ManifestMerkleNode, - manifest_merkle_node, - }, + domain::{FolderId, FolderRoster, Manifest, ManifestMerkleIndex, ManifestMerkleNode}, protocol::{ControlMessage, Invite, ManifestRequestKind, ManifestSummary}, }; @@ -35,6 +32,8 @@ pub const APPA_ALPN: &[u8] = b"appa/sync/3"; const MAX_CONTROL_MESSAGE_BYTES: usize = 16 * 1024 * 1024; const MAX_CONCURRENT_ANNOUNCEMENTS: usize = 4; const ANNOUNCEMENT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); +const CONTROL_OPERATION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); +const BLOB_OPERATION_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5 * 60); #[derive(Clone, Debug)] pub struct FolderSession { @@ -155,7 +154,13 @@ impl NodeHost { } pub async fn import_file(&self, file_path: &Path) -> anyhow::Result { - Ok(self.store.blobs().add_path(file_path).await?.hash) + Ok(tokio::time::timeout( + BLOB_OPERATION_TIMEOUT, + self.store.blobs().add_path(file_path), + ) + .await + .map_err(|_| anyhow::anyhow!("blob import timed out"))?? + .hash) } pub async fn download_blob( @@ -163,15 +168,24 @@ impl NodeHost { blob_hash: Hash, provider: EndpointAddr, ) -> anyhow::Result<()> { - self.store - .downloader(&self.endpoint) - .download(blob_hash, Some(provider.id)) - .await?; + tokio::time::timeout( + BLOB_OPERATION_TIMEOUT, + self.store + .downloader(&self.endpoint) + .download(blob_hash, Some(provider.id)), + ) + .await + .map_err(|_| anyhow::anyhow!("blob download timed out"))??; Ok(()) } pub async fn export_blob(&self, blob_hash: Hash, destination: &Path) -> anyhow::Result<()> { - self.store.blobs().export(blob_hash, destination).await?; + tokio::time::timeout( + BLOB_OPERATION_TIMEOUT, + self.store.blobs().export(blob_hash, destination), + ) + .await + .map_err(|_| anyhow::anyhow!("blob export timed out"))??; Ok(()) } @@ -248,11 +262,7 @@ impl NodeHost { capability: String, invite: Option, ) -> anyhow::Result<(Manifest, FolderRoster, Vec, String)> { - let connection = self - .endpoint - .connect(peer_address.clone(), APPA_ALPN) - .await?; - verify_connected_peer(&connection, &peer_address)?; + let connection = self.connect_to_peer(peer_address).await?; let response = request_control_message( &connection, &ControlMessage::ManifestRequest { @@ -283,11 +293,7 @@ impl NodeHost { capability: String, invite: Option, ) -> anyhow::Result<(ManifestSummary, FolderRoster, Vec, String)> { - let connection = self - .endpoint - .connect(peer_address.clone(), APPA_ALPN) - .await?; - verify_connected_peer(&connection, &peer_address)?; + let connection = self.connect_to_peer(peer_address).await?; let response = request_control_message( &connection, &ControlMessage::ManifestRequest { @@ -319,11 +325,8 @@ impl NodeHost { invite: Option, local_manifest: &Manifest, ) -> anyhow::Result { - let connection = self - .endpoint - .connect(peer_address.clone(), APPA_ALPN) - .await?; - verify_connected_peer(&connection, &peer_address)?; + let connection = self.connect_to_peer(peer_address).await?; + let local_merkle_index = ManifestMerkleIndex::build(local_manifest)?; let mut remote_manifest = local_manifest.clone(); let mut pending_prefixes = vec![String::new()]; while let Some(prefix) = pending_prefixes.pop() { @@ -339,7 +342,9 @@ impl NodeHost { if node.prefix() != prefix { anyhow::bail!("peer returned a Merkle node for the wrong prefix"); } - let local_node = manifest_merkle_node(local_manifest, &prefix)?; + let local_node = local_merkle_index + .node(&prefix)? + .ok_or_else(|| anyhow::anyhow!("local Merkle node is missing"))?; if local_node.hash() == node.hash() { continue; } @@ -349,7 +354,9 @@ impl NodeHost { } => { for (label, remote_hash) in children { let child_prefix = format!("{prefix}{label}"); - let local_child = manifest_merkle_node(local_manifest, &child_prefix)?; + let local_child = local_merkle_index + .node(&child_prefix)? + .ok_or_else(|| anyhow::anyhow!("local Merkle child is missing"))?; if local_child.hash() != remote_hash { pending_prefixes.push(child_prefix); } @@ -397,6 +404,17 @@ impl NodeHost { ) } + async fn connect_to_peer(&self, peer_address: EndpointAddr) -> anyhow::Result { + let connection = tokio::time::timeout( + CONTROL_OPERATION_TIMEOUT, + self.endpoint.connect(peer_address.clone(), APPA_ALPN), + ) + .await + .map_err(|_| anyhow::anyhow!("Appa peer connection timed out"))??; + verify_connected_peer(&connection, &peer_address)?; + Ok(connection) + } + pub fn take_announced_folders(&self) -> Vec { std::mem::take( &mut *self @@ -490,12 +508,16 @@ async fn request_control_message( if payload.len() > MAX_CONTROL_MESSAGE_BYTES { anyhow::bail!("control message exceeds the maximum size"); } - let (mut send, mut receive) = connection.open_bi().await?; - send.write_all(&payload).await?; - send.finish()?; - Ok(serde_json::from_slice( - &receive.read_to_end(MAX_CONTROL_MESSAGE_BYTES).await?, - )?) + tokio::time::timeout(CONTROL_OPERATION_TIMEOUT, async { + let (mut send, mut receive) = connection.open_bi().await?; + send.write_all(&payload).await?; + send.finish()?; + Ok(serde_json::from_slice( + &receive.read_to_end(MAX_CONTROL_MESSAGE_BYTES).await?, + )?) + }) + .await + .map_err(|_| anyhow::anyhow!("Appa control request timed out"))? } fn blob_activity_events() -> EventSender { -- knotmirror 0.0.1