Audit Git Diffs for Race Conditions and Shared Mutable State

Reviews code diffs for silent concurrency bugs, mapping concurrent execution timelines and providing exact thread-safe mitigation fixes.

Agent prompt for ChatGPT and Claude

You get: Summary, findings Table and hardened Code Snippets

Prompt

Prompt
Act as a principal systems engineer specialized in concurrency and multithreaded debugging. Your task is to audit the git diff I will paste below to detect silent race conditions, unsafe shared state, non-atomic read-modify-writes, and deadlocks.

Rules:
1. Focus strictly on lines introduced or modified in the diff, plus immediate surrounding context.
2. Ignore general style or cosmetic issues; highlight only hazards where timing, reordering, or concurrent execution can corrupt data or panic.
3. For every discovered issue, trace a concrete execution timeline with at least two threads or concurrent tasks.
4. Provide the exact thread-safe patch using language-standard primitives (such as mutexes, atomic types, or immutable structures).

Output format:
- Summary: A 2-sentence overview of overall concurrency safety in this diff.
- Findings Table: A Markdown table with four columns: Location (file and line), Shared State / Hazard, Concurrent Timeline (Thread A vs Thread B sequence), and Recommended Mitigation.
- Hardened Code Snippets: For each finding, display the exact corrected code block.
- Assumptions and Blind Spots: Note what runtime model, runtime scheduler settings, or external database guarantees this analysis assumes.

Paste your git diff below this prompt to begin.

Example output

Example for: a Go backend diff updating an in-memory session rate limiter.

Summary

The diff introduces shared mutable state via an unsynchronized map write inside an HTTP handler. Under concurrent traffic, this causes non-deterministic map panics and inaccurate token counts.

Findings Table
LocationShared State / HazardConcurrent TimelineRecommended Mitigation
limiter.go:42tokens map access without lock protectionThread 1 checks key existence; Thread 2 writes a new key; Thread 1 simultaneously writes, causing fatal concurrent map read/write runtime panic.Replace raw map[string]int with sync.Map or guard both read and write blocks using a dedicated sync.RWMutex.
limiter.go:58Check-then-act balance decrementThread 1 reads remaining = 1; Thread 2 reads remaining = 1; both subtract 1 and proceed; client sends two requests when only one was allowed.Combine check and subtraction into an atomic transaction under a write lock, or use atomic.Int64.
Hardened Code Snippets
type Limiter struct {
	mu     sync.Mutex
	tokens map[string]int
}

func (l *Limiter) Allow(apiKey string) bool {
	l.mu.Lock()
	defer l.mu.Unlock()

	count := l.tokens[apiKey]
	if count <= 0 {
		return false
	}
	l.tokens[apiKey] = count - 1
	return true
}
Assumptions and Blind Spots

Assumes standard Go runtime scheduler on a single OS process; does not account for multi-instance distributed state across multiple container replicas, which would require Redis or Postgres locking instead.

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Forces a step-by-step concurrent execution timeline, preventing vague warnings about thread safety.
  • Restricts review strictly to shared mutable state, eliminating off-topic style suggestions.
  • Demands a four-column structured comparison linking exact file locations to thread-safe remediations.

Paste your git diff or pull request patch directly under the prompt in GitHub Copilot Chat, ChatGPT, or Claude to uncover subtle multithreading flaws.

Common edits

  • Specify your target concurrency runtime, such as Tokio in Rust, asyncio in Python, or virtual threads in Java.
  • Add a constraint requiring distributed lock patterns (like Redis Redlock) instead of in-memory primitives.

Reuse it: Use in a workflowAdapt in the builder

Prompt details

By
Prompts-GPT editors
Published
How prompts are written and checked

Did this prompt work?

Audit a TypeScript API Route for Race Conditions, Auth, and N+1 Bugs

Get a severity-ranked security and performance audit of your TypeScript API route with exact line numbers, concrete exploit or failure scenarios, and minimal targeted fix diffs.

AI Coding AgentsClaude Code

Audit PR Git Diffs for Scope Creep and Churn

Reviews git diff statistics and patch files to immediately flag out-of-scope edits, accidental whitespace formatting churn, and unrequested dependency updates.

AI Coding AgentsClaude Code

Review AI Pull Requests for Phantom Imports and Bloat

Audits pull request diffs generated by coding agents to flag fake dependencies, single-call wrappers, and premature abstractions.

AI Coding AgentsClaude Code

Verify the Coding Agent's 'Done' Claim Against the Diff and Test Output

Checks each claim in a coding agent's final summary against the actual diff and test runs, marks it verified, unverified or contradicted with evidence, and lists unmentioned changes, skipped tests and stubs.

Conventional Commit and PR Description from a Diff and Ticket

Produces a Conventional Commits message and a reviewer-ready PR description covering what changed, why, how to test it, and the risks, based on your diff and ticket.

Audit a Pull Request Diff for Breaking API Changes

Separates wire-breaking schema, status code, and migration changes from safe additions, returning a consumer impact table and deprecation plan.