Skip to content

Commit

Permalink
[release-branch.go1.22] runtime: model wakeableSleep.lock in the race…
Browse files Browse the repository at this point in the history
… detector

Currently the flight recorder tests are failing in race mode because the
race detector doesn't see s.lock, leading to false positives. This has
also appeared in the trace tests. Model the lock in the race detector.

Fixes #65207.
Fixes #65283.

Change-Id: I1e9a5c9606536f55fdfc46b5f8443e9c7213c23d
Reviewed-on: https://go-review.googlesource.com/c/go/+/560215
LUCI-TryBot-Result: Go LUCI <[email protected]>
Reviewed-by: Michael Pratt <[email protected]>
(cherry picked from commit 0b12e3d)
Reviewed-on: https://go-review.googlesource.com/c/go/+/559956
Auto-Submit: Michael Knyszek <[email protected]>
  • Loading branch information
mknyszek authored and gopherbot committed Feb 1, 2024
1 parent 7b3786b commit e34f6a9
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/runtime/trace2.go
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,13 @@ func newWakeableSleep() *wakeableSleep {
func (s *wakeableSleep) sleep(ns int64) {
resetTimer(s.timer, nanotime()+ns)
lock(&s.lock)
if raceenabled {
raceacquire(unsafe.Pointer(&s.lock))
}
wakeup := s.wakeup
if raceenabled {
racerelease(unsafe.Pointer(&s.lock))
}
unlock(&s.lock)
<-wakeup
stopTimer(s.timer)
Expand All @@ -945,6 +951,9 @@ func (s *wakeableSleep) wake() {
// Grab the wakeup channel, which may be nil if we're
// racing with close.
lock(&s.lock)
if raceenabled {
raceacquire(unsafe.Pointer(&s.lock))
}
if s.wakeup != nil {
// Non-blocking send.
//
Expand All @@ -956,6 +965,9 @@ func (s *wakeableSleep) wake() {
default:
}
}
if raceenabled {
racerelease(unsafe.Pointer(&s.lock))
}
unlock(&s.lock)
}

Expand All @@ -969,11 +981,18 @@ func (s *wakeableSleep) wake() {
func (s *wakeableSleep) close() {
// Set wakeup to nil so that a late timer ends up being a no-op.
lock(&s.lock)
if raceenabled {
raceacquire(unsafe.Pointer(&s.lock))
}
wakeup := s.wakeup
s.wakeup = nil

// Close the channel.
close(wakeup)

if raceenabled {
racerelease(unsafe.Pointer(&s.lock))
}
unlock(&s.lock)
return
}

0 comments on commit e34f6a9

Please sign in to comment.