← Back to all chapters

Chapter 21Down the Rabbit Hole: Why Go Never Re-scans Your Stacks

7 min read
rabbit-holegogarbage-collectionruntimes

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.
rootsABCDblackscannedgreyqueuedwhitenot seen yetwhite and unreachable: freed
The three colors on a tiny heap, mid-mark. Illustrative.

An object is wrongly freed only if both happen during marking:

  1. The program stores a pointer to a white object into a black object, which the collector never revisits.
  2. 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.

BarrierShadesBreaks
Dijkstra insertion (1978)the new pointer on *slot = ptrcondition 1: black never gains a white pointer
Yuasa deletion (1990)the old pointer being overwrittencondition 2: anything reachable at mark start stays reachable or shaded
Go 1.5 to 1.7concurrent markSTW re-scan10s to 100s of msGo 1.8 and laterconcurrent markshort termination pausetime
One GC cycle, before and after Go 1.8. Schematic: bar widths are not measured durations.

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 = ptr

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

1/4Start
stack (black, empty)
heap:   B ──f──▶ C(white)
Race S1: a black (already scanned) stack unlinks the only heap path

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:

RacenoneYuasaDijkstrahybrid
S1: black stack loads C, then unlinksC lostokC lostok
S2: grey stack stores its only CC lostC lostokok

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.

Which race each barrier survives (from the simulator table)

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 real barrier, per heap pointer store. Click a box.
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 = ptr

Watching 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.4ms
  • 1,000 goroutines62 us
  • 100,000 goroutines133 us
  • 500,000 goroutines104 us
Worst STW pause by goroutine count (idle goroutines, small heap)

Worst 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 unsafe arithmetic or from C skip the barrier, so an object can stay white and be freed mid-mark. Maintainers attributed several crashes in gcWriteBarrier and wbBufFlush to 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#

© 2026 Bharat Kaurav. Built with Next.js & Tailwind.

>_ bk@netherwatcheresc to close

Welcome, reader. Type help to see what this shell can do.