Skip to content

fix(ui): gracefully stop TUI on signals and make Stop idempotent#7

Merged
Iceber merged 1 commit into
mainfrom
fix-signal
Jun 25, 2026
Merged

fix(ui): gracefully stop TUI on signals and make Stop idempotent#7
Iceber merged 1 commit into
mainfrom
fix-signal

Conversation

@Iceber

@Iceber Iceber commented Jun 25, 2026

Copy link
Copy Markdown
Owner

Summary

Fix TUI shutdown handling so cray restores the terminal when it receives process-level exit signals.

Previously, Ctrl-C was only handled when delivered through tcell as a key event. In SSH or launcher scenarios, Ctrl-C may arrive as a real SIGINT, causing the Go process to exit directly and bypass tview/tcell cleanup. That could leave the terminal in a broken raw/alternate-screen state.

fix: #6

Changes

  • Handle SIGINT, SIGHUP, SIGTERM, and SIGQUIT during TUI execution.
  • Route signal-based exits through the same App.Stop() cleanup path as keyboard exits.
  • Make App.Stop() idempotent so repeated stop requests are safe.
  • Add a guard for stop requests that arrive during early TUI startup.

Listen for SIGINT/SIGHUP/SIGTERM/SIGQUIT and trigger Stop. Guard
cleanup with sync.Once so Stop is safe to call repeatedly, and
defer tviewApp.Stop() to the UI thread via AfterDrawFunc to avoid
deadlock when invoked from the signal goroutine. A stopping flag
short-circuits Run() if shutdown is requested before the event
loop starts.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves c-ray’s TUI shutdown path so the terminal is reliably restored when the process receives OS signals (e.g., SIGINT from Ctrl-C in SSH/launcher contexts), routing those exits through the same cleanup logic as in-app key handling and making shutdown requests safe to repeat.

Changes:

  • Add process signal handling (SIGINT, SIGHUP, SIGTERM, SIGQUIT) to trigger App.Stop() during Run().
  • Make App.Stop() idempotent via sync.Once and an atomic stopping flag.
  • Add a first-draw guard to ensure stop requests during early startup still result in the TUI being stopped cleanly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Iceber
Iceber merged commit 4d42559 into main Jun 25, 2026
1 check passed
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]: ctr-c completely messes up the terminal

2 participants