mirror of
https://github.com/containers/podman.git
synced 2025-07-15 03:02:52 +08:00
Merge pull request #6881 from vrothberg/events-race
fix race condition in `libpod.GetEvents(...)`
This commit is contained in:
@ -3,6 +3,7 @@ package libpod
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/containers/libpod/v2/libpod/events"
|
"github.com/containers/libpod/v2/libpod/events"
|
||||||
"github.com/pkg/errors"
|
"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
|
// GetEvents reads the event log and returns events based on input filters
|
||||||
func (r *Runtime) GetEvents(ctx context.Context, filters []string) ([]*events.Event, error) {
|
func (r *Runtime) GetEvents(ctx context.Context, filters []string) ([]*events.Event, error) {
|
||||||
var readErr error
|
|
||||||
eventChannel := make(chan *events.Event)
|
eventChannel := make(chan *events.Event)
|
||||||
options := events.ReadOptions{
|
options := events.ReadOptions{
|
||||||
EventChannel: eventChannel,
|
EventChannel: eventChannel,
|
||||||
@ -98,17 +98,20 @@ func (r *Runtime) GetEvents(ctx context.Context, filters []string) ([]*events.Ev
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
go func() {
|
|
||||||
readErr = eventer.Read(ctx, options)
|
|
||||||
}()
|
|
||||||
if readErr != nil {
|
|
||||||
return nil, readErr
|
|
||||||
}
|
|
||||||
logEvents := make([]*events.Event, 0, len(eventChannel))
|
logEvents := make([]*events.Event, 0, len(eventChannel))
|
||||||
|
readLock := sync.Mutex{}
|
||||||
|
readLock.Lock()
|
||||||
|
go func() {
|
||||||
for e := range eventChannel {
|
for e := range eventChannel {
|
||||||
logEvents = append(logEvents, e)
|
logEvents = append(logEvents, e)
|
||||||
}
|
}
|
||||||
return logEvents, nil
|
readLock.Unlock()
|
||||||
|
}()
|
||||||
|
|
||||||
|
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
|
// GetLastContainerEvent takes a container name or ID and an event status and returns
|
||||||
|
Reference in New Issue
Block a user