Chapter 21Down the Rabbit Hole: Why Go Never Re-scans Your Stacks
Go makes a hundred thousand goroutines normal. Until Go 1.7 that had a hidden cost: at the end of every GC cycle the runtime stopped the world and re-scanned every goroutine stack that had run since its last scan. The design doc says that pause could take 10s to 100s of milliseconds. The last unbounded pause scaled with the number Go encourages you to make large.
Go 1.8 fixed it with the hybrid write barrier (Clements and Hudson, 2016), targeting worst-case pauses under 50 microseconds. It's one of the tidiest arguments I've read in a runtime.
Painting while you move the furniture#
Go marks live objects while your program runs, using three colors.
- White: unseen. Still white at the end means freed.
- Grey: seen, pointers not yet followed.
- Black: seen, everything it points to queued.
An object is wrongly freed only if both happen during marking:
- The program stores a pointer to a white object into a black object, which the collector never revisits.
- Every path from a grey object to it is destroyed before the collector follows it.
A write barrier only has to break one condition, and the two classic barriers each pick one.
| Barrier | Shades | Breaks |
|---|---|---|
| Dijkstra insertion (1978) | the new pointer on *slot = ptr | condition 1: black never gains a white pointer |
| Yuasa deletion (1990) | the old pointer being overwritten | condition 2: anything reachable at mark start stays reachable or shaded |
Stacks don't get a barrier#
The compiler emits no barrier for stack writes. Local pointer assignment is about the most common thing a program does, and a load, compare and possible call on each would tax all code to protect a rare race.
Go 1.5 to 1.7 used an insertion barrier. That keeps the heap safe, but a stack can pick up a white pointer silently, so stacks were permanently grey: scanned, then possibly dirty again as soon as the goroutine ran. Marking couldn't finish until every such stack was re-scanned with the world stopped.
The fix adds no stack barrier. It makes the heap barrier smart enough that a scanned stack is safe for the rest of the cycle.
Two shades, two races#
writePointer(slot, ptr):
shade(*slot) // Yuasa: the old value
if current stack is grey:
shade(ptr) // Dijkstra: the new value
*slot = ptrI only got why each half is needed from the dive's simulator, barrier_sim.py, which replays hand-scheduled races under different barriers. Two carry the whole design.
stack (black, empty) heap: B ──f──▶ C(white)
Race S2. The stack is still grey (unscanned) and holds the only pointer to C. It stores C into a black heap object, then drops its local. A deletion barrier shades whatever was in the slot before, not C. When the stack is finally scanned it no longer has C, and the black object is never rescanned: C is lost. The insertion half saves it, but only while the stack is grey.
The simulator's verdict:
| Race | none | Yuasa | Dijkstra | hybrid |
|---|---|---|---|---|
| S1: black stack loads C, then unlinks | C lost | ok | C lost | ok |
| S2: grey stack stores its only C | C lost | C lost | ok | ok |
Shades the new pointer. Survives S2 (grey stack stores its only C), loses C in S1. That's why Go 1.5 to 1.7 had to re-scan stacks with the world stopped.
Shades the old pointer. Survives S1 (black stack unlinks the only heap path), loses C in S2. You would have to scan every stack atomically at the start.
Shades both. Survives S1 and S2, and a scanned stack stays safe for the rest of the cycle, so the re-scan goes away.
Insertion alone fails S1, which is why Go 1.7 needed the rescan. Deletion alone fails S2, so you'd have to scan every stack atomically at the start. The hybrid passes both.
Why a scanned stack stays safe#
Right after its scan, a stack points only to objects the scan shaded. To hide an unseen object it must get a pointer to one, and there are only two ways.
- Load it from the heap. For it to then be unreachable from the heap, a heap slot must be overwritten, and the deletion shade catches that.
- Allocate it. New objects during marking are allocated black, which Go already did.
So once a stack is black the insertion shade adds nothing, and the rescan list could be deleted.
The design doc is explicit that this doesn't keep the strong "no black-to-white pointers" invariant. A black goroutine can store a white pointer into a black heap object unshaded. The weaker invariant kept: every white object a black object points to is still reachable from some grey object through white objects.
What actually ships#
The compiler's output
// compiler, for every heap pointer store *slot = ptr
if runtime.writeBarrier.enabled {
buf := gcWriteBarrier2() // reserve 2 slots in this P's buffer
buf[0] = ptr // new value
buf[1] = *slot // old value
}
*slot = ptrWatching the pause not grow#
The dive's gcdemo.go spins up idle goroutines with some heap pointer churn. On Go 1.27.2, 14 Ps, Apple silicon:
goroutines=1000 GCs=9 max STW pause=62us total STW=0.4ms
goroutines=100000 GCs=9 max STW pause=133us total STW=0.4ms
goroutines=500000 GCs=9 max STW pause=104us total STW=0.4msWorst pause stays around 100 microseconds from a thousand to half a million goroutines. The caveat: goroutines are idle and the heap is small, so this shows no pause scales with goroutine count, not that any service is pause-free.
Where it breaks now#
The barrier isn't the latency risk anymore. Three things are.
- Mark assist. If marking falls behind allocation, allocating goroutines are drafted into scan work:
assistWorkPerByte = scanWorkRemaining / (heapGoal - heapLive). With 40 MB left to scan and 20 MB before the heap goal, each allocated byte costs 2 bytes of scanning. Nobody has credit at cycle start, so early allocators pay. Go issue #16293 has field reports of stalls in the hundreds of milliseconds alongside 1 to 2 ms STW pauses. Tail latency moved from the pause to whoever is allocating. - unsafe and cgo. Pointers written through
unsafearithmetic or from C skip the barrier, so an object can stay white and be freed mid-mark. Maintainers attributed several crashes ingcWriteBarrierandwbBufFlushto this: the barrier is just where the bad pointer is first touched. - Real barrier bugs. In 2019 the wasm port's deletion barrier greyed the wrong object (#30873), so a reachable object could go unmarked: the exact failure this design prevents. Fixed and backported.
There's also a smaller tax: since the barrier shades more than strictly necessary, some objects that die during a cycle survive as floating garbage until the next one.
The question I'm left with#
The barrier has had the same shape since Go 1.8 in 2017, and I found no 2024 to 2026 paper proposing a new one for Go. Active work moved elsewhere: Green Tea, on by default in Go 1.26, marks whole pages instead of single objects for locality, with Google expecting 10 to 40% less GC CPU.
Still open is assist. Start marking earlier and the barrier burns more CPU for longer. Make assists harder and latency moves into request paths. Grow the heap and you pay in memory. The pacer meta-issue (#42430) has been open for years.
Underneath: Go is non-moving and non-generational, which is what lets it get away with only a marking barrier. A generational collector would need a remembered-set barrier on top. Whether pointer-heavy Go heaps would repay that, I couldn't find a settled answer, so that's probably the next hole.
Sources#
- Clements and Hudson. Proposal 17503: Eliminate STW stack re-scanning (2016)
- Hudson. Go GC: Prioritizing low latency and simplicity (2015)
- A Guide to the Go Garbage Collector
- Go runtime source: mbarrier.go, mwbbuf.go, mgcmark.go, mgcpacer.go
- Go issues: #22460 (buffered barrier), #16293 (assist latency), #42430 (pacer), #30873 (wasm barrier bug), #42249
- Green Tea GC and Go 1.26 release notes