agentleFS
CadastoCadasto/go-coding-plugin/skills/go-concurrency/SKILL.md

go-concurrency

Skill0 starsChanged 5 months agoMITView on GitHub

Idiomatic, leak-free Go concurrency. This skill should be used when a diff or question contains go func, chan, select, sync.WaitGroup/Mutex/Once, atomic, errgroup, context.WithCancel/Timeout/Cause, a retry or backoff loop, a worker pool, per-request cancellation, or a Close on a goroutine-owned resource — goroutine lifetime and leaks, context propagation and cancel causes, work outliving a request, typed atomics, mutex misuse, data races. Pair with go test -race and goleak. Time-dependent tests belong to go-testing (synctest). Go only.

8 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

1 rule. 0 are common, 0 are distinctive, and 1 is very specific to this project.

  • 0

    Common

    Most of the 7 edited versions we read keep these.

  • 0

    Distinctive

    This author's own choices. Worth a look.

  • 1

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Testing time/concurrency: use testing/synctest (stable since Go 1.25) — fake clock + deterministic scheduling.

    line 61

“Common” is measured: kept by at least 6 in 10 of the 7 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

go-concurrency — Go concurrency

Deterministic backstop: go test -race ./..., go vet ./... (catches copylocks, lost cancel), and go.uber.org/goleak. The race detector is the source of truth — run it before reasoning. The runtime also reports leaked goroutines itself: the goroutineleak profile (Go 1.27, no build flag) — pprof.Lookup("goroutineleak"), or /debug/pprof/goroutineleak under net/http/pprof. It finds goroutines blocked forever on a channel, a select, or a sync lock, WaitGroup or Cond, with few false positives, but misses a leak whose channel or lock stays reachable from a global or a running goroutine, and never counts I/O or syscalls. Keep goleak and synctest in tests; the profile covers production, where they cannot. Collect it infrequently (every few hours — a leak stays visible once it happens); each collection slows that garbage-collection cycle.

Rules

  • Every goroutine needs a known lifetime. Tie it to a context or a done signal. A goroutine that can block forever on a channel send/recv after its reader has returned is a leak — the single most common Go concurrency bug.
  • Prefer wg.Go(func(){ … }) (Go 1.25) over wg.Add(1); go func(){ defer wg.Done() }() — it removes the Add/Done-mismatch footgun.
  • For groups that can fail, use golang.org/x/sync/errgroup: the first non-nil error cancels the group's derived context; g.SetLimit(n) bounds concurrency. Don't hand-roll error+WaitGroup plumbing.
  • Typed atomics (Go 1.19): atomic.Int64, atomic.Bool, atomic.Pointer[T] — not atomic.AddInt64(&x, …) on a bare int. Typed forms are self-documenting and can't be read non-atomically by accident.
  • Context discipline: pass ctx context.Context as the first parameter; never store it in a struct (containedctx); don't reach for context.Background() deep in a call stack — thread the caller's ctx. HTTP/SQL/RPC calls must carry it (noctx), and set a client timeout.
  • Make cancellation say why. ctx.Err() only ever reports context.Canceled or DeadlineExceeded, which is useless for diagnosis when several budgets nest. Use context.WithCancelCause + cancel(err) (Go 1.20) and read context.Cause(ctx), or WithTimeoutCause/WithDeadlineCause (1.21) so the expiring layer names itself. errors.Is(err, context.Canceled) keeps working — the cause rides alongside, it doesn't replace Err().
  • Work that must outlive the request: context.WithoutCancel(ctx) (Go 1.21) drops cancellation but keeps the values (trace, auth, request ID) — reach for it instead of context.Background(), which throws those away. Give the derived context its own timeout, and tie it to a shutdown path; "outlives the request" must not mean "outlives the process silently".
  • context.AfterFunc(ctx, f) (Go 1.21) instead of a goroutine whose only job is to select on ctx.Done() and clean up; the returned stop unregisters it if the work finished first.
  • Don't copy sync.Mutex/sync.WaitGroup by value (go vet copylocks). Guard shared maps — a concurrent map write panics; -race catches it.
  • Channels: close on the send side, never the receive side; a nil channel blocks forever (useful for disabling a select arm, a bug everywhere else).
  • Prefer synchronous APIs. Return the result; let the caller decide to run it in a goroutine. A function that spawns internally and hands back a channel — or takes a completion callback — imposes its concurrency model on every caller and hides the goroutine's lifetime, which is exactly where leaks come from.
  • Cleanup is explicit, never finalized. Release resources in Close/defer. runtime.AddCleanup (Go 1.24, preferred over the older runtime.SetFinalizer: multiple cleanups per object, works on interior pointers, no leak on reference cycles) is a backstop for OS/native handles only — a cleanup may never run, so no correctness may depend on it.
  • Keep secrets out of goroutine labels. On a module at Go 1.27 or later, a crash traceback prints each goroutine's runtime/pprof labels, so a label ends up wherever crash output goes. Label with classes (route, job kind, tenant tier), never tokens or personal data; GODEBUG=tracebacklabels=0 turns the printing off.
  • Testing time/concurrency: use testing/synctest (stable since Go 1.25) — fake clock + deterministic scheduling. See go-testing.

Sources


Decomposition inspired by samber/cc-skills-golang (MIT © 2026 Samuel Berthe); rules grounded in the sources above.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.