You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Save state and shut Claude sessions down cleanly when Windows shuts down or logs off #167
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
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.
Ask Windows for time. Call ShutdownBlockReasonCreate(hwnd, "Saving N agent sessions…") so Windows shows the reason and gives the app time.
Shut down sessions. Run the existing Claude shutdown loop under a shorter budget, then ShutdownBlockReasonDestroy.
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.
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".
Activity
AThraen commentedon Nov 10, 2026
Related change in #191.
MainWindownow listens toApplication.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
OnClosingrun (with the shutdown board and Claude exit waits), and doesstate.jsonget written? If not, do a synchronousSaveStateAsyncflush inSessionEnding. Keep the Claude exit waits inside the time Windows allows before it shows "this app is preventing shutdown".