Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(ui): stop the app between update download and install

thinhlpgfedad1d082ed
diff --git a/web-app/src/hooks/__tests__/useAppUpdater.test.ts b/web-app/src/hooks/__tests__/useAppUpdater.test.ts
index 547d419aa..b013e7dbf 100644
--- a/web-app/src/hooks/__tests__/useAppUpdater.test.ts
+++ b/web-app/src/hooks/__tests__/useAppUpdater.test.ts
@@ -296,8 +296,10 @@ describe('useAppUpdater', () => {
await result.current.downloadAndInstallUpdate()
})
- expect(mockStopAllModels).toHaveBeenCalled()
- expect(mockEventsEmit).toHaveBeenCalledWith('KILL_SIDECAR')
+ // Stopping the engine now happens in the updater service after the
+ // download has verified, so the hook itself must not touch it.
+ expect(mockStopAllModels).not.toHaveBeenCalled()
+ expect(mockEventsEmit).not.toHaveBeenCalled()
expect(mockUpdaterDownloadAndInstallWithProgress).toHaveBeenCalled()
expect(mockRelaunch).toHaveBeenCalled()
})
@@ -345,7 +347,7 @@ describe('useAppUpdater', () => {
await result.current.downloadAndInstallUpdate()
})
- expect(mockStopAllModels).not.toHaveBeenCalled()
+ expect(mockUpdaterDownloadAndInstallWithProgress).not.toHaveBeenCalled()
})
it('should emit progress events during download', async () => {
diff --git a/web-app/src/hooks/useAppUpdater.ts b/web-app/src/hooks/useAppUpdater.ts
index 5ec820e5c..53cd6d72d 100644
--- a/web-app/src/hooks/useAppUpdater.ts
+++ b/web-app/src/hooks/useAppUpdater.ts
@@ -2,7 +2,6 @@ import { isDev } from '@/lib/utils'
import { useState, useCallback, useEffect } from 'react'
import { events, AppEvent } from '@janhq/core'
import type { UpdateInfo } from '@/services/updater/types'
-import { SystemEvent } from '@/types/events'
import { getServiceHub } from '@/hooks/useServiceHub'
export interface UpdateState {
@@ -169,9 +168,9 @@ export const useAppUpdater = () => {
let downloaded = 0
let contentLength = 0
- await getServiceHub().models().stopAllModels()
- getServiceHub().events().emit(SystemEvent.KILL_SIDECAR)
- await new Promise((resolve) => setTimeout(resolve, 1000))
+ // The engine, MCP servers and agent shells are stopped by the updater
+ // service once the download has verified, not here: a failed download
+ // must not take them down.
await getServiceHub().updater().downloadAndInstallWithProgress((event) => {
switch (event.event) {
diff --git a/web-app/src/services/updater/__tests__/tauri.test.ts b/web-app/src/services/updater/__tests__/tauri.test.ts
index 34e8b97f2..5299b344d 100644
--- a/web-app/src/services/updater/__tests__/tauri.test.ts
+++ b/web-app/src/services/updater/__tests__/tauri.test.ts
@@ -24,7 +24,7 @@ vi.mock('@tauri-apps/api/app', () => ({
getVersion: vi.fn().mockResolvedValue('1.0.0'),
}))
-import { check } from '@tauri-apps/plugin-updater'
+import { check, type Update } from '@tauri-apps/plugin-updater'
import { invoke } from '@tauri-apps/api/core'
import { TauriUpdaterService } from '../tauri'
import { DefaultUpdaterService } from '../default'
@@ -197,28 +197,37 @@ describe('TauriUpdaterService', () => {
})
describe('downloadAndInstallWithProgress()', () => {
- it('calls downloadAndInstall with progress callback', async () => {
- const mockDownloadAndInstall = vi.fn().mockImplementation(async (cb) => {
+ it('downloads, stops the app, then installs, in that order', async () => {
+ const calls: string[] = []
+ const download = vi.fn().mockImplementation(async (cb) => {
+ calls.push('download')
cb({ event: 'Started', data: { contentLength: 1000 } })
cb({ event: 'Progress', data: { chunkLength: 500 } })
cb({ event: 'Finished' })
})
+ const install = vi.fn().mockImplementation(async () => {
+ calls.push('install')
+ })
+ vi.mocked(invoke).mockImplementation(async (cmd: string) => {
+ calls.push(cmd)
+ })
vi.mocked(check).mockResolvedValueOnce({
version: '2.0.0',
- downloadAndInstall: mockDownloadAndInstall,
- } as any)
+ download,
+ install,
+ } as unknown as Update)
const progressCb = vi.fn()
await svc.downloadAndInstallWithProgress(progressCb)
- expect(mockDownloadAndInstall).toHaveBeenCalled()
+ expect(calls).toEqual(['download', 'shutdown_for_update', 'install'])
expect(progressCb).toHaveBeenCalledTimes(3)
expect(progressCb).toHaveBeenCalledWith({ event: 'Started', data: { contentLength: 1000 } })
expect(progressCb).toHaveBeenCalledWith({ event: 'Finished' })
})
it('throws when no update is available', async () => {
- vi.mocked(check).mockResolvedValueOnce(null as any)
+ vi.mocked(check).mockResolvedValueOnce(null)
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
await expect(svc.downloadAndInstallWithProgress(vi.fn())).rejects.toThrow(
@@ -227,12 +236,14 @@ describe('TauriUpdaterService', () => {
errorSpy.mockRestore()
})
- it('logs error and rethrows when download fails', async () => {
+ it('leaves the app running when the download or signature check fails', async () => {
const err = new Error('download error')
+ const install = vi.fn()
vi.mocked(check).mockResolvedValueOnce({
version: '2.0.0',
- downloadAndInstall: vi.fn().mockRejectedValue(err),
- } as any)
+ download: vi.fn().mockRejectedValue(err),
+ install,
+ } as unknown as Update)
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
await expect(svc.downloadAndInstallWithProgress(vi.fn())).rejects.toBe(err)
@@ -240,17 +251,36 @@ describe('TauriUpdaterService', () => {
'Error downloading update with progress in Tauri:',
err
)
+ expect(invoke).not.toHaveBeenCalledWith('shutdown_for_update')
+ expect(install).not.toHaveBeenCalled()
+ errorSpy.mockRestore()
+ })
+
+ it('does not run the installer when the shutdown fails', async () => {
+ const err = new Error('shutdown failed')
+ const install = vi.fn()
+ vi.mocked(invoke).mockRejectedValueOnce(err)
+ vi.mocked(check).mockResolvedValueOnce({
+ version: '2.0.0',
+ download: vi.fn().mockResolvedValue(undefined),
+ install,
+ } as unknown as Update)
+ const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
+
+ await expect(svc.downloadAndInstallWithProgress(vi.fn())).rejects.toBe(err)
+ expect(install).not.toHaveBeenCalled()
errorSpy.mockRestore()
})
it('handles errors in progress callback gracefully', async () => {
- const mockDownloadAndInstall = vi.fn().mockImplementation(async (cb) => {
+ const download = vi.fn().mockImplementation(async (cb) => {
cb({ event: 'Started' })
})
vi.mocked(check).mockResolvedValueOnce({
version: '2.0.0',
- downloadAndInstall: mockDownloadAndInstall,
- } as any)
+ download,
+ install: vi.fn().mockResolvedValue(undefined),
+ } as unknown as Update)
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
const badCallback = vi.fn().mockImplementation(() => {
diff --git a/web-app/src/services/updater/tauri.ts b/web-app/src/services/updater/tauri.ts
index cc7c6392f..a6be11acb 100644
--- a/web-app/src/services/updater/tauri.ts
+++ b/web-app/src/services/updater/tauri.ts
@@ -147,8 +147,13 @@ export class TauriUpdaterService extends DefaultUpdaterService {
throw new Error('No update available')
}
- // Use Tauri's downloadAndInstall with progress callback
- await update.downloadAndInstall((event) => {
+ // Download and verify first, then stop everything the app started, then
+ // run the installer. `downloadAndInstall` would exit the process (Windows)
+ // or swap the bundle (macOS/Linux) without raising `RunEvent::Exit`, so
+ // the engine, MCP servers and agent shells would be left running. The
+ // cleanup sits between the two so a failed download or signature check
+ // leaves the running app untouched.
+ await update.download((event) => {
try {
// Forward the event to the callback
progressCallback(event as UpdateProgressEvent)
@@ -156,6 +161,8 @@ export class TauriUpdaterService extends DefaultUpdaterService {
console.warn('Error in download progress callback:', callbackError)
}
})
+ await invoke('shutdown_for_update')
+ await update.install()
} catch (error) {
console.error('Error downloading update with progress in Tauri:', error)
throw error