golang-context

Propagate Go context.Context cancellation and deadlines across HTTP handlers and services.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Prevents Go context bugs that break cancellation/timeouts, leak resources, or lose request-scoped metadata when building HTTP handlers, service layers, database calls, and outbound requests.

Core Features & Use Cases

  • Correct context propagation across handler → service → DB → external calls, ensuring cancellation and deadlines flow consistently through the entire call chain.
  • Resource-safe derived contexts using context.WithCancel/WithTimeout/WithDeadline with guaranteed cancel() calls on all scoped paths to avoid leaks.
  • Request-scoped metadata best practices for context values, including safe typed keys, and choosing context values only for request metadata (not business parameters or infrastructure dependencies).
  • Background work with context values preserved using context.WithoutCancel (Go 1.21+) so audit/log tasks outlive the request while keeping trace/correlation values.
  • Deep dives and references for HTTP/service calls, cancellation and timeouts, and context value tracing to support consistent implementation and debugging.

Quick Start

Use the golang-context skill to refactor your Go request pipeline so every downstream operation receives the correct ctx, including cancellable DB queries and context-aware outbound HTTP requests.

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 cancellation and deadlines across HTTP handlers and database calls?

Propagate Go context cancellation and deadlines by passing context.Context as the first parameter through every handler, service, database query, and outbound HTTP request in the call chain. This ensures timeouts and cancellation signals flow consistently across all layers without leaking resources.

Why does my Go context leak resources when using context.WithTimeout or WithCancel?

Go context leaks happen when derived contexts from WithTimeout or WithCancel do not call the returned cancel function on all code paths. You must guarantee cancel() is called via defer to prevent goroutines and resources from outliving their intended scope.

Can I use context.WithoutCancel to run background work after an HTTP request finishes in Go 1.21?

Yes, context.WithoutCancel in Go 1.21+ detaches a context from the parent request cancellation while preserving request-scoped values. This allows background audit or logging tasks to outlive the HTTP request without losing trace and correlation metadata.

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

Store request-scoped values in a Go context using unexported typed key types. Only use context values for request metadata like trace IDs, never for business parameters or infrastructure dependencies, to maintain safe and idiomatic layer boundaries.

When should I avoid using context.Background() inside a Go request pipeline?

Avoid using context.Background() in the middle of a Go request flow because it breaks cancellation and deadline propagation. Use it only at the top level of main or tests, ensuring downstream operations inherit the correct request-scoped context.

Does Go context propagation work with database transactions and outbound HTTP clients?

Yes, Go context propagation works with database transactions and outbound HTTP clients by using context-aware variants. Passing the context to DB queries and HTTP requests ensures they are automatically cancelled or timed out when the parent context deadline expires.