Merge pull request #3597 from teivah/stats-race

Fixing stats race condition
This commit is contained in:
Sebastiaan van Stijn 2022-05-12 12:55:27 +02:00 committed by GitHub
commit 2b4ffb301b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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