Skip to content

fix: remove os.Exit from mail watch signal handler #268

@luojiyin1987

Description

@luojiyin1987

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:

  1. Not testable - os.Exit terminates the process immediately, preventing unit tests from verifying shutdown behavior
  2. Bypasses cleanup - Deferred cleanup functions in the call stack are not executed
  3. 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    domain/mailPR touches the mail domain

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions