s5cmd icon indicating copy to clipboard operation
s5cmd copied to clipboard

fix(main): exit with code 130 on SIGINT (#615)

Open gustcol opened this issue 7 months ago • 1 comments

Context

Fixes #615

Root cause

Different commands returned different exit codes on Ctrl-C:

  • ls returned 0 — the context-cancel path swallowed the error
  • cp returned 1 — its context-cancel path propagated an error
  • Neither returned 130, the POSIX standard (128 + signal 2)

This made it impossible to reliably detect Ctrl-C in scripts vs real failures.

Fix

Register a dedicated os.Interrupt notification channel (intCh) in main(), alongside the existing signal.NotifyContext. After command.Main returns, a non-blocking receive on intCh tells us whether SIGINT was the cause. If yes, we call os.Exit(130) before evaluating the command's own error return.

intCh := make(chan os.Signal, 1)
signal.Notify(intCh, os.Interrupt)
defer signal.Stop(intCh)

err := command.Main(ctx, os.Args)

select {
case <-intCh:
    os.Exit(130)
default:
}

Files changed: main.go

Tests

  • Added TestSIGINTExitCode in main_test.go: builds the binary, runs a command against an unreachable endpoint so it blocks, sends SIGINT, and asserts exit code 130.

Manual verification

s5cmd ls s3://bucket/ &
sleep 0.2
kill -INT %1
wait %1
echo $?  # 130

Notes

  • Exit code 130 is the POSIX standard for SIGINT-terminated processes.
  • This is technically a breaking change for scripts that checked for exit code 0 or 1 specifically from Ctrl-C interrupts.

gustcol avatar Apr 12 '26 13:04 gustcol

Test results

Unit tests (go test -race ./...): PASS (after hardening the test)

Changes included beyond the original fix:

  • main_test.go: Added --no-sign-request and AWS_DEFAULT_REGION=us-east-1 to skip credential/region discovery so the binary blocks immediately on the TCP dial; increased pre-signal sleep to 3 s to absorb build-time variance under parallel test load. The 500 ms sleep was insufficient when running alongside the full ./... suite.

Specific tests (run 3 times to confirm stable):

go test -race -count=1 ./...
ok  github.com/peak/s5cmd/v2   # includes TestSIGINTExitCode

Wasabi smoke test:

$ ./s5cmd cp /tmp/largefile.bin s3://bucket/large-upload-test &
PID=$!
sleep 2
kill -INT $PID
wait $PID
echo $?
130

SIGINT exits with code 130. SIGTERM or error exit remains 1.

gustcol avatar Apr 12 '26 15:04 gustcol