Skip to content

stats: restore terminal when streaming is interrupted by a signal#1355

Open
Otoru wants to merge 1 commit intoapple:mainfrom
Otoru:fix/stats-streaming-terminal-restore
Open

stats: restore terminal when streaming is interrupted by a signal#1355
Otoru wants to merge 1 commit intoapple:mainfrom
Otoru:fix/stats-streaming-terminal-restore

Conversation

@Otoru
Copy link
Copy Markdown

@Otoru Otoru commented Mar 27, 2026

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update

Motivation and Context

When container stats runs in streaming mode, pressing Ctrl+C sends SIGINT directly to the process before Swift can run the defer block responsible for restoring the terminal. This leaves the alternate screen buffer active and the cursor hidden after exit.

Fix this by wrapping the streaming loop in a withThrowingTaskGroup that races the work against an AsyncSignalHandler for SIGINT and SIGTERM. When a signal arrives, the group is cancelled through normal Swift unwinding, allowing the defer to execute and restore the terminal correctly. This follows the same pattern used in BuildCommand.

runStreaming, collectStats, printStatsTable, and clearScreen are promoted to static methods since none of them read instance state, which is required to safely capture only the containers value in the task closure under Swift 6 strict concurrency.

Fixes #1351

Testing

  • Tested locally
  • Added/updated tests
  • Added/updated docs

@github-actions github-actions bot added the cli label Mar 27, 2026
@dcantah
Copy link
Copy Markdown
Member

dcantah commented Mar 27, 2026

@Otoru We require all commits be signed (gpg/ssh)

@Otoru Otoru force-pushed the fix/stats-streaming-terminal-restore branch from 53678f9 to b79b414 Compare March 27, 2026 11:11
@Otoru
Copy link
Copy Markdown
Author

Otoru commented Mar 27, 2026

@dcantah, thanks for explaining. Fixed.

@dcantah
Copy link
Copy Markdown
Member

dcantah commented Mar 27, 2026

@Otoru Can you remove the unit tests? We can add separately

@Otoru Otoru force-pushed the fix/stats-streaming-terminal-restore branch from b79b414 to eef85e7 Compare March 27, 2026 11:25
@Otoru
Copy link
Copy Markdown
Author

Otoru commented Mar 27, 2026

@dcantah of course

@Otoru Otoru requested a review from dcantah March 27, 2026 21:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: container stats does not restore terminal upon exit

2 participants