Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): stage Whisper model downloads

Aditya Garude0325b44314d
diff --git a/apps/desktop-gpui/src/transcription.rs b/apps/desktop-gpui/src/transcription.rs
index 92fa942a9..0f7351a20 100644
--- a/apps/desktop-gpui/src/transcription.rs
+++ b/apps/desktop-gpui/src/transcription.rs
@@ -412,9 +412,9 @@ async fn download_whisper_model(model: &str) -> Result<(), String> {
let result = write_whisper_parts(model, model_parts, &path, total_size).await;
if result.is_err() {
- // Unlike the Tauri version, drop the half-written file: it lives at
- // the final path, and a later existence check would call it a model.
- let _ = std::fs::remove_file(&path);
+ // An interrupted process can leave this file behind without making
+ // an incomplete download look installed on the next disk scan.
+ let _ = std::fs::remove_file(staging_path(&path));
}
result
}
@@ -425,7 +425,8 @@ async fn write_whisper_parts(
path: &Path,
total_size: u64,
) -> Result<(), String> {
- let mut file = File::create(path).map_err(|e| format!("Failed to create file: {e}"))?;
+ let staging = staging_path(path);
+ let mut file = File::create(&staging).map_err(|e| format!("Failed to create file: {e}"))?;
let mut downloaded: u64 = 0;
let part_count = model_parts.len() as f64;
@@ -474,7 +475,15 @@ async fn write_whisper_parts(
}
file.flush()
- .map_err(|e| format!("Failed to flush file: {e}"))
+ .map_err(|e| format!("Failed to flush file: {e}"))?;
+ drop(file);
+ std::fs::rename(&staging, path).map_err(|e| format!("Failed to install model: {e}"))
+}
+
+/// Downloads land here and are renamed into place once complete, so a
+/// half-written file never sits at the path that marks a model installed.
+fn staging_path(path: &Path) -> PathBuf {
+ path.with_extension("bin.downloading")
}
/// `total_content_length` (`captions.rs:2198-2218`).
@@ -1768,8 +1777,8 @@ pub fn transcribe_blocking(
language: &str,
) -> Result<Vec<CaptionSegment>, String> {
let requested = model;
- // A download writes straight to the model's final path, so a model still
- // downloading looks present but cannot load yet.
+ // A model being downloaded should not be selected as a fallback, even
+ // if an older copy still exists at its final path.
let model = transcription_model(model, language, |model| {
model_files_present(model) && !hub().downloading(model)
})?;
@@ -2141,6 +2150,8 @@ pub fn caption_generation_error_message(message: &str) -> String {
message.to_string()
}
+#[cfg(test)]
+mod download_tests;
#[cfg(test)]
mod verify_tests;
diff --git a/apps/desktop-gpui/src/transcription/download_tests.rs b/apps/desktop-gpui/src/transcription/download_tests.rs
new file mode 100644
index 000000000..10ac65277
--- /dev/null
+++ b/apps/desktop-gpui/src/transcription/download_tests.rs
@@ -0,0 +1,112 @@
+use super::*;
+use tokio::{
+ io::{AsyncReadExt, AsyncWriteExt},
+ net::TcpListener,
+ sync::oneshot,
+};
+
+async fn multipart_server() -> (
+ [String; 2],
+ oneshot::Receiver<()>,
+ oneshot::Sender<()>,
+ tokio::task::JoinHandle<()>,
+) {
+ let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let address = listener.local_addr().unwrap();
+ let (waiting_tx, waiting_rx) = oneshot::channel();
+ let (finish_tx, finish_rx) = oneshot::channel();
+ let server = tokio::spawn(async move {
+ let (mut first, _) = listener.accept().await.unwrap();
+ let mut request = [0; 1024];
+ assert!(first.read(&mut request).await.unwrap() > 0);
+ first
+ .write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 5\r\nConnection: close\r\n\r\nfirst")
+ .await
+ .unwrap();
+ drop(first);
+
+ let (mut second, _) = listener.accept().await.unwrap();
+ assert!(second.read(&mut request).await.unwrap() > 0);
+ waiting_tx.send(()).unwrap();
+ if finish_rx.await.is_ok() {
+ second
+ .write_all(
+ b"HTTP/1.1 200 OK\r\nContent-Length: 6\r\nConnection: close\r\n\r\nsecond",
+ )
+ .await
+ .unwrap();
+ }
+ });
+ (
+ [
+ format!("http://{address}/part0"),
+ format!("http://{address}/part1"),
+ ],
+ waiting_rx,
+ finish_tx,
+ server,
+ )
+}
+
+#[tokio::test]
+async fn interrupted_whisper_download_is_not_an_installed_model() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("medium.bin");
+ let (urls, waiting, finish, server) = multipart_server().await;
+ let parts = [urls[0].as_str(), urls[1].as_str()];
+ let mut download = Box::pin(write_whisper_parts("medium", &parts, &path, 11));
+
+ tokio::select! {
+ result = &mut download => panic!("Download ended before the second part: {result:?}"),
+ result = tokio::time::timeout(Duration::from_secs(5), waiting) => result.unwrap().unwrap(),
+ }
+
+ drop(download);
+ drop(finish);
+ server.await.unwrap();
+ assert!(
+ !path.exists(),
+ "An interrupted download must not look installed"
+ );
+
+ assert_eq!(std::fs::read(staging_path(&path)).unwrap(), b"first");
+
+ let (urls, waiting, finish, server) = multipart_server().await;
+ let parts = [urls[0].as_str(), urls[1].as_str()];
+ let retry = write_whisper_parts("medium", &parts, &path, 11);
+ let complete = async move {
+ waiting.await.unwrap();
+ finish.send(()).unwrap();
+ server.await.unwrap();
+ };
+ let (result, ()) = tokio::time::timeout(Duration::from_secs(5), async {
+ tokio::join!(retry, complete)
+ })
+ .await
+ .unwrap();
+ result.unwrap();
+ assert_eq!(std::fs::read(&path).unwrap(), b"firstsecond");
+ assert_eq!(std::fs::read_dir(dir.path()).unwrap().count(), 1);
+}
+
+#[tokio::test]
+async fn failed_whisper_download_does_not_replace_an_installed_model() {
+ let dir = tempfile::tempdir().unwrap();
+ let path = dir.path().join("medium.bin");
+ std::fs::write(&path, b"installed").unwrap();
+ let (urls, waiting, finish, server) = multipart_server().await;
+ let parts = [urls[0].as_str(), urls[1].as_str()];
+ let download = write_whisper_parts("medium", &parts, &path, 11);
+ let fail = async move {
+ waiting.await.unwrap();
+ drop(finish);
+ server.await.unwrap();
+ };
+ let (result, ()) = tokio::time::timeout(Duration::from_secs(5), async {
+ tokio::join!(download, fail)
+ })
+ .await
+ .unwrap();
+ assert!(result.is_err());
+ assert_eq!(std::fs::read(&path).unwrap(), b"installed");
+}