Skip to content

mock: synchronize TestData lazy initialization - #2019

Open
anishpentyala wants to merge 2 commits into
stretchr:masterfrom
anishpentyala:mock-testdata-initialization
Open

anishpentyala wants to merge 2 commits into
stretchr:masterfrom
anishpentyala:mock-testdata-initialization

Conversation

@anishpentyala

Copy link
Copy Markdown

Summary

Guard Mock.TestData initialization with the existing Mock mutex so concurrent accessors receive the same initialized map.

Changes

  • Lock around the nil check, allocation and returned map lookup.
  • Clarify that callers must still synchronize access to the returned map. This does not make arbitrary concurrent map reads/writes safe.
  • Add a regression that releases 32 accessor goroutines together on fresh Mock instances, then checks shared map identity with sequential writes/reads after all accessors finish.

Motivation

The reproduction in #2018 also fails on current master 87a7b9d5 under the race detector. The added regression fails there with a data race and can also show callers receiving orphaned maps. The change only synchronizes lazy initialization; it does not change the map type or the API.

Validation on Linux amd64 with Go 1.26.6:

  • go test -race ./...
  • go test -race ./mock -run '^TestMockTestDataConcurrentInitialization$' -count=100
  • The same focused 100-repeat race run with GOMAXPROCS=1
  • go vet ./...
  • Project formatting and generation checks, with no generated diff

Older Go versions and other operating systems have not been tested locally.

Related issues

Closes #2018.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mock: data race in Mock.TestData() lazy init (skips mutex)

1 participant