Fixing stats race condition

Signed-off-by: teivah <t.harsanyi@thebeat.co>
This commit is contained in:
teivah 2022-05-10 12:10:12 +02:00
parent 4cc4385075
commit 13e053387b
No known key found for this signature in database
GPG Key ID: E27C9FE630A49EF9
1 changed files with 7 additions and 3 deletions

View File

@ -60,7 +60,7 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error {
// monitorContainerEvents watches for container creation and removal (only // monitorContainerEvents watches for container creation and removal (only
// used when calling `docker stats` without arguments). // used when calling `docker stats` without arguments).
monitorContainerEvents := func(started chan<- struct{}, c chan events.Message) { monitorContainerEvents := func(started chan<- struct{}, c chan events.Message, stopped <-chan struct{}) {
f := filters.NewArgs() f := filters.NewArgs()
f.Add("type", "container") f.Add("type", "container")
options := types.EventsOptions{ options := types.EventsOptions{
@ -72,9 +72,12 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error {
// Whether we successfully subscribed to eventq or not, we can now // Whether we successfully subscribed to eventq or not, we can now
// unblock the main goroutine. // unblock the main goroutine.
close(started) close(started)
defer close(c)
for { for {
select { select {
case <-stopped:
return
case event := <-eventq: case event := <-eventq:
c <- event c <- event
case err := <-errq: case err := <-errq:
@ -150,8 +153,9 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error {
eventChan := make(chan events.Message) eventChan := make(chan events.Message)
go eh.Watch(eventChan) go eh.Watch(eventChan)
go monitorContainerEvents(started, eventChan) stopped := make(chan struct{})
defer close(eventChan) go monitorContainerEvents(started, eventChan, stopped)
defer close(stopped)
<-started <-started
// Start a short-lived goroutine to retrieve the initial list of // Start a short-lived goroutine to retrieve the initial list of