Skip to content

Commit 5a4d697

Browse files
committed
cli/command/container: RunStats: small tweaks on closeChan
Some suggestions from ChatGPT to prevent deadlocks. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent d309027 commit 5a4d697

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

cli/command/container/stats.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,9 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
113113

114114
// waitFirst is a WaitGroup to wait first stat data's reach for each container
115115
waitFirst := &sync.WaitGroup{}
116-
// closeChan is a non-buffered channel used to collect errors from goroutines.
117-
closeChan := make(chan error)
116+
// closeChan is used to collect errors from goroutines.
117+
// It uses a small buffer to avoid blocking sends before the reader starts.
118+
closeChan := make(chan error, 4)
118119
cStats := stats{}
119120

120121
showAll := len(options.Containers) == 0
@@ -197,7 +198,12 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
197198
case event := <-eventChan:
198199
c <- event
199200
case err := <-errChan:
200-
closeChan <- err
201+
// Prevent blocking if closeChan is full or unread
202+
select {
203+
case closeChan <- err:
204+
default:
205+
// drop if not read; avoids deadlock
206+
}
201207
return
202208
}
203209
}

0 commit comments

Comments
 (0)