smithery/JamesPrial

go-context-cancellation

Context cancellation patterns for graceful shutdown

Installation

$ npx skills add smithery/JamesPrial --skill go-context-cancellation

Similar popular skills

Related neighbors and high-traction skills in the same topics — useful to compare before installing.

Also in this package

Other skills from smithery/JamesPrial · top by installs.

npx skills add smithery/JamesPrial

Browse all from smithery/JamesPrial

More details

Agent compatibility

Declared targets from SKILL.md / docs. Unmarked agents are not listed — the skill may still install via the CLI.

Claude Code Not declared
Cursor Not declared
Codex Not declared
GitHub Copilot Not declared
Windsurf Not declared
Gemini CLI Not declared
Cline Not declared
OpenCode Not declared

Package contents

Files included with this skill beyond the listing page.

  • skill md SKILL.md 1,781 B
  • docs SUMMARY.md 82 B

History

  1. First recorded snapshot · 0 installs

SKILL.md

Context Cancellation

Pattern

Pass context.Context as first parameter. Check ctx.Done() in goroutines and long operations.

CORRECT

func ProcessItems(ctx context.Context, items []string) error {
    for _, item := range items {
        select {
        case <-ctx.Done():
            return ctx.Err()
        default:
            if err := processItem(item); err != nil {
                return err
            }
        }
    }
    return nil
}

func main() {
    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()

    if err := ProcessItems(ctx, items); err != nil {
        log.Printf("processing failed: %v", err)
    }
}

WRONG - No context checking

func ProcessItems(items []string) error {
    for _, item := range items {
        // No way to cancel - runs forever if stuck
        if err := processItem(item); err != nil {
            return err
        }
    }
    return nil
}

Rules

  1. Context is first parameter: func Do(ctx context.Context, ...)
  2. Always call cancel() via defer to prevent leaks
  3. Check ctx.Done() in loops and before expensive operations
  4. Propagate context to downstream calls
  5. Use context.WithTimeout or WithDeadline for time limits

Common Patterns

// HTTP server with context
func handler(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context() // Request context
    result, err := fetchData(ctx)
    // ...
}

// Worker with cancellation
func worker(ctx context.Context) {
    for {
        select {
        case <-ctx.Done():
            return
        case work := <-workCh:
            process(work)
        }
    }
}