Reliability Go active any

Context flows per call and detached work gets its own lifetime

rel-go-001

Intent

Keep Go request-scoped cancellation and deadlines attached to the call chain without leaking them into reusable objects or detached work.

Applicability

Applies to Go request handlers, I/O paths, and background work spawned from request code.

What to inspect

Function signatures, stored contexts, derived contexts, background goroutines, and cancel calls.

Pass criteria

Request-scoped work accepts and forwards context.Context per call, derived contexts are canceled, and detached work uses its own deliberate lifetime instead of the request context.

Fail criteria

The diff drops caller context, stores it in reusable structs, forgets to cancel derived contexts, or uses request context for detached background work.

Do not flag

Purely synchronous helpers with no blocking or long-lived work.

Confidence guidance

HIGH when the bad context flow is directly visible. MEDIUM when downstream propagation is partly hidden. LOW when the boundary is incomplete.

Remediation

Pass context per call, defer cancel() on derived contexts, and create a new owned context for detached work.

Pass example

ctx, cancel := context.WithTimeout(parent, 5*time.Second)
defer cancel()

Fail example

type Client struct { ctx context.Context }

Sources

  • 100 Go Mistakes and How to Avoid Them book
  • Concurrency in Go book
  • Effective Go + Go Code Review Comments standard
  • Go official blog: Contexts and structs article
  • Google Go Style Guide standard