Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential races in createWaitForEvent* #1391

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,25 @@ func createWaitForEventHandler(
if stringSliceContains(events, ev.typ) {
if predicateFn != nil {
if predicateFn(ev.data) {
ch <- ev.data
select {
case ch <- ev.data:
case <-evCancelCtx.Done():
return
}
}
} else {
ch <- nil
select {
case ch <- nil:
case <-evCancelCtx.Done():
return
}
}
close(ch)

// We wait for one matching event only,
// then remove event handler by cancelling context and stopping goroutine.
evCancelFn()

return
}
}
Expand Down Expand Up @@ -185,9 +194,12 @@ func createWaitForEventPredicateHandler(
case ev := <-chEvHandler:
if stringSliceContains(events, ev.typ) &&
predicateFn != nil && predicateFn(ev.data) {
ch <- ev.data
close(ch)
evCancelFn()
select {
case ch <- ev.data:
close(ch)
evCancelFn()
case <-evCancelCtx.Done():
}
return
}
}
Expand Down
Loading