Skip to content

Commit

Permalink
fix: app getting stuck at downloading 99 percent while downloading mo…
Browse files Browse the repository at this point in the history
…del (janhq#1320)

Addressed a critical issue where the application would become unresponsive at the 99 percent mark during model download
  • Loading branch information
louis-jan authored Jan 4, 2024
1 parent 7c784ea commit 051dbcb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
24 changes: 18 additions & 6 deletions electron/handlers/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
}
)
}
})

/**
Expand All @@ -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, {})
Expand Down Expand Up @@ -93,13 +107,11 @@ export function handleDownloaderIPCs() {
DownloadEvent.onFileDownloadError,
{
fileName,
err: 'Download cancelled',
err: { message: 'aborted' },
}
)
}
})
.pipe(createWriteStream(downloadingTempFile))

DownloadManager.instance.setRequest(fileName, rq)
})
}
5 changes: 3 additions & 2 deletions web/containers/Providers/EventListener.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion web/hooks/useDownloadState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down

0 comments on commit 051dbcb

Please sign in to comment.