Skip to content

Add graceful shutdown handling for long-running operations #57

@dapi

Description

@dapi

Problem

The --scan command iterates through the entire port range (default 1000 ports) and can take significant time. If interrupted (Ctrl+C), there is no signal handling:

  • Partial data may be written
  • No cleanup performed
  • User gets no feedback about interruption

Current Behavior

$ port-selector --scan
# User presses Ctrl+C during scan
# Process terminates immediately
# Allocations file may be in inconsistent state

Proposed Solution

Add signal handling for SIGINT and SIGTERM:

func runScan(...) error {
    ctx, cancel := context.WithCancel(context.Background())
    
    sigChan := make(chan os.Signal, 1)
    signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
    
    go func() {
        <-sigChan
        fmt.Fprintln(os.Stderr, "\nInterrupted, saving partial results...")
        cancel()
    }()
    
    // Use ctx in scan loop
    for port := start; port <= end; port++ {
        select {
        case <-ctx.Done():
            return savePartialResults()
        default:
            // scan port
        }
    }
}

Acceptance Criteria

  • Handle SIGINT/SIGTERM in --scan
  • Save partial results on interruption
  • Print message about interruption
  • Consider applying to other long-running operations

Priority

Low - edge case, but improves robustness

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions