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
// 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.Add("type", "container")
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
// unblock the main goroutine.
close(started)
defer close(c)
for {
select {
case <-stopped:
return
case event := <-eventq:
c <- event
case err := <-errq:
@ -150,8 +153,9 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error {
eventChan := make(chan events.Message)
go eh.Watch(eventChan)
go monitorContainerEvents(started, eventChan)
defer close(eventChan)
stopped := make(chan struct{})
go monitorContainerEvents(started, eventChan, stopped)
defer close(stopped)
<-started
// Start a short-lived goroutine to retrieve the initial list of