go: sync: Map CompareAndSwap is not atomic with respect to other map operations in Go 1.24

Go version

go version devel go1.24-eef35e3bd9 Mon Dec 23 07:08:40 2024 -0800 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/Users/flo/Library/Caches/go-build'
GODEBUG=''
GOENV='/Users/flo/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/1_/dsgxrj_j2ws_5h4ykmddqjyr0000gn/T/go-build418421246=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD='/Users/flo/Projects/dex/go.mod'
GOMODCACHE='/Users/flo/go/pkg/mod'
GONOPROXY='*.home.znkr.io'
GONOSUMDB='*.home.znkr.io'
GOOS='darwin'
GOPATH='/Users/flo/go'
GOPRIVATE='*.home.znkr.io'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/Users/flo/Projects/go/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='on'
GOTELEMETRYDIR='/Users/flo/Library/Application Support/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/Users/flo/Projects/go/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='devel go1.24-eef35e3bd9 Mon Dec 23 07:08:40 2024 -0800'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

I am experimenting with weak maps and ran into a problem that I can’t explain. I am either missing something and there’s a bug somewhere in my implementation or there’s a bug in Go.

I reduced the problem to

package main

import (
	"fmt"
	"runtime"
	"sync"
	"weak"
)

const N = 1_000_000
const P = 5_000

type dummy [32]byte

func main() {
	var m sync.Map
	var wg sync.WaitGroup
	wg.Add(N)
	for i := range N {
		go func() {
			defer wg.Done()
			a := get(&m, i%P)
			b := get(&m, i%P)
			if a != b {
				fmt.Printf("a != b (%p vs %p)\n", a, b)
			}
		}()
	}
	wg.Wait()
}

func get(m *sync.Map, key int) *dummy {
	nv := new(dummy)
	nw := weak.Make(nv)
	for {
		w, loaded := m.LoadOrStore(key, nw)
		if !loaded {
			runtime.AddCleanup(nv, cleanup, cleanupArg{m, key, nw})
			return nv
		}
		if v := w.(weak.Pointer[dummy]).Value(); v != nil {
			return v
		}

		// Weak pointer was reclaimed, try to replace it with nw.
		if m.CompareAndSwap(key, w, nw) {
			runtime.AddCleanup(nv, cleanup, cleanupArg{m, key, nw})
			return nv
		}

		// This works:
		// m.CompareAndDelete(key, w)
	}
}

type cleanupArg struct {
	m     *sync.Map
	key   int
	value weak.Pointer[dummy]
}

func cleanup(arg cleanupArg) {
	arg.m.CompareAndDelete(arg.key, arg.value)
}

What did you see happen?

Unfortunately, this is a bit flaky and it doesn’t always reproduce. I sometimes have to run the program multiple times. When it reproduces it prints something like this:

a != b (0x1400506bee0 vs 0x1400506bf00)
a != b (0x14006316540 vs 0x1400206dc40)
a != b (0x140040dca40 vs 0x140040dca80)

I can also reproduce it on the Go playground: https://go.dev/play/p/HBJ2IU2IGdF?v=gotip

What did you expect to see?

I expect to always observe that same pointer being returned by get for the same key.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (14 by maintainers)

Commits related to this issue

Most upvoted comments

I’m going to spend a little time staring at your code to try to figure out what I think ought to be happening, but it might not hurt if comments in the example or a later commentary explained what you thought was supposed to be going on. It can be easier to check your assumptions, than to run this GC-interacting piece of code in my error-prone head.