From fc68f7360dab1127ea707fa789a27d191bac50d6 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Wed, 29 Jul 2026 21:14:09 +0200 Subject: [PATCH] fix: avoid retaining desktop updater downloads --- src/app/pages/client/ClientNonUIFeatures.tsx | 2 - src/app/pages/client/DesktopUpdater.tsx | 137 +++++++++++++------ 2 files changed, 92 insertions(+), 47 deletions(-) diff --git a/src/app/pages/client/ClientNonUIFeatures.tsx b/src/app/pages/client/ClientNonUIFeatures.tsx index 897c11a57..969accd5a 100644 --- a/src/app/pages/client/ClientNonUIFeatures.tsx +++ b/src/app/pages/client/ClientNonUIFeatures.tsx @@ -4,7 +4,6 @@ import { ShareTargetFeature } from '$features/share-target/ShareTargetFeature'; import { useIncomingCallSignaling } from '$features/call/useCallSignaling'; import { MatrixRTCSessionProvider } from '$hooks/useMatrixRTCSession'; import { BackgroundNotifications } from './BackgroundNotifications'; -import { DesktopUpdater } from './DesktopUpdater'; import { WebUpdater } from './WebUpdater'; import { NotificationTransportRuntimeFeature } from '$features/settings/notifications/NotificationTransportRuntimeFeature'; import { @@ -55,7 +54,6 @@ export function ClientNonUIFeatures({ children }: ClientNonUIFeaturesProps) { - diff --git a/src/app/pages/client/DesktopUpdater.tsx b/src/app/pages/client/DesktopUpdater.tsx index 0b4406ccd..bfc39272e 100644 --- a/src/app/pages/client/DesktopUpdater.tsx +++ b/src/app/pages/client/DesktopUpdater.tsx @@ -27,11 +27,21 @@ export function DesktopUpdater() { const setLastChecked = useSetAtom(desktopUpdateLastCheckedAtom); const [updateInfo, setUpdateInfo] = useState(null); const [isDownloaded, setIsDownloaded] = useState(false); + const [isDownloading, setIsDownloading] = useState(false); const [isInstalled, setIsInstalled] = useState(false); const [isInstalling, setIsInstalling] = useState(false); const [dismissed, setDismissed] = useState(false); const [useCustomTitleBar] = useDesktopSetting('useCustomTitleBar'); const hasUpdateRef = useRef(false); + const runningRef = useRef(false); + const downloadedRef = useRef(false); + const pendingUpdateRef = useRef(null); + const installStartedRef = useRef(false); + + const closePendingUpdate = useCallback((update: Update | null) => { + if (!update || fakeDesktopUpdate()) return; + void update.close().catch((err) => log.error('Failed to release desktop update', err)); + }, []); useEffect(() => { if (!isDesktopTauri()) return undefined; @@ -39,22 +49,18 @@ export function DesktopUpdater() { let mounted = true; - setPhase({ type: 'checking' }); + if (!downloadedRef.current) { + setPhase({ type: 'checking' }); + } async function run() { + if (runningRef.current || downloadedRef.current || installStartedRef.current) return; + runningRef.current = true; try { if (fakeDesktopUpdate()) { - log.log('Fake update: simulating download'); + log.log('Fake update: simulating available update'); setUpdateInfo({ version: '9.9.9', body: 'Fake changelog for testing.' } as Update); - setPhase({ type: 'downloading', progress: 0 }); - for (let pct = 0; pct <= 100; pct += 2) { - // eslint-disable-next-line no-await-in-loop - await new Promise((r) => setTimeout(r, 40)); - if (!mounted) return; - setPhase({ type: 'downloading', progress: pct }); - } - if (!mounted) return; - setIsDownloaded(true); + setIsDownloaded(false); setPhase({ type: 'ready', version: '9.9.9' }); setLastChecked(new Date().toISOString()); if (!hasCustomDesktopTitlebar(useCustomTitleBar)) setBannerVisible(true); @@ -63,38 +69,30 @@ export function DesktopUpdater() { const { check } = await import('@tauri-apps/plugin-updater'); const update = await check(); - if (!mounted) return; + if (!mounted) { + closePendingUpdate(update); + return; + } if (!update) { - if (!hasUpdateRef.current) { - setPhase({ type: 'idle' }); - } + closePendingUpdate(pendingUpdateRef.current); + pendingUpdateRef.current = null; + setUpdateInfo(null); + setIsDownloaded(false); + hasUpdateRef.current = false; + setPhase({ type: 'idle' }); setLastChecked(new Date().toISOString()); return; } - log.log(`Desktop update ${update.version} available, downloading...`); + closePendingUpdate(pendingUpdateRef.current); + pendingUpdateRef.current = update; + log.log(`Desktop update ${update.version} available`); setUpdateInfo(update); + setIsDownloaded(false); setIsInstalled(false); setDismissed(false); hasUpdateRef.current = true; - setPhase({ type: 'downloading', progress: 0 }); - - let downloadedBytes = 0; - let contentLength = 0; - await update.download((event) => { - if (event.event === 'Started') { - contentLength = event.data.contentLength ?? 0; - } else if (event.event === 'Progress') { - downloadedBytes += event.data.chunkLength; - const pct = contentLength > 0 ? Math.round((downloadedBytes / contentLength) * 100) : 0; - setPhase({ type: 'downloading', progress: Math.min(pct, 100) }); - } - }); - - if (!mounted) return; - log.log(`Update ${update.version} downloaded`); - setIsDownloaded(true); setPhase({ type: 'ready', version: update.version }); setLastChecked(new Date().toISOString()); if (!hasCustomDesktopTitlebar(useCustomTitleBar)) setBannerVisible(true); @@ -106,6 +104,8 @@ export function DesktopUpdater() { } setLastChecked(new Date().toISOString()); } + } finally { + runningRef.current = false; } } @@ -116,11 +116,19 @@ export function DesktopUpdater() { return () => { mounted = false; clearInterval(interval); + if (!installStartedRef.current) { + closePendingUpdate(pendingUpdateRef.current); + pendingUpdateRef.current = null; + } }; } return () => { mounted = false; + if (!installStartedRef.current) { + closePendingUpdate(pendingUpdateRef.current); + pendingUpdateRef.current = null; + } }; }, [ autoUpdateCheck, @@ -129,6 +137,7 @@ export function DesktopUpdater() { setBannerVisible, setLastChecked, useCustomTitleBar, + closePendingUpdate, ]); useEffect(() => { @@ -138,25 +147,52 @@ export function DesktopUpdater() { }, [bannerVisible, dismissed]); const handleInstall = useCallback(async () => { - if (!updateInfo) return; + if (!updateInfo || installStartedRef.current) return; + installStartedRef.current = true; try { - setIsInstalling(true); - setPhase({ type: 'installing' }); + setIsDownloading(!isDownloaded); + if (!isDownloaded) setPhase({ type: 'downloading', progress: 0 }); if (fakeDesktopUpdate()) { - await new Promise((r) => setTimeout(r, 1500)); - } else { - await updateInfo.install(); + for (let pct = 0; pct <= 100; pct += 2) { + // eslint-disable-next-line no-await-in-loop + await new Promise((r) => setTimeout(r, 40)); + setPhase({ type: 'downloading', progress: pct }); + } + } else if (!isDownloaded) { + let downloadedBytes = 0; + let contentLength = 0; + await updateInfo.download((event) => { + if (event.event === 'Started') { + contentLength = event.data.contentLength ?? 0; + } else if (event.event === 'Progress') { + downloadedBytes += event.data.chunkLength; + const pct = contentLength > 0 ? Math.round((downloadedBytes / contentLength) * 100) : 0; + setPhase({ type: 'downloading', progress: Math.min(pct, 100) }); + } + }); } + setIsDownloading(false); + setIsDownloaded(true); + downloadedRef.current = true; + setIsInstalling(true); + setPhase({ type: 'installing' }); + if (fakeDesktopUpdate()) await new Promise((r) => setTimeout(r, 1500)); + else await updateInfo.install(); + setPhase({ type: 'ready', version: updateInfo.version }); setIsInstalling(false); setIsInstalled(true); + pendingUpdateRef.current = null; + closePendingUpdate(updateInfo); } catch (err) { log.error('Failed to install update', err); + setIsDownloading(false); setIsInstalling(false); + installStartedRef.current = false; } - }, [updateInfo, setPhase]); + }, [closePendingUpdate, isDownloaded, updateInfo, setPhase]); const handleRestart = useCallback(async () => { try { @@ -170,7 +206,13 @@ export function DesktopUpdater() { const handleDismiss = useCallback(() => { setDismissed(true); setBannerVisible(false); - }, [setBannerVisible]); + if (!installStartedRef.current) { + closePendingUpdate(pendingUpdateRef.current); + pendingUpdateRef.current = null; + setUpdateInfo(null); + setIsDownloaded(false); + } + }, [closePendingUpdate, setBannerVisible]); const bannerData = useMemo(() => { if (!bannerVisible || !updateInfo || dismissed) return null; @@ -195,16 +237,20 @@ export function DesktopUpdater() { }; } - if (!isDownloaded) return null; - return { id: 'desktop-update-ready', priority: 200, icon: ArrowUp, title: 'Update Available', - description: `Sable ${updateInfo.version} is ready to install.${updateInfo.body ? `\n${updateInfo.body}` : ''}`, + description: `Sable ${updateInfo.version} is available.${updateInfo.body ? `\n${updateInfo.body}` : ''}`, primaryAction: { - label: isInstalling ? 'Installing...' : 'Install & Update', + label: isDownloading + ? 'Downloading...' + : isInstalling + ? 'Installing...' + : isDownloaded + ? 'Install & Update' + : 'Download & Install', variant: 'Primary', onClick: handleInstall, }, @@ -219,6 +265,7 @@ export function DesktopUpdater() { updateInfo, dismissed, isDownloaded, + isDownloading, isInstalled, isInstalling, handleInstall,