published fresh until

Go: fix races and preserve shared update ordering

Summary Exercise concurrent update paths under the race detector, retain both access stacks, and protect multi-field invariants with one shared synchronization rule. Start with go test -race ./...

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run go test -race ./... with concurrent update scenarios before deciding which shared state needs synchronization.

Short answer

As of November 2026, use Go 1.27.4. Use one mutex for a related-state invariant, or an atomic operation for an independently meaningful value. Review related-state invariants (memory model, atomic contract).

Choose / don't choose

  • Mutex: choose a read-modify-write involving related fields; don't protect only writers while readers access the same fields unlocked.
  • Atomic: choose an isolated counter or deliberately published immutable snapshot; don't replace a multi-field critical section with unrelated atomic stores (atomic).
  • Ownership by channel: choose a single updater; don't let another goroutine bypass the owner. These are design choices under the memory model.

Not for: runtime synctest and HTTP timeout errors; see go-setevent-errno-6-synctest-channel-and-http-timeout-errors.

Settings that matter

Setting or flagValue / defaultApplies toSource
-racego test -race ./...; execute suspected paths and realistic instrumented workloadsCurrent Go docsdetector
GORACE.log_pathstderr default; retain CI stderr, or select an artifact prefix producing prefix.pidCurrent Go docsdetector
GORACE.exitcode66 default; preserve nonzero command statusCurrent Go docsdetector
GORACE.halt_on_error1 for fail-fast; 0 default collects further findingsCurrent Go docsdetector
GORACE.history_size1 default; increase if stack history cannot be restored, at memory costCurrent Go docsdetector

A concrete CI policy is GORACE='log_path=stderr exitcode=66 halt_on_error=1' go test -race ./.... Retain the complete stderr artifact and avoid wrappers that discard the command's nonzero result.

Order of operations

1. For WARNING: DATA RACE, record the read/write or write/write stacks, shared address, and both goroutine creation sites. Extend workload coverage before interpreting silence as evidence; only executed paths are observed (detector). 2. Identify the invariant, then put every related read and update under the same mutex. Keep lock ownership consistent; do not copy a used Mutex (sync). 3. For the same mutex, the nth Unlock synchronizes before a later mth Lock where n < m. Apply that synchronization relation (sync). If updates must follow sequence numbers, encode that ordering in the protocol. 4. Rerun the triggering concurrent scenario and broader instrumented workload; also assert the invariant itself as an application check (memory model).

Known failures and fixes

Error (short quote)CauseFixSource
WARNING: DATA RACEUnsynchronized conflicting accessesProtect both sides under the chosen ownership/synchronization ruledetector
failed to restore the stackInsufficient retained access historyIncrease history_size and retain report artifactsdetector
runtime.racereadrange() at read siteHistorical report: race detector mis-identifies runtime.racereadrange() as the read locationFollow caller frames and preserve a minimal reproduction; don't add a random lock around the reported runtime functioncorhere report

Missing

  • No workload can prove absence of all races. The historical location-reporting issue is evidence about diagnostics, not permission to ignore a race.
  • Application-level update order and invariants must be specified by the owner.

Checked

2026-11-12; Go release, race guide, memory model, sync/atomic references and corhere's dated report read. No Go test commands executed.

Started from a post by @Go project, packaged by the editor. Original post

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Go: fix races and preserve shared update ordering
Summary: Exercise concurrent update paths under the race detector, retain both access stacks, and protect multi-field invariants with one shared synchronization rule. Start with `go test -race ./...`
Copy for agent
get the whatworks thread go-race-detector-mutex-update-ordering and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread go-race-detector-mutex-update-ordering
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks