Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix(desktop): emit restart only after install (#6715)

Shreyas384cb0c84219
diff --git a/packages/hoppscotch-desktop/src-tauri/src/updater.rs b/packages/hoppscotch-desktop/src-tauri/src/updater.rs
index 2a57f58b3..aac7f9194 100644
--- a/packages/hoppscotch-desktop/src-tauri/src/updater.rs
+++ b/packages/hoppscotch-desktop/src-tauri/src/updater.rs
@@ -1,12 +1,13 @@
use crate::{dialog, util};
use native_dialog::MessageType;
use serde::{Deserialize, Serialize};
-use std::sync::Arc;
+use std::future::Future;
+use std::sync::{Arc, PoisonError};
use tauri::{AppHandle, Emitter};
use tauri_plugin_updater::{Update, UpdaterExt};
use tokio::sync::Mutex;
-#[derive(Debug, Clone, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateInfo {
pub available: bool,
@@ -15,7 +16,7 @@ pub struct UpdateInfo {
pub release_notes: Option<String>,
}
-#[derive(Debug, Clone, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DownloadProgress {
pub downloaded: u64,
@@ -23,7 +24,7 @@ pub struct DownloadProgress {
pub percentage: f64,
}
-#[derive(Debug, Clone, Serialize, Deserialize)]
+#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum UpdateEvent {
CheckStarted,
@@ -55,9 +56,11 @@ pub enum UpdateEvent {
static UPDATE_STATE: std::sync::LazyLock<Arc<Mutex<Option<Update>>>> =
std::sync::LazyLock::new(|| Arc::new(Mutex::new(None)));
-static DOWNLOAD_STATE: std::sync::LazyLock<Arc<Mutex<DownloadProgress>>> =
+// The updater's chunk callback is synchronous and never holds this lock
+// across an await, so a std mutex fits.
+static DOWNLOAD_STATE: std::sync::LazyLock<Arc<std::sync::Mutex<DownloadProgress>>> =
std::sync::LazyLock::new(|| {
- Arc::new(Mutex::new(DownloadProgress {
+ Arc::new(std::sync::Mutex::new(DownloadProgress {
downloaded: 0,
total: None,
percentage: 0.0,
@@ -170,7 +173,9 @@ pub async fn download_and_install_update(app: AppHandle) -> Result<(), String> {
);
{
- let mut download_state = DOWNLOAD_STATE.lock().await;
+ let mut download_state = DOWNLOAD_STATE
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner);
download_state.downloaded = 0;
download_state.total = None;
download_state.percentage = 0.0;
@@ -178,63 +183,112 @@ pub async fn download_and_install_update(app: AppHandle) -> Result<(), String> {
let app_progress = app.clone();
let app_callback = app.clone();
+ let mut last_emitted_percent: Option<u64> = None;
+
+ let install = update.download_and_install(
+ move |chunk_length, content_length| {
+ // Updating and emitting inline keeps events in chunk order.
+ let events = {
+ let mut download_state = DOWNLOAD_STATE
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner);
+ apply_chunk(
+ &mut download_state,
+ &mut last_emitted_percent,
+ chunk_length,
+ content_length,
+ )
+ };
+ for event in events {
+ let _ = app_progress.emit("updater-event", event);
+ }
+ },
+ move || {
+ // Installation starts after this callback returns, so
+ // `RestartRequired` is emitted by `finish_install` once
+ // `download_and_install` returns instead.
+ let _ = app_callback.emit("updater-event", UpdateEvent::DownloadCompleted);
+ let _ = app_callback.emit("updater-event", UpdateEvent::InstallStarted);
+ },
+ );
- update
- .download_and_install(
- move |chunk_length, content_length| {
- let app = app_progress.clone();
- tauri::async_runtime::spawn(async move {
- let mut download_state = DOWNLOAD_STATE.lock().await;
-
- if let Some(total) = content_length {
- if download_state.total.is_none() {
- download_state.total = Some(total);
- let _ = app.emit(
- "updater-event",
- UpdateEvent::DownloadStarted {
- total_bytes: Some(total),
- },
- );
- }
- }
-
- download_state.downloaded += chunk_length as u64;
-
- if let Some(total) = download_state.total {
- download_state.percentage =
- (download_state.downloaded as f64 / total as f64) * 100.0;
- }
-
- let progress = download_state.clone();
- // Release the lock before emitting
- // TODO: See if it's possible to not have a lock at all
- drop(download_state);
-
- let _ = app.emit("updater-event", UpdateEvent::DownloadProgress { progress });
- });
- },
- move || {
- let app = app_callback.clone();
- tauri::async_runtime::spawn(async move {
- let _ = app.emit("updater-event", UpdateEvent::DownloadCompleted);
- let _ = app.emit("updater-event", UpdateEvent::InstallStarted);
- let _ = app.emit("updater-event", UpdateEvent::RestartRequired);
- });
- },
- )
- .await
- .map_err(|e| {
- let error_msg = format!("Failed to download and install update: {}", e);
- let _ = app.emit(
- "updater-event",
- UpdateEvent::Error {
- message: error_msg.clone(),
- },
- );
- error_msg
- })?;
+ finish_install(install, |event| {
+ let _ = app.emit("updater-event", event);
+ })
+ .await
+}
+
+/// Applies one downloaded chunk to `state` and returns the events it calls
+/// for, in order.
+///
+/// `DownloadStarted` reports the total the first time the server sends one.
+/// Progress is emitted once per whole percent, which caps a download at about
+/// a hundred progress events instead of one per network chunk. Without a
+/// total there is no percent to compare, so every chunk is emitted.
+fn apply_chunk(
+ state: &mut DownloadProgress,
+ last_emitted_percent: &mut Option<u64>,
+ chunk_length: usize,
+ content_length: Option<u64>,
+) -> Vec<UpdateEvent> {
+ let mut events = Vec::new();
+
+ if let Some(total) = content_length {
+ if state.total.is_none() {
+ state.total = Some(total);
+ events.push(UpdateEvent::DownloadStarted {
+ total_bytes: Some(total),
+ });
+ }
+ }
+
+ state.downloaded += chunk_length as u64;
+
+ // A total of 0 would divide to NaN or infinity, and the frontend reads
+ // `percentage` from this state, so it stays at 0, as with no total.
+ if let Some(total) = state.total.filter(|&total| total > 0) {
+ state.percentage = (state.downloaded as f64 * 100.0) / total as f64;
+ }
+
+ // Integer division, since a float divided before it is multiplied lands
+ // just under some whole percents, 29 of 100 bytes as 28.999..., and the
+ // step to 29% was skipped. A total of 0 has no percent, so every chunk is
+ // emitted.
+ let percent = state.total.and_then(|total| {
+ (state.downloaded as u128 * 100)
+ .checked_div(total as u128)
+ .map(|p| p as u64)
+ });
+ if percent.is_none() || percent != *last_emitted_percent {
+ *last_emitted_percent = percent;
+ events.push(UpdateEvent::DownloadProgress {
+ progress: state.clone(),
+ });
+ }
+
+ events
+}
+
+/// Awaits the download and install, then emits what follows it.
+///
+/// `RestartRequired` is emitted only after `install` resolves `Ok`, since the
+/// plugin replaces the app bundle after the download-finish callback returns,
+/// and a restart before that leaves a partly extracted bundle in place.
+async fn finish_install<E: std::fmt::Display>(
+ install: impl Future<Output = Result<(), E>>,
+ emit: impl Fn(UpdateEvent),
+) -> Result<(), String> {
+ if let Err(e) = install.await {
+ let error_msg = format!("Failed to download and install update: {}", e);
+ emit(UpdateEvent::Error {
+ message: error_msg.clone(),
+ });
+ return Err(error_msg);
+ }
tracing::info!("Update download and installation completed successfully");
+ emit(UpdateEvent::InstallCompleted);
+ emit(UpdateEvent::RestartRequired);
Ok(())
}
@@ -260,7 +314,9 @@ pub async fn cancel_update(app: AppHandle) -> Result<(), String> {
}
{
- let mut download_state = DOWNLOAD_STATE.lock().await;
+ let mut download_state = DOWNLOAD_STATE
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner);
download_state.downloaded = 0;
download_state.total = None;
download_state.percentage = 0.0;
@@ -273,7 +329,9 @@ pub async fn cancel_update(app: AppHandle) -> Result<(), String> {
/// Get current download progress (for polling if needed)
#[tauri::command]
pub async fn get_download_progress() -> Result<DownloadProgress, String> {
- let download_state = DOWNLOAD_STATE.lock().await;
+ let download_state = DOWNLOAD_STATE
+ .lock()
+ .unwrap_or_else(PoisonError::into_inner);
Ok(download_state.clone())
}
@@ -306,3 +364,221 @@ async fn handle_portable_update_dialog(
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::cell::RefCell;
+
+ fn fresh() -> (DownloadProgress, Option<u64>) {
+ (
+ DownloadProgress {
+ downloaded: 0,
+ total: None,
+ percentage: 0.0,
+ },
+ None,
+ )
+ }
+
+ fn progress_events(events: &[UpdateEvent]) -> Vec<DownloadProgress> {
+ events
+ .iter()
+ .filter_map(|e| match e {
+ UpdateEvent::DownloadProgress { progress } => Some(progress.clone()),
+ _ => None,
+ })
+ .collect()
+ }
+
+ #[test]
+ fn download_started_carries_the_total_once() {
+ let (mut state, mut last) = fresh();
+ let first = apply_chunk(&mut state, &mut last, 10, Some(1000));
+ let second = apply_chunk(&mut state, &mut last, 10, Some(1000));
+
+ assert_eq!(
+ first[0],
+ UpdateEvent::DownloadStarted {
+ total_bytes: Some(1000)
+ }
+ );
+ assert!(!second
+ .iter()
+ .any(|e| matches!(e, UpdateEvent::DownloadStarted { .. })));
+ }
+
+ #[test]
+ fn progress_is_emitted_once_per_whole_percent() {
+ let (mut state, mut last) = fresh();
+ let mut all = Vec::new();
+ // 10,000 bytes in 1-byte chunks is 100 chunks per percent
+ for _ in 0..10_000 {
+ all.extend(apply_chunk(&mut state, &mut last, 1, Some(10_000)));
+ }
+ let progress = progress_events(&all);
+
+ // 0% on the first chunk, then 1% to 100%
+ assert_eq!(progress.len(), 101);
+ let percents: Vec<u64> = progress
+ .iter()
+ .map(|p| p.percentage.floor() as u64)
+ .collect();
+ assert_eq!(percents, (0..=100).collect::<Vec<_>>());
+ }
+
+ #[test]
+ fn no_percent_is_skipped_when_the_float_lands_under_it() {
+ let (mut state, mut last) = fresh();
+ let mut all = Vec::new();
+ // 29, 57 and 58 of 100 bytes, divided before multiplying, each land
+ // just under the percent
+ for _ in 0..100 {
+ all.extend(apply_chunk(&mut state, &mut last, 1, Some(100)));
+ }
+ let downloaded: Vec<u64> = progress_events(&all).iter().map(|p| p.downloaded).collect();
+
+ assert_eq!(downloaded, (1..=100).collect::<Vec<_>>());
+ }
+
+ #[test]
+ fn progress_is_in_chunk_order_and_ends_at_the_total() {
+ let (mut state, mut last) = fresh();
+ let mut all = Vec::new();
+ for chunk in [300, 300, 300, 100] {
+ all.extend(apply_chunk(&mut state, &mut last, chunk, Some(1000)));
+ }
+ let progress = progress_events(&all);
+
+ let downloaded: Vec<u64> = progress.iter().map(|p| p.downloaded).collect();
+ assert_eq!(downloaded, vec![300, 600, 900, 1000]);
+ assert_eq!(progress.last().unwrap().percentage, 100.0);
+ assert_eq!(state.downloaded, 1000);
+ }
+
+ #[test]
+ fn a_large_chunk_spanning_several_percents_emits_once() {
+ let (mut state, mut last) = fresh();
+ let events = apply_chunk(&mut state, &mut last, 500, Some(1000));
+ let progress = progress_events(&events);
+
+ assert_eq!(progress.len(), 1);
+ assert_eq!(progress[0].percentage, 50.0);
+ }
+
+ #[test]
+ fn without_a_total_every_chunk_is_emitted_and_percentage_stays_zero() {
+ let (mut state, mut last) = fresh();
+ let mut all = Vec::new();
+ for _ in 0..5 {
+ all.extend(apply_chunk(&mut state, &mut last, 100, None));
+ }
+ let progress = progress_events(&all);
+
+ assert!(!all
+ .iter()
+ .any(|e| matches!(e, UpdateEvent::DownloadStarted { .. })));
+ assert_eq!(progress.len(), 5);
+ assert!(progress.iter().all(|p| p.total.is_none()));
+ assert!(progress.iter().all(|p| p.percentage == 0.0));
+ assert_eq!(progress.last().unwrap().downloaded, 500);
+ }
+
+ #[test]
+ fn a_total_of_zero_keeps_percentage_at_zero_and_emits_every_chunk() {
+ let (mut state, mut last) = fresh();
+ let mut all = Vec::new();
+ for _ in 0..3 {
+ all.extend(apply_chunk(&mut state, &mut last, 100, Some(0)));
+ }
+ let progress = progress_events(&all);
+
+ assert_eq!(progress.len(), 3);
+ assert!(progress.iter().all(|p| p.percentage == 0.0));
+ }
+
+ #[test]
+ fn a_total_reported_after_the_first_chunk_is_picked_up() {
+ let (mut state, mut last) = fresh();
+ let mut all = apply_chunk(&mut state, &mut last, 100, None);
+ all.extend(apply_chunk(&mut state, &mut last, 100, Some(1000)));
+
+ assert!(all.contains(&UpdateEvent::DownloadStarted {
+ total_bytes: Some(1000)
+ }));
+ assert_eq!(state.total, Some(1000));
+ assert_eq!(state.percentage, 20.0);
+ }
+
+ #[test]
+ fn restart_is_emitted_only_after_the_install_resolves() {
+ // One log for both the emitted events and the install finishing, so
+ // the test sees their relative order.
+ let log: RefCell<Vec<String>> = RefCell::new(Vec::new());
+
+ let install = async {
+ log.borrow_mut().push("installed".into());
+ Ok::<(), String>(())
+ };
+ let result = tauri::async_runtime::block_on(finish_install(install, |event| {
+ log.borrow_mut().push(format!("{:?}", event));
+ }));
+
+ assert!(result.is_ok());
+ assert_eq!(
+ *log.borrow(),
+ vec!["installed", "InstallCompleted", "RestartRequired"]
+ );
+ }
+
+ #[test]
+ fn a_failed_install_emits_an_error_and_no_restart() {
+ let events: RefCell<Vec<UpdateEvent>> = RefCell::new(Vec::new());
+
+ let install = async { Err::<(), _>("signature mismatch") };
+ let result = tauri::async_runtime::block_on(finish_install(install, |event| {
+ events.borrow_mut().push(event);
+ }));
+
+ let message = "Failed to download and install update: signature mismatch";
+ assert_eq!(result, Err(message.to_string()));
+ assert_eq!(
+ *events.borrow(),
+ vec![UpdateEvent::Error {
+ message: message.into()
+ }]
+ );
+ }
+
+ #[test]
+ fn events_serialize_in_the_shape_the_frontend_reads() {
+ // `updater.client.ts` switches on `type` and reads `totalBytes`
+ let started = serde_json::to_value(UpdateEvent::DownloadStarted {
+ total_bytes: Some(42),
+ })
+ .unwrap();
+ assert_eq!(
+ started,
+ serde_json::json!({ "type": "DownloadStarted", "totalBytes": 42 })
+ );
+
+ let restart = serde_json::to_value(UpdateEvent::RestartRequired).unwrap();
+ assert_eq!(restart, serde_json::json!({ "type": "RestartRequired" }));
+
+ let progress = serde_json::to_value(UpdateEvent::DownloadProgress {
+ progress: DownloadProgress {
+ downloaded: 5,
+ total: Some(10),
+ percentage: 50.0,
+ },
+ })
+ .unwrap();
+ assert_eq!(
+ progress,
+ serde_json::json!({
+ "type": "DownloadProgress",
+ "progress": { "downloaded": 5, "total": 10, "percentage": 50.0 }
+ })
+ );
+ }
+}