Skip to content

[Bug]: macOS app remains in Dock after quitting and requires quitting a second time #644

Closed

Description

@khaira777

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

  1. Launch FloCafe on macOS.
  2. Click the red close button ('X') on the main window. The window hides to the Dock as expected.
  3. Right-click the FloCafe icon in the macOS Dock and click Quit (or press Cmd+Q while focused).
  4. Notice that the tray/menu bar icon disappears, but the FloCafe Dock icon remains present with an active indicator dot.
  5. Click the Dock icon: nothing happens.
  6. Try launching FloCafe from Spotlight or Applications: nothing happens.
  7. 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

  1. Window Close Hides Window:
    In main/index.ts (L700-L705):

    mainWindow.on('close', (event) => {
      if (!isQuitting) {
        event.preventDefault();
        mainWindow?.hide();
      }
    });
  2. First Quit Cancels Termination to Run Async Cleanup:
    When Quit is clicked from the Dock, Electron emits before-quit (setting isQuitting = true), closes the window, and emits will-quit.
    In main/shutdown.ts (L474-L481):

    app.on('will-quit', (event: ShutdownEvent) => {
      if (cleanupFinished) {
        destroyWindow();
        return;
      }
      event.preventDefault();
      quitAfterCleanup();
    });

    Because cleanupFinished is initially false, event.preventDefault() is called synchronously. This instructs macOS Cocoa to abort the quit sequence (NSTerminateCancel).

  3. Post-Cleanup app.quit() Fails to Terminate Process on macOS:
    In main/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, cleanupFinished becomes true. For clean exits (exitCode === 0), it calls app.quit().
    However, on macOS, once Cocoa termination has been cancelled and all windows have already been closed/destroyed, calling app.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, but exitCode === 0 uses app.quit()).

  4. Dock Activation Ignores Clicks:
    When the user clicks the stuck Dock icon, macOS fires app.on('activate').
    In main/index.ts (L765) and main/runtime-recovery.ts (L29):

    if (input.shutdownRequested || input.state === 'stopping') return 'ignore';

    Because isQuitting is true and runtimeState is 'stopping', activation returns 'ignore'. No window is restored or created.

  5. Second Instance Lock Blocks Relaunching from Spotlight/Finder:
    In main/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 via second-instance. The primary instance calls handleMainWindowActivation(), which again evaluates to 'ignore', locking the user out of the application entirely.

  6. Why the Second Quit Works:
    When the user right-clicks the Dock and clicks Quit the 2nd time, macOS sends a fresh quit event.
    In app.on('will-quit'):

    if (cleanupFinished) {
      destroyWindow();
      return;
    }

    Because cleanupFinished was already set to true during the first quit attempt, event.preventDefault() is NOT called. macOS Cocoa proceeds with termination and the process finally exits.

  7. Flawed Unit Test in tests/shutdown-lifecycle.test.ts:
    In tests/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 AppDouble only incremented a counter on quit(), and the test script manually synthesized a second will-quit event via app.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-updater is disabled (isStoreBuild). On direct/DMG builds, createRestartAndInstallHandler runs runCleanup() before calling quitAndInstall(), and isInstallingUpdate() already guards line 457 of shutdown.ts. Calling app.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-quit occurs 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

  1. added
    area:backend
    platform:macos
    on Sep 5, 2026
  2. khaira777 commented on Sep 6, 2026

    @khaira777
    ContributorAuthor

    Fixed in #650 and merged into main (3dfb3f5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:backendplatform:macos

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions