Skip to content

fix(shutdown): hold a Windows session end for unsaved editor changes - #3675

Closed
kdcokenny wants to merge 1 commit into
mainfrom
fix/windows-session-end-unsaved
Closed

kdcokenny wants to merge 1 commit into
mainfrom
fix/windows-session-end-unsaved

Conversation

@kdcokenny

@kdcokenny kdcokenny commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Description

What users saw: on Windows, restart, shutdown or sign-out with unsaved editor changes ended
Emdash with no "Save changes before quitting?" prompt, and the edit was lost. Windows does not
emit before-quit for a session end, so the quit coordinator was never asked.

Now: the main window listens for Electron's query-session-end (Windows only; Electron
45.4.1 documents that event.preventDefault() there can delay the system shutdown). When the
renderer has reported unsaved changes, the coordinator calls preventDefault() and starts the
existing quit flow (handleQuitRequested()), so the same prompts appear: "Save changes before
quitting?", then "Quit Emdash?".

  • No unsaved changes: nothing is held; the session ends as before.
  • Critical session end (reasons includes critical), an updater install pending, or a
    renderer that cannot answer: nothing is held.
  • Already confirming: a repeated query is held again without a second prompt.

"Has unsaved changes" source: query-session-end is answered synchronously, so main keeps a
copy of the flag. The renderer reports openFileStore.dirtyEntries().length > 0 (the source the
quit prompt already uses) through a new host.setHasUnsavedChanges Wire procedure, via a MobX
reaction after shutdownReady(). OpenFileStore's entry map is now observable.map
(non-deep, mutations in runInAction), so the reaction also follows files opened later. Main
resets the flag whenever the renderer is invalidated.

What Windows allows (what the user will see): returning "do not end" from
WM_QUERYENDSESSION makes Windows show its own full-screen "Emdash is preventing you from
restarting / shutting down / signing out" screen with "… anyway" and "Cancel". The Emdash prompt
is open behind it.

  • Cancel: back to the desktop with the Emdash prompt. The restart is cancelled. Save All or
    Discard then quits Emdash (the restart is not resumed; the user starts it again); Cancel keeps
    Emdash open with the edit intact.
  • "… anyway" (or forced shutdown, AutoEndTasks, a critical session end): Windows ends
    Emdash and the unsaved edit is lost, as today. The app cannot prevent this.

If the hold turns out to be unreliable on real Windows, the fallback would be to write dirty
buffers to a recovery location on session-end and restore them on the next launch. Not built
here.

Unchanged: macOS and Linux (query-session-end is emitted on Windows only), and normal quit
on Windows (window close, Ctrl+Q, tray; all still go through before-quit). The Ctrl+Q focus
issue from the issue's notes is not addressed.

High-risk area: Electron shutdown coordination (src/main/bootstrap/shutdown/), plus a new
Wire procedure on the host contract.

Related issues

Fixes #3590

Testing

From apps/emdash-desktop (Linux):

Check Result
pnpm run format:check (root) pass
pnpm run lint (root) pass
nx run @emdash/emdash-desktop:typecheck pass
vitest run --project node 5536 passed, 2 failed, 11 skipped. xterm-tmux.test.ts also fails on main here; host-supervisor.ssh.test.ts passes when run alone (2/2, twice)
shutdown.test.ts + open-file-store.test.ts pass; the 8 new tests fail without the change

New tests: session end not held without unsaved changes; held with them, and Cancel keeps the
app open; Save/confirm quits; a repeated query is held without a second prompt; not held after
the flag clears, for a critical session end, after a main-frame navigation, or with an updater
install pending; a MobX reaction on dirtyEntries() sees edits in a file opened after it
started.

Windows: not yet tested in the running app.

Checklist
  • I kept this PR small and focused
  • I ran a self-review before opening this PR
  • I ran the relevant local checks or explained why not
  • I updated docs when behavior or setup changed (no docs change needed)
  • I added or updated tests when behavior changed, or explained why not
  • I only added comments where the logic is not obvious
  • I used Conventional Commits for commit
    messages and, when possible, the PR title

Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

Windows does not emit before-quit for a restart, shutdown or sign-out, so
unsaved edits were lost without a prompt. The main window's
query-session-end now holds the session end (preventDefault) only when the
renderer has reported unsaved changes, and opens the existing quit prompt.
The renderer reports the flag from OpenFileStore.dirtyEntries(), whose entry
map is now observable so the report follows files opened later.

Fixes #3590

Copy link
Copy Markdown
Member Author

Tested on a real Windows machine (Windows Server 2022, PR build at 95ac50fba8, run non-elevated, compared with v1.2.15). Result: this approach does not work as it stands, and in two cases it is worse than v1.2.15. Staying a draft; a different design will replace it.

What works

  • The hold: Windows shows "Emdash: This app is preventing you from restarting" where v1.2.15 restarts in 16 s and loses the edit.
  • Cancel on the Windows screen within about 5 s, then Save All or Discard in Emdash's prompt: the file is saved or left alone and Emdash quits. The restart does not resume; the user has to start it again.
  • Nothing unsaved: no delay and no screen. Window close, tray quit and Ctrl+Q behave as on v1.2.15.

What does not

Case Result
Restart, user does nothing Emdash's prompt is behind the full-screen Windows screen. After 60 s the quit confirmation deadline (CONFIRMATION_DEADLINE_MS) settles as "quit without saving", and the restart goes through. Edit lost, 60 s later than on v1.2.15. Same for shut down and sign out.
Restart, Cancel on the Windows screen, Emdash's prompt answered late The same deadline quits Emdash and discards the edit, although the restart was cancelled. Worse than v1.2.15.
Restart, Cancel on the Windows screen after about 10 s, then Cancel in Emdash The app stays open with the edit, but Ctrl+S and Save All fail with no message (content.write times out). The edit cannot be saved. Worse than v1.2.15.
"Restart anyway", shutdown /r /f Edit lost, as expected; Windows does not let an app prevent these.

Why

When the session end begins, Windows ends all of the app's worker processes at once (exit code 0x40010004), including the one that writes files, and new processes cannot start while the session end is pending (0xC000026B). The supervisor retries six times in about 10 s and gives up, and nothing restarts the workers after the user cancels the restart. So the window survives the session end, but the processes it needs to save do not.

Next

Holding the session end cannot protect the edit in the cases above, and never in the forced ones. The replacement keeps a backup of unsaved editor buffers while the user types and restores them at the next start, which does not depend on what Windows allows during a restart.

Copy link
Copy Markdown
Member Author

Closing in favour of #3676. Emdash already writes a backup of unsaved editor buffers; it was never restored because the restore ran before the reopened tabs had loaded their files. #3676 fixes the restore, which covers a Windows restart without depending on what Windows allows during a session end.

@kdcokenny kdcokenny closed this Dec 5, 2026
@kdcokenny
kdcokenny deleted the fix/windows-session-end-unsaved branch December 5, 2026 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: Windows restart or shutdown with unsaved editor changes loses them without a prompt

1 participant