golang-context

Review Go code for correct context.Context propagation and cancellation.

4|Updated May 17, 2026
One-click install
npx skills add https://github.com/hellopoisonx/aim --skill golang-context-hellopoisonx
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: golang-context
Source: https://github.com/hellopoisonx/aim/tree/main/skills/golang-context
Command: npx skills add https://github.com/hellopoisonx/aim --skill golang-context-hellopoisonx

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Prevents broken cancellation, deadline, and tracing flows by enforcing correct and idiomatic use of Go context.Context across the full call chain.

Core Features & Use Cases

  • End-to-end context propagation: ensures the same ctx flows from entry points through services, database calls, and outbound HTTP/gRPC requests.
  • Cancellation correctness: covers WithCancel, WithTimeout, WithDeadline, ctx.Done listening, and required cancel() cleanup to avoid leaks.
  • Safe request-scoped values: uses unexported key types and limits context values to metadata like trace_id/user_id/correlation IDs.
  • Background work without canceling: applies context.WithoutCancel for goroutines that must outlive the request while preserving values.

Quick Start

Use the golang-context skill to review a Go handler and its downstream service/database/HTTP calls for context propagation, cancellation leaks, context value key safety, and correct use of context.TODO/background/WithoutCancel.

Frequently Asked Questions about golang-context

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I propagate Go context across HTTP handlers and service layers safely?

To propagate Go context safely, pass context.Context as the first parameter through API boundaries and use context-aware APIs like Query or HTTP WithContext to maintain cancellation and deadlines across service layers.

Why does my Go context cancellation leak goroutines in downstream HTTP calls?

Go context cancellation leaks occur when derived contexts from WithCancel or WithTimeout are not cleaned up. You must call the returned cancel function using defer to prevent goroutine leaks when downstream HTTP calls finish or time out.

When should I use context.WithoutCancel for background work in Go?

Use context.WithoutCancel in Go when spawning goroutines that must outlive the parent request while still preserving request-scoped values like trace_id, ensuring background work continues safely without premature cancellation.

What is the safe way to store request-scoped values in Go context?

The safe way to store request-scoped values in Go context is by using unexported key types and limiting stored values to metadata like trace_id or correlation IDs, preventing key collisions across API boundaries.

How do I handle timeouts and deadlines in Go database operations?

Handle timeouts and deadlines in Go database operations by propagating context.Context to context-aware database APIs like Exec or Query, ensuring query execution respects request lifetimes and cancels cleanly upon timeout.

Can I use context.Background instead of context.TODO in Go server handlers?

Use context.Background in Go server handlers only for top-level initialization, while context.TODO is reserved for uncertain propagation paths; handlers should derive contexts from incoming requests to maintain cancellation flows.