From 051dbcb9378dfb6599ab4bf010941f7dda33cba1 Mon Sep 17 00:00:00 2001 From: Louis Date: Thu, 4 Jan 2024 11:22:11 +0700 Subject: [PATCH] fix: app getting stuck at downloading 99 percent while downloading model (#1320) Addressed a critical issue where the application would become unresponsive at the 99 percent mark during model download --- electron/handlers/download.ts | 24 ++++++++++++++++------ web/containers/Providers/EventListener.tsx | 5 +++-- web/hooks/useDownloadState.ts | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/electron/handlers/download.ts b/electron/handlers/download.ts index 621d85043..e8867b055 100644 --- a/electron/handlers/download.ts +++ b/electron/handlers/download.ts @@ -34,8 +34,18 @@ export function handleDownloaderIPCs() { */ ipcMain.handle(DownloadRoute.abortDownload, async (_event, fileName) => { const rq = DownloadManager.instance.networkRequests[fileName] - DownloadManager.instance.networkRequests[fileName] = undefined - rq?.abort() + if (rq) { + DownloadManager.instance.networkRequests[fileName] = undefined + rq?.abort() + } else { + WindowManager?.instance.currentWindow?.webContents.send( + DownloadEvent.onFileDownloadError, + { + fileName, + err: { message: 'aborted' }, + } + ) + } }) /** @@ -54,7 +64,11 @@ export function handleDownloaderIPCs() { } const destination = resolve(userDataPath, fileName) const rq = request(url) - // downloading file to a temp file first + + // Put request to download manager instance + DownloadManager.instance.setRequest(fileName, rq) + + // Downloading file to a temp file first const downloadingTempFile = `${destination}.download` progress(rq, {}) @@ -93,13 +107,11 @@ export function handleDownloaderIPCs() { DownloadEvent.onFileDownloadError, { fileName, - err: 'Download cancelled', + err: { message: 'aborted' }, } ) } }) .pipe(createWriteStream(downloadingTempFile)) - - DownloadManager.instance.setRequest(fileName, rq) }) } diff --git a/web/containers/Providers/EventListener.tsx b/web/containers/Providers/EventListener.tsx index ff661aacc..b82efb066 100644 --- a/web/containers/Providers/EventListener.tsx +++ b/web/containers/Providers/EventListener.tsx @@ -52,7 +52,8 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) { window.electronAPI.onFileDownloadError( async (_event: string, state: any) => { - console.error('Download error', state) + if (state.err?.message !== 'aborted') + console.error('Download error', state) const modelName = await baseName(state.fileName) const model = modelsRef.current.find( (model) => modelBinFileName(model) === modelName @@ -66,7 +67,7 @@ export default function EventListenerWrapper({ children }: PropsWithChildren) { if (state && state.fileName) { const modelName = await baseName(state.fileName) const model = modelsRef.current.find( - async (model) => modelBinFileName(model) === modelName + (model) => modelBinFileName(model) === modelName ) if (model) { setDownloadStateSuccess(model.id) diff --git a/web/hooks/useDownloadState.ts b/web/hooks/useDownloadState.ts index e1dd7163f..0d7967ac5 100644 --- a/web/hooks/useDownloadState.ts +++ b/web/hooks/useDownloadState.ts @@ -33,7 +33,7 @@ const setDownloadStateFailedAtom = atom(null, (get, set, modelId: string) => { const currentState = { ...get(modelDownloadStateAtom) } const state = currentState[modelId] if (!state) { - console.error(`Cannot find download state for ${modelId}`) + console.debug(`Cannot find download state for ${modelId}`) toaster({ title: 'Cancel Download', description: `Model ${modelId} cancel download`,