Skip to content

Save state and shut Claude sessions down cleanly when Windows shuts down or logs off #167

Open

Description

@AThraen

This was generated by AI during triage.

Summary

When Windows is shutting down or the user logs off, save state and shut Claude sessions down properly. Today CSM does nothing special, and the OS kills it partway through.

Current behaviour

There is no SessionEnding / WM_QUERYENDSESSION handling anywhere in the code. On reboot or logoff:

  • OnClosing's orderly shutdown (the shutdown board, per-session exit waits, ClaudeShutdownBudgetMs) may never run, or may be cut short;
  • state.json may miss changes made since the last save. Most saves are fire-and-forget and debounced;
  • claude.exe children are killed without writing their config. That is the unlocked ~/.claude.json race that the shutdown loop exists to avoid.

Herdr fixed exactly this in v0.9.4 ("Restarting a Mac or shutting down Windows keeps your agents' sessions. Herdr saves before the system starts closing apps"). Its Linux build asks logind for a shutdown delay.

Proposal

  1. Save first. Handle Application.SessionEnding (WPF), or WM_QUERYENDSESSION on the main HWND. Do a synchronous final SaveStateAsync first; it is the one thing that must land.
  2. Ask Windows for time. Call ShutdownBlockReasonCreate(hwnd, "Saving N agent sessions…") so Windows shows the reason and gives the app time.
  3. Shut down sessions. Run the existing Claude shutdown loop under a shorter budget, then ShutdownBlockReasonDestroy.
  4. Measure first. Windows force-terminates after a few seconds unless the user waits. Measure how many sessions get a clean exit before choosing the budget (see the measurement notes on ClaudeShutdownBudgetMs).

Notes

  • Log every step to crash.log. A shutdown is otherwise unobservable.
  • Don't add a confirmation prompt. The user has already chosen to shut down.

Activity

  1. AThraen commented on Nov 10, 2026

    @AThraen
    ContributorAuthor

    This was generated by AI during triage.

    Related change in #191. MainWindow now listens to Application.SessionEnding, but only to set _osSessionEnding, which suppresses the "still running?" dialog during logoff. It does not save state or run the clean Claude shutdown. The hook point this issue needs is now there.

    Still to verify: during a real logoff, does OnClosing run (with the shutdown board and Claude exit waits), and does state.json get written? If not, do a synchronous SaveStateAsync flush in SessionEnding. Keep the Claude exit waits inside the time Windows allows before it shows "this app is preventing shutdown".

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions