diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-07-07 20:21:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-07 20:21:13 +0200 |
commit | c3c03f9ca1925a0fb1317bd464fff93db81b321b (patch) | |
tree | 61ac763193047f04f94ba26462846574c6e46e7a | |
parent | 442220e7a8cc3c3c2f0e7fce275240c2d0a9ed5f (diff) | |
parent | 0bc40bdcf3063fcc0b2db5b65b9c9f74ca08f0f1 (diff) | |
download | podman-c3c03f9ca1925a0fb1317bd464fff93db81b321b.tar.gz podman-c3c03f9ca1925a0fb1317bd464fff93db81b321b.tar.bz2 podman-c3c03f9ca1925a0fb1317bd464fff93db81b321b.zip |
Merge pull request #6883 from vrothberg/2.0-events-race
[2.0] fix race condition in `libpod.GetEvents(...)`
-rw-r--r-- | libpod/events.go | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/libpod/events.go b/libpod/events.go index 7560940a5..3cbde8c5e 100644 --- a/libpod/events.go +++ b/libpod/events.go @@ -3,6 +3,7 @@ package libpod import ( "context" "fmt" + "sync" "github.com/containers/libpod/v2/libpod/events" "github.com/pkg/errors" @@ -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, @@ -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 |