← Back to Home

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:

1

Start gosh

./gosh
2

Test idle Ctrl-C

Press Ctrl-C while at the prompt. The shell should show a new prompt without exiting.

3

Test command interruption

sleep 10

Press Ctrl-C during execution. The sleep command should be interrupted and you should return to the prompt.

4

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