runtime: windows/amd64 stack corruption when LockOSThread wraps registry calls (new in Go 1.27)
Go version
go version go1.27.1 windows/amd64
Output of go env in your module/workspace:
set AR=ar
set CC=gcc
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_ENABLED=0
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set CXX=g++
set GCCGO=gccgo
set GO111MODULE=
set GOAMD64=v1
set GOARCH=amd64
set GOAUTH=netrc
set GOBIN=C:\Users\<user>\go\bin
set GOCACHE=C:\Users\<user>\AppData\Local\go-build
set GOCACHEPROG=
set GODEBUG=
set GOENV=C:\Users\<user>\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFIPS140=off
set GOFLAGS=
set GOGCCFLAGS=-m64 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\<user>\AppData\Local\Temp\go-build4072696019=/tmp/go-build -gno-record-gcc-switches
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMOD=C:\Users\<user>\code\Go\go-issue-proof\go.mod
set GOMODCACHE=C:\Users\<user>\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPACKAGESDRIVER=
set GOPATH=C:\Users\<user>\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\<user>\AppData\Roaming\go\telemetry
set GOTMPDIR=
set GOTOOLCHAIN=go1.27.1
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.27.1
set GOWORK=
set PKG_CONFIG=pkg-configWhat did you do?
Windows 11 Pro 10.0.26100, Intel Core i7-13700 (16 cores, 24 threads, no AMX). CGO_ENABLED=0.
A TUI application (Bubble Tea) crashed at startup with fatal error: fault inside runtime.selectgo. At the time, one goroutine was enumerating HKLM\...\Uninstall through golang.org/x/sys/windows/registry, and Key.ReadSubKeyNames calls runtime.LockOSThread. I reduced it to the program below. It uses only the standard library.
// Repro: stack corruption on windows/amd64 with Go 1.27.x.
//
// Worker goroutines call runtime.LockOSThread, make Windows system calls,
// and unlock, while 16 other goroutines send on a channel and allocate.
//
// LOCK=1 (default) lock the OS thread around the system calls
// LOCK=0 same work without LockOSThread
// WORK=registry enumerate and open subkeys of HKLM/HKCU Uninstall (default)
// WORK=syscall call GetCurrentProcessId repeatedly instead
package main
import (
"fmt"
"os"
"runtime"
"strings"
"sync/atomic"
"syscall"
"time"
)
var (
lock = os.Getenv("LOCK") != "0"
work = os.Getenv("WORK")
)
func openKey(parent syscall.Handle, path string, access uint32) (syscall.Handle, bool) {
p, err := syscall.UTF16PtrFromString(path)
if err != nil {
return 0, false
}
var k syscall.Handle
return k, syscall.RegOpenKeyEx(parent, p, 0, access, &k) == nil
}
func subkeyNames(k syscall.Handle) []string {
if lock {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
}
var names []string
buf := make([]uint16, 256)
for i := uint32(0); ; i++ {
n := uint32(len(buf))
if syscall.RegEnumKeyEx(k, i, &buf[0], &n, nil, nil, nil, nil) != nil {
return names
}
names = append(names, syscall.UTF16ToString(buf[:n]))
}
}
func scanRegistry() {
for _, hive := range []syscall.Handle{syscall.HKEY_LOCAL_MACHINE, syscall.HKEY_CURRENT_USER} {
for _, view := range []uint32{0x100, 0x200} { // KEY_WOW64_64KEY, KEY_WOW64_32KEY
root, ok := openKey(hive, `SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall`, syscall.KEY_READ|view)
if !ok {
continue
}
for _, name := range subkeyNames(root) {
if k, ok := openKey(root, name, syscall.KEY_QUERY_VALUE|view); ok {
syscall.RegCloseKey(k)
}
}
syscall.RegCloseKey(root)
}
}
}
func callSyscall() {
for i := 0; i < 200; i++ {
if lock {
runtime.LockOSThread()
}
syscall.Getpid()
if lock {
runtime.UnlockOSThread()
}
}
}
func main() {
scan := scanRegistry
if work == "syscall" {
scan = callSyscall
}
var sends atomic.Int64
ch := make(chan int)
go func() {
for range ch {
}
}()
for i := 0; i < 16; i++ {
go func() {
var garbage []string
for {
ch <- 1
sends.Add(1)
garbage = append(garbage, strings.Repeat("x", 100))
if len(garbage) > 1000 {
garbage = garbage[:0]
}
}
}()
}
scans := 0
for deadline := time.Now().Add(25 * time.Second); time.Now().Before(deadline); scans++ {
for i := 0; i < 4; i++ {
go scan()
}
scan()
}
fmt.Printf("ok %s lock=%v work=%q scans=%d sends=%d\n", runtime.Version(), lock, work, scans, sends.Load())
}go build -trimpath -o repro.exe .
.\repro.exeA run lasts 25 seconds and prints ok ... if it survives. I ran 3 or 4 copies at a time.
| Toolchain | Settings | Crashed |
|---|---|---|
| go1.27.1 | default (LOCK=1, WORK=registry) |
11 of 15 |
| go1.27.0 | default | 6 of 6 |
| go1.26.8 | default | 0 of 15 |
| go1.27.1 | LOCK=0 (no LockOSThread) |
0 of 12 |
| go1.27.1 | WORK=syscall (LockOSThread around Getpid only) |
0 of 9 |
| go1.27.1 | GODEBUG=asyncpreemptoff=1 |
5 of 9 |
Two things are needed to trigger it: LockOSThread around the registry calls, and the goroutines that send on the channel. Removing either one stops the crashes. Turning off asynchronous preemption doesn't help, so I don't think this is #79249. The CPU has no AMX, and the program doesn't fault on purpose, so I don't think it's #81238 either.
What did you see happen?
Every failure starts with unexpected return pc in a parked runtime goroutine, usually g 3 (runtime.bgsweep) and sometimes runtime.gcenable.gowrap1 or runtime.gopark. The corrupted stack holds frames from runtime.chansend, runtime.send and goready. Those frames look like they come from one of the channel-sending goroutines, not the goroutine that owns the stack. A GC mark worker reports the corruption when it scans the stack. In this run, goroutine 1 was parked in subkeyNames with its thread still locked. First part of one failure (the full output lists 55 goroutines):
runtime: g 3: unexpected return pc for runtime.bgsweep called from 0x7ff600000008
stack: frame={sp:0xcdec0c3f88, fp:0xcdec0c3fc8} stack=[0xcdec0c2000,0xcdec0c4000)
7 6 5 4 3 2 1 0 f e d c b a 9 8 0123456789abcdef
000000cdec0c3e80: 00007ff6 386cca97 ..l8....
^ <runtime.systemstack+0x37>
000000cdec0c3e90: 000000cd ec0c3ed8 00007ff6 38661205 .>........f8....
^ <runtime.send+0x165>
000000cdec0c3ea0: 000000cd ec0c3ec0 00000000 00000000 .>..............
000000cdec0c3eb0: 00000000 00000000 000000cd ec0be000 ................
000000cdec0c3ec0: 00007ff6 386c0060 000000cd ec0be000 `.l8............
^ <_.goready.func1+0x0>
000000cdec0c3ed0: 00000000 00000004 000000cd ec0c3f48 ........H?......
000000cdec0c3ee0: 00007ff6 38660ff1 00000000 00000000 ..f8............
^ <runtime.chansend+0x551>
000000cdec0c3ef0: 000000cd ec0da000 00000000 00000000 ................
000000cdec0c3f00: 000000cd ec0c3f38 00000000 00000003 8?..............
000000cdec0c3f10: 00000000 00000000 00000000 00000000 ................
000000cdec0c3f20: 00000000 00000000 000000cd ec0ce068 ........h.......
000000cdec0c3f30: 00000000 00000000 00007ff6 386c0020 ........ .l8....
^ <runtime.chansend.func1+0x0>
000000cdec0c3f40: 000000cd ec0ce000 000000cd ec0c3f78 ........x?......
000000cdec0c3f50: 00007ff6 38660a97 000000cd ec0c3f78 ..f8....x?......
^ <runtime.chansend1+0x17>
000000cdec0c3f60: 00007ff6 386c89ea 00000000 00000001 ..l8............
^ <runtime.gopark+0xca>
000000cdec0c3f70: 00000000 00000000 000000cd ec0c3fb8 .........?......
000000cdec0c3f80: 00007ff6 38682a94 00007ff9 851afddc .*h8............
^ <runtime.bgsweep+0x94>
^ SP
000000cdec0c3f90: 00000000 00000000 00000000 00000000 ................
000000cdec0c3fa0: 00000000 00000000 00000000 00000000 ................
000000cdec0c3fb0: 000000cd 00000000 00000007 00000009 ................
000000cdec0c3fc0: 00007ff6 00000008 0000129f 5bd8fd27 ........'..[....
^ FP
000000cdec0c3fd0: 00000000 00000168 00007ff9 851b0009 h...............
000000cdec0c3fe0: 00000000 00000000 000000cd ec0c40f0 .........@......
000000cdec0c3ff0: 00000000 00000014 00000000 00000000 ................
fatal error: unknown caller pc
runtime stack:
runtime.throw({0x7ff6386fb817?, 0x9314dff6c8?})
runtime/panic.go:1243 +0x4d fp=0x9314dff660 sp=0x9314dff630 pc=0x7ff6386c890d
runtime.(*unwinder).next(0x9314dff710)
runtime/traceback.go:475 +0x256 fp=0x9314dff6d8 sp=0x9314dff660 pc=0x7ff6386b4996
runtime.scanstack(0xcdec0beb40, 0xcdec063260)
runtime/mgcmark.go:977 +0x2f1 fp=0x9314dff7f0 sp=0x9314dff6d8 pc=0x7ff63867a4b1
runtime.markroot.func1()
runtime/mgcmark.go:306 +0xb1 fp=0x9314dff840 sp=0x9314dff7f0 pc=0x7ff6386c13d1
runtime.markroot(0xcdec063260, 0xb, 0x1)
runtime/mgcmark.go:280 +0x1a5 fp=0x9314dff8f0 sp=0x9314dff840 pc=0x7ff638678d25
runtime.gcDrain(0xcdec063260, 0x3)
runtime/mgcmark.go:1289 +0x4a7 fp=0x9314dff960 sp=0x9314dff8f0 pc=0x7ff63867b067
runtime.gcDrainMarkWorkerDedicated(...)
runtime/mgcmark.go:1192
runtime.gcBgMarkWorker.func2()
runtime/mgc.go:1878 +0x9a fp=0x9314dff9b0 sp=0x9314dff960 pc=0x7ff6386c10fa
runtime.systemstack(0xcdec554000)
runtime/asm_amd64.s:531 +0x49 fp=0x9314dff9c0 sp=0x9314dff9b0 pc=0x7ff6386ccaa9
goroutine 72 gp=0xcdec47ed20 m=15 mp=0xcdec54c008 [GC worker (active)]:
runtime.systemstack_switch()
runtime/asm_amd64.s:481 +0x8 fp=0xcdec68ff40 sp=0xcdec68ff30 pc=0x7ff6386cca48
runtime.gcBgMarkWorker(0xcdec456070)
runtime/mgc.go:1863 +0x1e5 fp=0xcdec68ffc8 sp=0xcdec68ff40 pc=0x7ff6386770c5
runtime.gcBgMarkStartWorkers.gowrap1()
runtime/mgc.go:1711 +0x17 fp=0xcdec68ffe0 sp=0xcdec68ffc8 pc=0x7ff6386c1037
runtime.goexit({})
runtime/asm_amd64.s:1264 +0x1 fp=0xcdec68ffe8 sp=0xcdec68ffe0 pc=0x7ff6386ce5c1
created by runtime.gcBgMarkStartWorkers in goroutine 28
runtime/mgc.go:1711 +0xfc
goroutine 1 gp=0xcdec0be000 m=nil [GC assist wait, locked to thread]:
runtime.gopark(0xe9178?, 0x508000?, 0xb2?, 0x2c?, 0x9312dff880?)
runtime/proc.go:474 +0xca fp=0xcdec0c5888 sp=0xcdec0c5868 pc=0x7ff6386c89ea
runtime.goparkunlock(...)
runtime/proc.go:480
runtime.gcParkAssist()
runtime/mgcmark.go:822 +0xf7 fp=0xcdec0c58c0 sp=0xcdec0c5888 pc=0x7ff638679fd7
runtime.gcAssistAlloc(0xcdec0be000)
runtime/mgcmark.go:672 +0x338 fp=0xcdec0c5958 sp=0xcdec0c58c0 pc=0x7ff6386798f8
runtime.mallocgcSmallNoScanSlowPath(0x26, 0x7ff6387c4048, 0x1, 0xb, 0x30)
runtime/malloc_generated.go:2133 +0xa5 fp=0xcdec0c59b0 sp=0xcdec0c5958 pc=0x7ff63866d085
runtime.mallocgcSmallNoScanSC5(0xcdec163400?, 0xcdec0c5a28?, 0xc8?)
runtime/malloc_generated.go:1506 +0x68 fp=0xcdec0c5a08 sp=0xcdec0c59b0 pc=0x7ff63866bf88
runtime.mallocgc(0xcdec0c5af8?, 0xcdec0c5cf8?, 0x0?)
runtime/malloc.go:1082 +0x6b fp=0xcdec0c5a38 sp=0xcdec0c5a08 pc=0x7ff6386c748b
runtime.makeslice(0x0?, 0x7ff6387db0e0?, 0x0?)
runtime/slice.go:117 +0x49 fp=0xcdec0c5a60 sp=0xcdec0c5a38 pc=0x7ff6386c9a69
syscall.UTF16ToString({0xcdec0c5af8, 0x7ff60000003a?, 0x100})
syscall/syscall_windows.go:82 +0x71 fp=0xcdec0c5ab0 sp=0xcdec0c5a60 pc=0x7ff6386e2ef1
main.subkeyNames(0x320)
repro/main.go:48 +0x14c fp=0xcdec0c5d58 sp=0xcdec0c5ab0 pc=0x7ff6386f7f8c
main.scanRegistry()
repro/main.go:59 +0xb3 fp=0xcdec0c5dd8 sp=0xcdec0c5d58 pc=0x7ff6386f8193
main.main()
repro/main.go:110 +0x173 fp=0xcdec0c5eb8 sp=0xcdec0c5dd8 pc=0x7ff6386f8453
runtime.main()
runtime/proc.go:302 +0x3fb fp=0xcdec0c5fe0 sp=0xcdec0c5eb8 pc=0x7ff63869823b
runtime.goexit({})
runtime/asm_amd64.s:1264 +0x1 fp=0xcdec0c5fe8 sp=0xcdec0c5fe0 pc=0x7ff6386ce5c1
...In the original application the same bug showed up as unexpected fault address / fatal error: fault in runtime.selectgo called from (*Program).Send. That's a goroutine sending on a channel while another goroutine was in RegCloseKey inside ReadSubKeyNames' locked section.
In earlier runs of an equivalent program on go1.27.0, two processes stopped using CPU and never exited, and I had to kill them. I didn't capture their state.
What did you expect to see?
The program runs for 25 seconds and prints ok, as it does on go1.26.8.
Source: golang/go