Skip to content

runtime: map race with *[]byte as index/key returns nil map value when map value is not nil #76988

@andrewhodel

Description

@andrewhodel

Go version

go version go1.25.3 linux/amd64

Output of go env in your module/workspace:

AR='ar'
CC='gcc'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='1'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='g++'
GCCGO='gccgo'
GO111MODULE=''
GOAMD64='v1'
GOARCH='amd64'
GOAUTH='netrc'
GOBIN=''
GOCACHE='/home/zip/.cache/go-build'
GOCACHEPROG=''
GODEBUG=''
GOENV='/home/zip/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build1641070464=/tmp/go-build -gno-record-gcc-switches'
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMOD='/dev/null'
GOMODCACHE='/home/zip/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/zip/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/zip/xyzbots/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/zip/.config/go/telemetry'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/zip/xyzbots/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.25.3'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"
)

func main() {
	var l = make(map[*[]byte]map[string]string)

	var ll = make([]byte, 0)
	var existing_pointer *[]byte = &ll
	l[existing_pointer] = make(map[string]string)

	var m sync.Mutex

	var c = 0
	for {
		if c == 20000 {
			break
		}

		go func() {

			time.Sleep(time.Second * time.Duration(rand.Intn(10)))

			m.Lock()

			var another_pointer *[]byte
			for e := range l {
				// this is the same pointer as existing_pointer
				another_pointer = e
			}

			if l[another_pointer] == nil {
				// this can happen multiple times incorrectly
				fmt.Println("another_pointer is not in l")
				l[another_pointer] = make(map[string]string)
			}

			m.Unlock()

		}()

		c += 1
	}
}

What did you see happen?

fmt.Println("another_pointer is not in l")

This is executed multiple times.

What did you expect to see?

fmt.Println("another_pointer is not in l")

This executed once.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.WaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions