Problem
The mail watch command uses os.Exit(0) in its signal handler to terminate the program when receiving Ctrl+C. This approach has several drawbacks:
- Not testable -
os.Exit terminates the process immediately, preventing unit tests from verifying shutdown behavior
- Bypasses cleanup - Deferred cleanup functions in the call stack are not executed
- Unidiomatic - CLI tools should use context cancellation for graceful shutdown
Solution
Replace os.Exit(0) with a graceful shutdown mechanism using:
context.WithCancel to propagate cancellation to the WebSocket client
- A
shutdownBySignal channel to coordinate between signal handler and main goroutine
- Proper
defer signal.Stop(sigCh) for resource cleanup
Changes
shortcuts/mail/mail_watch.go: Remove os.Exit(0), add context-based shutdown coordination
Problem
The
mail watchcommand usesos.Exit(0)in its signal handler to terminate the program when receiving Ctrl+C. This approach has several drawbacks:os.Exitterminates the process immediately, preventing unit tests from verifying shutdown behaviorSolution
Replace
os.Exit(0)with a graceful shutdown mechanism using:context.WithCancelto propagate cancellation to the WebSocket clientshutdownBySignalchannel to coordinate between signal handler and main goroutinedefer signal.Stop(sigCh)for resource cleanupChanges
shortcuts/mail/mail_watch.go: Removeos.Exit(0), add context-based shutdown coordination