Skip to content

Commit

Permalink
Merge pull request #6881 from vrothberg/events-race
Browse files Browse the repository at this point in the history
fix race condition in `libpod.GetEvents(...)`
  • Loading branch information
openshift-merge-robot authored Jul 7, 2020
2 parents 8014008 + f4a2d25 commit cd08485
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions libpod/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package libpod
import (
"context"
"fmt"
"sync"

"github.com/containers/libpod/v2/libpod/events"
"github.com/pkg/errors"
Expand Down Expand Up @@ -86,7 +87,6 @@ func (r *Runtime) Events(ctx context.Context, options events.ReadOptions) error

// GetEvents reads the event log and returns events based on input filters
func (r *Runtime) GetEvents(ctx context.Context, filters []string) ([]*events.Event, error) {
var readErr error
eventChannel := make(chan *events.Event)
options := events.ReadOptions{
EventChannel: eventChannel,
Expand All @@ -98,17 +98,20 @@ func (r *Runtime) GetEvents(ctx context.Context, filters []string) ([]*events.Ev
if err != nil {
return nil, err
}

logEvents := make([]*events.Event, 0, len(eventChannel))
readLock := sync.Mutex{}
readLock.Lock()
go func() {
readErr = eventer.Read(ctx, options)
for e := range eventChannel {
logEvents = append(logEvents, e)
}
readLock.Unlock()
}()
if readErr != nil {
return nil, readErr
}
logEvents := make([]*events.Event, 0, len(eventChannel))
for e := range eventChannel {
logEvents = append(logEvents, e)
}
return logEvents, nil

readErr := eventer.Read(ctx, options)
readLock.Lock() // Wait for the events to be consumed.
return logEvents, readErr
}

// GetLastContainerEvent takes a container name or ID and an event status and returns
Expand Down

0 comments on commit cd08485

Please sign in to comment.