1
0
mirror of https://github.com/docker/cli.git synced 2026-01-06 05:41:44 +03:00

cli/command/container: RunStats: small tweaks on closeChan

Some suggestions from ChatGPT to prevent deadlocks.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2025-10-28 11:30:36 +01:00
parent d309027d58
commit 0b1c7bc0f1

View File

@@ -113,8 +113,10 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
// waitFirst is a WaitGroup to wait first stat data's reach for each container
waitFirst := &sync.WaitGroup{}
// closeChan is a non-buffered channel used to collect errors from goroutines.
closeChan := make(chan error)
// closeChan is used to collect errors from goroutines. It uses a small buffer
// to avoid blocking sends when sends occur after closeChan is set to nil or
// after the reader has exited, preventing deadlocks.
closeChan := make(chan error, 4)
cStats := stats{}
showAll := len(options.Containers) == 0
@@ -197,7 +199,12 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
case event := <-eventChan:
c <- event
case err := <-errChan:
closeChan <- err
// Prevent blocking if closeChan is full or unread
select {
case closeChan <- err:
default:
// drop if not read; avoids deadlock
}
return
}
}