スキル一覧に戻る
JamesPrial

go-goroutine-leaks

by JamesPrial

WIP - collection of various Claude stuff i make/use/have_Claude_hallucinate

2🍴 0📅 2026年1月23日
GitHubで見るManusで実行

SKILL.md


name: go-goroutine-leaks description: Prevent goroutine leaks with proper shutdown mechanisms

Goroutine Leak Prevention

Pattern

Every goroutine must have a way to exit. Use channels or context for shutdown signals.

CORRECT - Done channel

type Worker struct {
    done chan struct{}
}

func (w *Worker) Start() {
    w.done = make(chan struct{})
    go func() {
        for {
            select {
            case <-w.done:
                return
            case <-time.After(1 * time.Second):
                // do work
            }
        }
    }()
}

func (w *Worker) Stop() {
    close(w.done)
}

CORRECT - Context

func StartWorker(ctx context.Context) {
    go func() {
        ticker := time.NewTicker(1 * time.Second)
        defer ticker.Stop()

        for {
            select {
            case <-ctx.Done():
                return
            case <-ticker.C:
                // do work
            }
        }
    }()
}

WRONG - No exit mechanism

func StartWorker() {
    go func() {
        for {
            // Runs forever - goroutine leak!
            time.Sleep(1 * time.Second)
            // do work
        }
    }()
}

WRONG - Unbuffered channel send can block

func GetData() string {
    ch := make(chan string)
    go func() {
        ch <- fetchData() // Blocks forever if nobody reads
    }()

    // If timeout happens, goroutine leaks
    select {
    case result := <-ch:
        return result
    case <-time.After(1 * time.Second):
        return "timeout"
    }
}

Fix with buffered channel

func GetData() string {
    ch := make(chan string, 1) // Buffer size 1
    go func() {
        ch <- fetchData() // Won't block
    }()

    select {
    case result := <-ch:
        return result
    case <-time.After(1 * time.Second):
        return "timeout"
    }
}

Rules

  1. Every go func() needs an exit condition
  2. Use select with ctx.Done() or done channel
  3. Buffered channels (size 1) for single sends
  4. Close channels to signal completion
  5. Test with runtime.NumGoroutine() to detect leaks

Detection

func TestNoLeaks(t *testing.T) {
    before := runtime.NumGoroutine()

    worker := NewWorker()
    worker.Start()
    worker.Stop()

    time.Sleep(100 * time.Millisecond) // Allow cleanup
    after := runtime.NumGoroutine()

    if after > before {
        t.Errorf("goroutine leak: before=%d after=%d", before, after)
    }
}

スコア

総合スコア

65/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

+5

レビュー

💬

レビュー機能は近日公開予定です