Skip to content

Commit ec972f2

Browse files
committed
iter: fix race instrumentation in Pull2
Pull2 tests are failing with -race, giving false-positive race conditions due to bad race instrumentation. No tests for this as it should be caught by the race builders. The only reason it was not caught is because it is behind GOEXPERIMENT=rangefunc. Fixes [reserved]
1 parent 46ea4ab commit ec972f2

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/iter/iter.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,48 @@ func Pull[V any](seq Seq[V]) (next func() (V, bool), stop func()) {
122122
// simultaneously.
123123
func Pull2[K, V any](seq Seq2[K, V]) (next func() (K, V, bool), stop func()) {
124124
var (
125-
k K
126-
v V
127-
ok bool
128-
done bool
125+
k K
126+
v V
127+
ok bool
128+
done bool
129+
racer int
129130
)
130131
c := newcoro(func(c *coro) {
132+
race.Acquire(unsafe.Pointer(&racer))
131133
yield := func(k1 K, v1 V) bool {
132134
if done {
133135
return false
134136
}
135137
k, v, ok = k1, v1, true
138+
race.Release(unsafe.Pointer(&racer))
136139
coroswitch(c)
140+
race.Acquire(unsafe.Pointer(&racer))
137141
return !done
138142
}
139143
seq(yield)
140144
var k0 K
141145
var v0 V
142146
k, v, ok = k0, v0, false
143147
done = true
148+
race.Release(unsafe.Pointer(&racer))
144149
})
145150
next = func() (k1 K, v1 V, ok1 bool) {
146-
race.Write(unsafe.Pointer(&c)) // detect races
151+
race.Write(unsafe.Pointer(&racer)) // detect races
147152
if done {
148153
return
149154
}
155+
race.Release(unsafe.Pointer(&racer))
150156
coroswitch(c)
157+
race.Acquire(unsafe.Pointer(&racer))
151158
return k, v, ok
152159
}
153160
stop = func() {
154-
race.Write(unsafe.Pointer(&c)) // detect races
161+
race.Write(unsafe.Pointer(&racer)) // detect races
155162
if !done {
156163
done = true
164+
race.Release(unsafe.Pointer(&racer))
157165
coroswitch(c)
166+
race.Acquire(unsafe.Pointer(&racer))
158167
}
159168
}
160169
return next, stop

0 commit comments

Comments
 (0)