Skip to content

feat(mmterm): auto-save session on SIGTERM/SIGHUP/SIGINT#91

Open
roramirez wants to merge 1 commit into
mainfrom
feat/session-save-on-signal
Open

feat(mmterm): auto-save session on SIGTERM/SIGHUP/SIGINT#91
roramirez wants to merge 1 commit into
mainfrom
feat/session-save-on-signal

Conversation

@roramirez

Copy link
Copy Markdown
Owner

Summary

When mmterm is terminated unattended — PC shutdown/reboot (SIGTERM from systemd), logout / display-server teardown (SIGHUP), or kill (SIGINT) — the session was lost. Persistence only ran through the interactive Ctrl+Q / window-close prompt (InputMode::QuitSave), which needs a human keypress.

This adds signal handling so the session is auto-saved on those signals and can be restored on the next launch. Reopening with the same --scope restores the tabs, panes, layout, cwds, and theme, because the save routes through the existing scope-aware session_path().

Changes

  • Cargo.toml — add signal-hook under [target.'cfg(unix)'.dependencies].
  • src/main.rs — new App.shutdown_requested: Arc<AtomicBool> flag; spawn_signal_watcher() runs a background thread on Signals::new([SIGTERM, SIGHUP, SIGINT]), sets the flag, and nudges the event loop awake via the existing EventLoopProxy; wired in main() on Unix.
  • src/winit_handler.rsuser_event checks the flag first; if set it saves and exits without prompting (an unattended shutdown must not block).
  • src/restore.rssave_session_on_shutdown() reuses build_saved_session() + session::save_to(), gated on general.restore_session.
  • CHANGELOG.md — entry under [Unreleased] → Added.
  • Tests in src/restore_test.rs.

Design notes: the actual save happens on the main thread (safe to touch App/self.window, no async-signal-safety constraints); the watcher thread only flips a flag and wakes the loop. restore_session = false still exits promptly, just without saving. Interactive quit paths are unchanged.

How to test

Automated:

cargo fmt --check
cargo clippy --locked -- -D warnings
cargo test

New tests: save_session_on_shutdown_{writes_when_restore_enabled,is_noop_when_restore_disabled,honors_scope_path}.

Manual (end-to-end):

  1. cargo run -- --scope worktest, open a couple of tabs / splits, cd somewhere.
  2. From another shell: kill -TERM $(pgrep -n mmterm) (repeat with -HUP, -INT).
  3. Confirm ~/.config/mmterm/sessions/worktest.toml was written/updated.
  4. cargo run -- --scope worktest → tabs, layout, cwds, theme restored.
  5. Verify the interactive Ctrl+Q prompt and normal quit are unchanged.

An unattended termination — PC shutdown/reboot (SIGTERM), session
teardown (SIGHUP), or kill (SIGINT) — previously lost the session
because persistence only ran through the interactive Ctrl+Q /
window-close prompt.

A background signal-watcher thread now flips a flag and wakes the
winit event loop via the existing EventLoopProxy; the main thread
saves in user_event and exits without prompting. The save reuses
build_saved_session + session::save_to and routes through the
scope-aware session_path(), so reopening with the same --scope
restores tabs/panes/layout/cwds/theme. Respects the
general.restore_session gate.
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.

1 participant