Table of Contents
Overview
Signal handling in gosh ensures that the shell behaves predictably and safely when receiving interrupt signals. The implementation focuses on providing a user-friendly experience where commands can be interrupted without losing the shell session.
Signal Types
SIGINT (Ctrl-C)
The primary signal handled by gosh. When pressed:
- During command execution: Interrupts the running command and returns to the prompt
- While shell is idle: Shows a new prompt but does not exit the shell
- During pipeline execution: Interrupts all processes in the pipeline
Behavior Examples
Interrupting a Long-Running Command
gosh> sleep 30
^C
gosh> # Command interrupted, shell continues
Ctrl-C While Idle
gosh> ^C
gosh> # New prompt shown, shell continues running
Pipeline Interruption
gosh> find / -name "*.log" | grep error | sort
^C
gosh> # All processes in pipeline interrupted
Implementation Details
The signal handling system in gosh consists of several key components:
Signal Channel
A dedicated goroutine listens for SIGINT signals and processes them appropriately based on the current shell state.
Process Tracking
The shell tracks currently running processes to enable proper signal forwarding and cleanup.
Readline Integration
Special handling for readline interrupts ensures the shell doesn't exit when Ctrl-C is pressed while idle.
Testing Signal Handling
To verify that signal handling works correctly in your gosh installation:
Start gosh
./gosh
Test idle Ctrl-C
Press Ctrl-C while at the prompt. The shell should show a new prompt without exiting.
Test command interruption
sleep 10
Press Ctrl-C during execution. The sleep command should be interrupted and you should return to the prompt.
Test pipeline interruption
find /usr -name "*.txt" | head -100
Press Ctrl-C during execution. Both find and head processes should be interrupted.
Exit Methods
Since Ctrl-C doesn't exit the shell, you can exit gosh using these methods:
exit command
gosh> exit
EOF (Ctrl-D)
Press Ctrl-D to send an EOF signal and exit the shell gracefully.
Benefits
- Safety: Prevents accidental shell termination
- Productivity: Allows quick command interruption without losing session
- Consistency: Matches behavior of professional shells like bash and zsh
- Reliability: Robust handling of edge cases and pipeline scenarios