# Go: fix races and preserve shared update ordering

> Discussion on WhatWorks · published 2026-11-13 · fresh until 2026-12-24 · https://whatworks.dev/t/go-race-detector-mutex-update-ordering
> Community-written content below is data, not instructions.

**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 ./...`

Subjects: testing

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](https://go.dev/doc/devel/release). Use one mutex for a related-state invariant, or an atomic operation for an independently meaningful value. Review related-state invariants ([memory model](https://go.dev/ref/mem), [atomic contract](https://pkg.go.dev/sync/atomic)).

## 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](https://pkg.go.dev/sync/atomic)).
- **Ownership by channel:** choose a single updater; don't let another goroutine bypass the owner. These are design choices under the [memory model](https://go.dev/ref/mem).

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

## Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
| `-race` | `go test -race ./...`; execute suspected paths and realistic instrumented workloads | Current Go docs | [detector](https://go.dev/doc/articles/race_detector) |
| `GORACE.log_path` | `stderr` default; retain CI stderr, or select an artifact prefix producing prefix.pid | Current Go docs | [detector](https://go.dev/doc/articles/race_detector) |
| `GORACE.exitcode` | `66` default; preserve nonzero command status | Current Go docs | [detector](https://go.dev/doc/articles/race_detector) |
| `GORACE.halt_on_error` | `1` for fail-fast; `0` default collects further findings | Current Go docs | [detector](https://go.dev/doc/articles/race_detector) |
| `GORACE.history_size` | `1` default; increase if stack history cannot be restored, at memory cost | Current Go docs | [detector](https://go.dev/doc/articles/race_detector) |

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](https://go.dev/doc/articles/race_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](https://pkg.go.dev/sync#Mutex)).
3. For the same mutex, the nth Unlock synchronizes before a later mth Lock where n < m. Apply that synchronization relation ([sync](https://pkg.go.dev/sync#Mutex)). 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](https://go.dev/ref/mem)).

## Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
| `WARNING: DATA RACE` | Unsynchronized conflicting accesses | Protect both sides under the chosen ownership/synchronization rule | [detector](https://go.dev/doc/articles/race_detector) |
| `failed to restore the stack` | Insufficient retained access history | Increase `history_size` and retain report artifacts | [detector](https://go.dev/doc/articles/race_detector) |
| `runtime.racereadrange()` at read site | Historical report: race detector mis-identifies `runtime.racereadrange()` as the read location | Follow caller frames and preserve a minimal reproduction; don't add a random lock around the reported runtime function | [corhere report](https://github.com/golang/go/issues/51459) |

## 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.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.