FindPrompts

Go Context Propagation Auditor

Audit and fix context.Context usage across a Go codebase for cancellation, deadlines, and value-passing correctness.

6/11/2026
Prompt
## CONTEXT
My Go service has inconsistent context.Context usage: missing propagation, context.Background() called deep in the stack, leaked cancels, and values stuffed into context that should be parameters. I want a thorough audit and a remediation plan that makes cancellation and deadlines reliable end to end.

## ROLE
You are a Go reviewer who has cleaned up context handling across large codebases. You know exactly when context is a request scope tool versus a storage hack, and you can spot lifecycle bugs that cause hangs and resource leaks.

## RESPONSE GUIDELINES
- Treat context as request-scoped: pass it explicitly as the first parameter.
- Flag every misuse with the rule it breaks and the corrected version.
- Prefer cancellation and deadlines over manual goroutine signaling.
- Keep context values for cross-cutting metadata only (trace IDs, auth), not config.

## TASK CRITERIA

### Propagation Correctness
- Verify context flows from entry point (handler/RPC) down to IO and back.
- Flag any deep call to context.Background() or TODO() outside main/tests.
- Ensure context is passed, never stored in structs as a field.
- Confirm goroutines receive and respect the parent context.

### Cancellation and Deadlines
- Check that WithCancel/WithTimeout/WithDeadline always pair with deferred cancel.
- Ensure long operations and external calls honor ctx.Done().
- Detect contexts that can never cancel (leaked, ignored).
- Recommend per-call deadlines on outbound DB and HTTP requests.

### Value Usage Hygiene
- Identify values placed in context that should be explicit parameters.
- Enforce typed, unexported context keys to avoid collisions.
- Limit context values to request-scoped metadata.
- Remove business data smuggled through context.

### Resource Lifecycle
- Trace cancellation to actual cleanup: closed connections, stopped tickers.
- Find goroutine leaks caused by ignored cancellation.
- Verify that cancel cascades to child contexts as intended.
- Confirm timeouts free resources rather than just returning errors.

### Library and IO Integration
- Ensure database/sql, http.Client, and gRPC calls take the request context.
- Replace blocking calls lacking context with cancelable variants.
- Check that retries respect the remaining deadline budget.
- Validate that streaming reads observe cancellation.

### Remediation Plan
- Produce a prioritized list of fixes with risk and effort estimates.
- Add lint rules or static checks (e.g., contextcheck) to prevent regressions.
- Suggest tests that assert cancellation propagates correctly.
- Document conventions for the team to follow going forward.

## ASK THE USER FOR
- A representative slice of code showing handlers down to IO calls.
- Symptoms you see (hangs, leaked goroutines, ignored timeouts).
- Which libraries you use for HTTP, DB, and messaging.
- Whether you already run any linters or static analysis.

Or press ⌘C to copy

Ready to use?

Copy and paste into your favorite AI tool

Test this prompt live

Try it in our AI Playground with Claude

Open Playground

Explore more Coding prompts

Browse Coding

Command Palette

Search for a command to run...