diff --git a/src/handlers/appInfoHandlers.ts b/src/handlers/appInfoHandlers.ts index bfb8d17e..2b1f57ce 100644 --- a/src/handlers/appInfoHandlers.ts +++ b/src/handlers/appInfoHandlers.ts @@ -1,4 +1,4 @@ -import { app, ipcMain, shell } from 'electron'; +import { app, ipcMain } from 'electron'; import { IPC_CHANNELS } from '../constants'; /** * Handles static information about the app in IPC channels. diff --git a/src/install/installationValidator.ts b/src/install/installationValidator.ts index 2f7bd09d..8ffc032f 100644 --- a/src/install/installationValidator.ts +++ b/src/install/installationValidator.ts @@ -39,7 +39,7 @@ export class InstallationValidator { // Failed - try the parent dir const parsed = path.parse(file); await fs.access(parsed.dir); - shell.openPath(parsed.dir); + await shell.openPath(parsed.dir); } catch (error) { // Nothing works. Log, notify, quit. log.error( diff --git a/src/main-process/appWindow.ts b/src/main-process/appWindow.ts index 59c7fb0c..2094a436 100644 --- a/src/main-process/appWindow.ts +++ b/src/main-process/appWindow.ts @@ -1,4 +1,4 @@ -import { BrowserWindow, screen, app, shell, ipcMain, Tray, Menu, dialog, MenuItem, type Point } from 'electron'; +import { BrowserWindow, screen, app, shell, ipcMain, Tray, Menu, dialog, MenuItem } from 'electron'; import path from 'node:path'; import Store from 'electron-store'; import { AppWindowSettings } from '../store'; @@ -103,9 +103,9 @@ export class AppWindow { }); } - public loadComfyUI(serverArgs: ServerArgs) { + public async loadComfyUI(serverArgs: ServerArgs) { const host = serverArgs.host === '0.0.0.0' ? 'localhost' : serverArgs.host; - this.window.loadURL(`http://${host}:${serverArgs.port}`); + await this.window.loadURL(`http://${host}:${serverArgs.port}`); } public openDevTools(): void { @@ -146,7 +146,7 @@ export class AppWindow { } else { const appResourcesPath = getAppResourcesPath(); const frontendPath = path.join(appResourcesPath, 'ComfyUI', 'web_custom_versions', 'desktop_app'); - this.window.loadFile(path.join(frontendPath, 'index.html'), { hash: urlPath }); + await this.window.loadFile(path.join(frontendPath, 'index.html'), { hash: urlPath }); } } diff --git a/src/main-process/comfyDesktopApp.ts b/src/main-process/comfyDesktopApp.ts index 70eee3bc..ce4b056d 100644 --- a/src/main-process/comfyDesktopApp.ts +++ b/src/main-process/comfyDesktopApp.ts @@ -1,4 +1,4 @@ -import { app, dialog, ipcMain, type Point } from 'electron'; +import { app, dialog, ipcMain } from 'electron'; import log from 'electron-log/main'; import * as Sentry from '@sentry/electron/main'; import { graphics } from 'systeminformation'; @@ -35,7 +35,7 @@ export class ComfyDesktopApp { } public async initialize(): Promise { - this.comfySettings.loadSettings(); + await this.comfySettings.loadSettings(); this.registerIPCHandlers(); this.initializeTodesktop(); await this.setupGPUContext(); @@ -161,8 +161,8 @@ export class ComfyDesktopApp { await appWindow.loadRenderer('welcome'); } - return new Promise((resolve) => { - ipcMain.on(IPC_CHANNELS.INSTALL_COMFYUI, async (event, installOptions: InstallOptions) => { + return new Promise((resolve, reject) => { + ipcMain.on(IPC_CHANNELS.INSTALL_COMFYUI, (_event, installOptions: InstallOptions) => { const installWizard = new InstallWizard(installOptions); useDesktopConfig().set('basePath', installWizard.basePath); @@ -171,30 +171,34 @@ export class ComfyDesktopApp { useDesktopConfig().set('selectedDevice', device); } - await installWizard.install(); - useDesktopConfig().set('installState', 'installed'); - appWindow.maximize(); - resolve(installWizard.basePath); + installWizard + .install() + .then(() => { + useDesktopConfig().set('installState', 'installed'); + appWindow.maximize(); + resolve(installWizard.basePath); + }) + .catch((reason) => { + reject(reason); + }); }); }); } async startComfyServer(serverArgs: ServerArgs) { - app.on('before-quit', async () => { + app.on('before-quit', () => { if (!this.comfyServer) { return; } - try { - log.info('Before-quit: Killing Python server'); - await this.comfyServer.kill(); - } catch (error) { + log.info('Before-quit: Killing Python server'); + this.comfyServer.kill().catch((error) => { log.error('Python server did not exit properly'); log.error(error); - } + }); }); log.info('Server start'); - this.appWindow.loadRenderer('server-start'); + await this.appWindow.loadRenderer('server-start'); DownloadManager.getInstance(this.appWindow!, getModelsDirectory(this.basePath)); diff --git a/src/main-process/comfyServer.ts b/src/main-process/comfyServer.ts index 5c9ce899..c7614e9c 100644 --- a/src/main-process/comfyServer.ts +++ b/src/main-process/comfyServer.ts @@ -98,7 +98,7 @@ export class ComfyServer { async start() { await rotateLogFiles(app.getPath('logs'), 'comfyui', 50); - return new Promise(async (resolve, reject) => { + return new Promise((resolve, reject) => { const comfyUILog = log.create({ logId: 'comfyui' }); comfyUILog.transports.file.fileName = 'comfyui.log'; @@ -136,19 +136,19 @@ export class ComfyServer { this.comfyServerProcess = comfyServerProcess; - try { - await waitOn({ - resources: [`${this.baseUrl}/queue`], - timeout: ComfyServer.MAX_FAIL_WAIT, - interval: ComfyServer.CHECK_INTERVAL, + waitOn({ + resources: [`${this.baseUrl}/queue`], + timeout: ComfyServer.MAX_FAIL_WAIT, + interval: ComfyServer.CHECK_INTERVAL, + }) + .then(() => { + log.info('Python server is ready'); + resolve(); + }) + .catch((error) => { + log.error('Server failed to start:', error); + reject(new Error('Python Server Failed To Start Within Timeout.')); }); - - log.info('Python server is ready'); - resolve(); - } catch (error) { - log.error('Server failed to start:', error); - reject('Python Server Failed To Start Within Timeout.'); - } }); } diff --git a/src/main.ts b/src/main.ts index 2b8ec784..31b1a71b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -38,22 +38,26 @@ if (!gotTheLock) { log.info('App already running. Exiting...'); app.quit(); } else { - app.on('ready', async () => { + app.on('ready', () => { log.debug('App ready'); - try { - const store = await DesktopConfig.load(shell); - if (!store) throw new Error('Unknown error loading app config on startup.'); - } catch (error) { - dialog.showErrorBox('User Data', `Unknown error whilst writing to user data folder:\n\n${error}`); - app.exit(20); - } - - await startApp(); + startApp().catch((reason) => { + log.error('Unhandled exception in app startup', reason); + app.exit(2020); + }); }); } async function startApp() { + try { + const store = await DesktopConfig.load(shell); + if (!store) throw new Error('Unknown error loading app config on startup.'); + } catch (error) { + dialog.showErrorBox('User Data', `Unknown error whilst writing to user data folder:\n\n${error}`); + app.exit(20); + return; + } + try { const appWindow = new AppWindow(); appWindow.onClose(() => { @@ -93,7 +97,7 @@ async function startApp() { await comfyDesktopApp.startComfyServer({ host, port, extraServerArgs }); } appWindow.sendServerStartProgress(ProgressStatus.READY); - appWindow.loadComfyUI({ host, port, extraServerArgs }); + await appWindow.loadComfyUI({ host, port, extraServerArgs }); } catch (error) { appWindow.sendServerStartProgress(ProgressStatus.ERROR); appWindow.send(IPC_CHANNELS.LOG_MESSAGE, error); diff --git a/src/preload.ts b/src/preload.ts index 6f42f9dd..417c1b19 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -2,7 +2,6 @@ import { contextBridge, ipcRenderer } from 'electron'; import { IPC_CHANNELS, ELECTRON_BRIDGE_API, ProgressStatus, DownloadStatus } from './constants'; import type { DownloadState } from './models/DownloadManager'; import path from 'node:path'; -import { DesktopConfig } from './store/desktopConfig'; /** * Open a folder in the system's default file explorer. diff --git a/src/store/desktopConfig.ts b/src/store/desktopConfig.ts index d3f61115..93e4e93d 100644 --- a/src/store/desktopConfig.ts +++ b/src/store/desktopConfig.ts @@ -4,7 +4,6 @@ import { app, dialog } from 'electron'; import path from 'node:path'; import fs from 'fs/promises'; import type { DesktopSettings } from '.'; -import type { TorchDeviceType } from '../preload'; /** Backing ref for the singleton config instance. */ let current: DesktopConfig; diff --git a/src/virtualEnvironment.ts b/src/virtualEnvironment.ts index 58269cde..b59933eb 100644 --- a/src/virtualEnvironment.ts +++ b/src/virtualEnvironment.ts @@ -6,7 +6,6 @@ import { app } from 'electron'; import * as pty from 'node-pty'; import * as os from 'os'; import { getDefaultShell } from './shell/util'; -import { DesktopConfig } from './store/desktopConfig'; import type { TorchDeviceType } from './preload'; type ProcessCallbacks = {