Repository navigation
Description
Bug description
On macOS (observed on version 3.5.1 from the Mac App Store), closing the application window with the red cross button ('X') hides the app to the Dock as expected. However, when right-clicking the app in the Dock and selecting Quit (or pressing Cmd+Q), the app only partially quits:
- The menu bar tray icon disappears and backend services (Express/KDS/SQLite/Bonjour) stop.
- The app icon remains persistent in the macOS Dock with an active process dot.
- Clicking the Dock icon does nothing (no window appears, no error is shown).
- Launching FloCafe from Spotlight or Finder while in this state also does nothing (locked out by the single instance lock).
- The user must right-click the Dock icon and select Quit a second time to actually terminate the process and remove it from the Dock.
Steps to reproduce
- Launch FloCafe on macOS.
- Click the red close button ('X') on the main window. The window hides to the Dock as expected.
- Right-click the FloCafe icon in the macOS Dock and click Quit (or press Cmd+Q while focused).
- Notice that the tray/menu bar icon disappears, but the FloCafe Dock icon remains present with an active indicator dot.
- Click the Dock icon: nothing happens.
- Try launching FloCafe from Spotlight or Applications: nothing happens.
- Right-click the FloCafe Dock icon again and click Quit a second time: only now does the app fully exit and disappear from the Dock.
Expected behavior
Selecting Quit from the Dock (or via Cmd+Q) should fully shut down all services and terminate the macOS application process on the first attempt, cleanly removing the icon from the Dock.
Actual behavior
The first Quit attempt runs background cleanup, but the Electron/macOS process never exits. It enters a zombie headless state where clicking the Dock icon is ignored. A second Quit command is required to terminate the process.
Operating System
macOS
Installation Method
Mac App Store (also applies to direct/DMG packaged builds)
FloCafe version
3.5.1 (and current main branch)
Root Cause & Code Evidence
-
Window Close Hides Window:
Inmain/index.ts(L700-L705):mainWindow.on('close', (event) => { if (!isQuitting) { event.preventDefault(); mainWindow?.hide(); } });
-
First Quit Cancels Termination to Run Async Cleanup:
When Quit is clicked from the Dock, Electron emitsbefore-quit(settingisQuitting = true), closes the window, and emitswill-quit.
Inmain/shutdown.ts(L474-L481):app.on('will-quit', (event: ShutdownEvent) => { if (cleanupFinished) { destroyWindow(); return; } event.preventDefault(); quitAfterCleanup(); });
Because
cleanupFinishedis initiallyfalse,event.preventDefault()is called synchronously. This instructs macOS Cocoa to abort the quit sequence (NSTerminateCancel). -
Post-Cleanup
app.quit()Fails to Terminate Process on macOS:
Inmain/shutdown.ts(L453-L460):void runCleanup().then( () => { const exitCode = getQuitExitCode(); destroyWindow(); if (isInstallingUpdate()) return; if (exitCode === 0) app.quit(); else app.exit(exitCode); },
When cleanup completes,
cleanupFinishedbecomestrue. For clean exits (exitCode === 0), it callsapp.quit().
However, on macOS, once Cocoa termination has been cancelled and all windows have already been closed/destroyed, callingapp.quit()from an asynchronous Promise callback does not re-terminate the headless Cocoa run loop. (Notice that for non-zero exits,app.exit(exitCode)is called, which terminates immediately, butexitCode === 0usesapp.quit()). -
Dock Activation Ignores Clicks:
When the user clicks the stuck Dock icon, macOS firesapp.on('activate').
Inmain/index.ts(L765) andmain/runtime-recovery.ts(L29):if (input.shutdownRequested || input.state === 'stopping') return 'ignore';
Because
isQuittingistrueandruntimeStateis'stopping', activation returns'ignore'. No window is restored or created. -
Second Instance Lock Blocks Relaunching from Spotlight/Finder:
Inmain/index.ts(L565-L570):
When the user tries to open FloCafe again from Finder, the second instance exits due to the single-instance lock and notifies the primary instance viasecond-instance. The primary instance callshandleMainWindowActivation(), which again evaluates to'ignore', locking the user out of the application entirely. -
Why the Second Quit Works:
When the user right-clicks the Dock and clicks Quit the 2nd time, macOS sends a fresh quit event.
Inapp.on('will-quit'):if (cleanupFinished) { destroyWindow(); return; }
Because
cleanupFinishedwas already set totrueduring the first quit attempt,event.preventDefault()is NOT called. macOS Cocoa proceeds with termination and the process finally exits. -
Flawed Unit Test in
tests/shutdown-lifecycle.test.ts:
Intests/shutdown-lifecycle.test.ts(L412-L426):assert.equal(app.quitCount, 1, `${label} resumes Electron quit after cleanup`); ... const secondWillQuit = { prevented: false, preventDefault: () => { secondWillQuit.prevented = true; } }; app.emit('will-quit', secondWillQuit); assert.equal(secondWillQuit.prevented, false, `${label} allows the resumed quit`);
The mock
AppDoubleonly incremented a counter onquit(), and the test script manually synthesized a secondwill-quitevent viaapp.emit('will-quit'), masking the fact that real Electron on macOS does not trigger this cycle when all windows are already closed.
Edge Cases Evaluated
- Auto-updater impact: On MAS builds,
electron-updateris disabled (isStoreBuild). On direct/DMG builds,createRestartAndInstallHandlerrunsrunCleanup()before callingquitAndInstall(), andisInstallingUpdate()already guards line 457 ofshutdown.ts. Callingapp.exit(0)after cleanup when!isInstallingUpdate()does not interfere with updater relaunches. - Cmd+Q while window is open: Hits the exact same flow because
will-quitoccurs after window closure. - System Tray Quit: Hits the exact same flow.
- Failed cleanup: Calls
app.exit(1)and exits immediately; the hang only occurs when cleanup succeeds cleanly.
Suggested Fix
In main/shutdown.ts:
After runCleanup() has completely drained all database connections, sockets, and HTTP requests, and after verifying !isInstallingUpdate(), terminate the process with app.exit(exitCode) (or app.exit(0)) instead of calling app.quit(), ensuring the headless macOS process exits promptly on the first quit command.
Activity
fix(macos): exit process cleanly after cleanup on quit (#644) (#650)
khaira777 commentedon Sep 6, 2026
Fixed in #650 and merged into main (3dfb3f5).