Skip to content

Terminal: restore original termios on panic and terminating signals - #105

Merged
ElianThorne merged 1 commit into
TruFoundation:mainfrom
exlier:fix/tty-restore-on-panic
Aug 11, 2026
Merged

Terminal: restore original termios on panic and terminating signals#105
ElianThorne merged 1 commit into
TruFoundation:mainfrom
exlier:fix/tty-restore-on-panic

Conversation

@exlier

@exlier exlier commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

Problem: when the shell panics or is killed while the terminal is in raw/cbreak mode, the terminal can be left unusable (no echo / broken input).
Fix: save the original termios at startup and restore it from a panic hook and for terminating signals (SIGTERM, SIGQUIT, SIGHUP).
Files changed: src/tty.rs (new), src/main.rs, src/job_control.rs.
This prevents users from needing to run reset or reopen the terminal after a crash. Closes #94

Summary by CodeRabbit

  • Bug Fixes
    • Terminal settings are now restored when the application exits due to termination signals, including SIGTERM, SIGQUIT, and SIGHUP.
    • Terminal settings are also restored after a panic, helping prevent the terminal from being left in an unusable state.
    • The application warns if it cannot save the original terminal configuration at startup.

Save original termios at startup and restore on panic / SIGTERM/SIGQUIT/SIGHUP. Fixes TruFoundation#94.
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 48cb9566-1736-42e7-a46d-84f81d2194b2

📥 Commits

Reviewing files that changed from the base of the PR and between da28f17 and 6f0e4e5.

📒 Files selected for processing (3)
  • src/job_control.rs
  • src/main.rs
  • src/tty.rs

📝 Walkthrough

Walkthrough

The shell now saves stdin terminal attributes at startup and restores them during panic handling and selected termination signals. Restoration uses synchronized global state and best-effort system calls.

Changes

Terminal cleanup

Layer / File(s) Summary
Termios storage and restoration
src/tty.rs
Stores original stdin termios attributes once and restores them with tcsetattr.
Startup and panic cleanup
src/main.rs
Saves terminal attributes at startup, warns on save failure, and restores them before default panic handling.
Termination signal cleanup
src/job_control.rs
Handles SIGTERM, SIGQUIT, and SIGHUP, restores terminal attributes, and exits with status 128 + signal.

Estimated code review effort: 2 (Simple) | ~10 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Shell
  participant tty
  participant Terminal
  participant SignalOrPanic
  Shell->>tty: save_original_termios
  tty->>Terminal: tcgetattr
  Terminal-->>tty: original attributes
  SignalOrPanic->>tty: restore_original_termios
  tty->>Terminal: tcsetattr
Loading

Possibly related PRs

Suggested reviewers: elianthorne

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes restoring terminal settings during panics and terminating signals.
Linked Issues check ✅ Passed The changes save terminal settings and restore them during panics and SIGTERM, SIGQUIT, or SIGHUP termination, meeting issue #94.
Out of Scope Changes check ✅ Passed The changes are limited to terminal-state preservation, panic cleanup, and terminating-signal handling required by issue #94.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)

❌ Error creating Unit Test PR.

  • Create PR with unit tests
  • Commit unit tests in branch fix/tty-restore-on-panic

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

Note

Unit test generation is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.


Generating unit tests... This may take up to 20 minutes.

@ElianThorne
ElianThorne merged commit ce14503 into TruFoundation:main Aug 11, 2026
2 checks passed
@coderabbitai

coderabbitai Bot commented Aug 11, 2026

Copy link
Copy Markdown

❌ Failed to create PR with unit tests: AGENT_CHAT: Failed to open pull request

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.

Terminal: Handle raw mode cleanup on panic (ensure terminal restored on abnormal exit)

2 participants