Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 29f6ef3

Browse files
committed
Fix potential races in createWaitForEvent*
It is possible (and noticed in the real world) that the goroutine might enter the out select and then by the time get to writing to the channel back for the event handling to have been canceled.
1 parent c3f099a commit 29f6ef3

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

common/helpers.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,25 @@ func createWaitForEventHandler(
144144
if stringSliceContains(events, ev.typ) {
145145
if predicateFn != nil {
146146
if predicateFn(ev.data) {
147-
ch <- ev.data
147+
select {
148+
case ch <- ev.data:
149+
case <-evCancelCtx.Done():
150+
return
151+
}
148152
}
149153
} else {
150-
ch <- nil
154+
select {
155+
case ch <- nil:
156+
case <-evCancelCtx.Done():
157+
return
158+
}
151159
}
152160
close(ch)
153161

154162
// We wait for one matching event only,
155163
// then remove event handler by cancelling context and stopping goroutine.
156164
evCancelFn()
165+
157166
return
158167
}
159168
}
@@ -185,9 +194,12 @@ func createWaitForEventPredicateHandler(
185194
case ev := <-chEvHandler:
186195
if stringSliceContains(events, ev.typ) &&
187196
predicateFn != nil && predicateFn(ev.data) {
188-
ch <- ev.data
189-
close(ch)
190-
evCancelFn()
197+
select {
198+
case ch <- ev.data:
199+
close(ch)
200+
evCancelFn()
201+
case <-evCancelCtx.Done():
202+
}
191203
return
192204
}
193205
}

0 commit comments

Comments
 (0)