Skip to content

Commit 5dcedd6

Browse files
neildmknyszek
authored andcommitted
runtime: lock mheap_.speciallock when allocating synctest specials
Avoid racing use of mheap_.specialBubbleAlloc. Fixes golang#75134 Change-Id: I0c9140c18d2bca1e1c3387cd81230f0e8c9ac23e Reviewed-on: https://go-review.googlesource.com/c/go/+/699255 Reviewed-by: Michael Knyszek <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent d3be949 commit 5dcedd6

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/internal/synctest/synctest_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,28 @@ func TestWaitGroupHeapAllocated(t *testing.T) {
779779
})
780780
}
781781

782+
// Issue #75134: Many racing bubble associations.
783+
func TestWaitGroupManyBubbles(t *testing.T) {
784+
var wg sync.WaitGroup
785+
for range 100 {
786+
wg.Go(func() {
787+
synctest.Run(func() {
788+
cancelc := make(chan struct{})
789+
var wg2 sync.WaitGroup
790+
for range 100 {
791+
wg2.Go(func() {
792+
<-cancelc
793+
})
794+
}
795+
synctest.Wait()
796+
close(cancelc)
797+
wg2.Wait()
798+
})
799+
})
800+
}
801+
wg.Wait()
802+
}
803+
782804
func TestHappensBefore(t *testing.T) {
783805
// Use two parallel goroutines accessing different vars to ensure that
784806
// we correctly account for multiple goroutines in the bubble.

src/runtime/synctest.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ func getOrSetBubbleSpecial(p unsafe.Pointer, bubbleid uint64, add bool) (assoc i
410410
} else if add {
411411
// p is not associated with a bubble,
412412
// and we've been asked to add an association.
413+
lock(&mheap_.speciallock)
413414
s := (*specialBubble)(mheap_.specialBubbleAlloc.alloc())
415+
unlock(&mheap_.speciallock)
414416
s.bubbleid = bubbleid
415417
s.special.kind = _KindSpecialBubble
416418
s.special.offset = offset

0 commit comments

Comments
 (0)