From 34c80809f1c8206cd16e6bdd4beb58a4006bfd99 Mon Sep 17 00:00:00 2001 From: "A.Shakhmatov" Date: Sat, 21 Feb 2026 17:45:58 +0300 Subject: [PATCH] fix: prevent snapshot hang caused by u8 overflow in progress calculation When creating/restoring snapshots with 4+ tables, the progress percentage calculation overflowed u8 (e.g. 4*80=320 > 255), causing a panic that left the IPC call unresolved. Also deduplicate fetch_foreign_keys_raw call. Co-Authored-By: Claude Opus 4.6 --- src-tauri/src/commands/snapshot.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src-tauri/src/commands/snapshot.rs b/src-tauri/src/commands/snapshot.rs index 8f93eaf..8ac093e 100644 --- a/src-tauri/src/commands/snapshot.rs +++ b/src-tauri/src/commands/snapshot.rs @@ -41,8 +41,10 @@ pub async fn create_snapshot( .map(|t| (t.schema.clone(), t.table.clone())) .collect(); + // Fetch FK info once — used for both dependency expansion and topological sort + let fk_rows = fetch_foreign_keys_raw(&pool).await?; + if params.include_dependencies { - let fk_rows = fetch_foreign_keys_raw(&pool).await?; for fk in &fk_rows { for (schema, table) in ¶ms.tables.iter().map(|t| (t.schema.clone(), t.table.clone())).collect::>() { if &fk.schema == schema && &fk.table == table { @@ -56,7 +58,6 @@ pub async fn create_snapshot( } // FK-based topological sort - let fk_rows = fetch_foreign_keys_raw(&pool).await?; let fk_edges: Vec<(String, String, String, String)> = fk_rows .iter() .map(|fk| (fk.schema.clone(), fk.table.clone(), fk.ref_schema.clone(), fk.ref_table.clone())) @@ -75,7 +76,7 @@ pub async fn create_snapshot( let mut total_rows: u64 = 0; for (i, (schema, table)) in sorted_tables.iter().enumerate() { - let percent = 10 + ((i as u8) * 80 / total_tables.max(1) as u8); + let percent = (10 + (i * 80 / total_tables.max(1))).min(90) as u8; let _ = app.emit( "snapshot-progress", SnapshotProgress { @@ -249,7 +250,7 @@ pub async fn restore_snapshot( continue; } - let percent = 20 + ((i as u8) * 75 / total_tables.max(1) as u8); + let percent = (20 + (i * 75 / total_tables.max(1))).min(95) as u8; let _ = app.emit( "snapshot-progress", SnapshotProgress {