Skip to content

Commit edd99f4

Browse files
committed
fix race condition
1 parent 0f3254a commit edd99f4

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

cli/stat.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,37 @@ func (*RootCmd) stat() *clibase.Cmd {
3232
var sr statsRow
3333

3434
// Get CPU measurements first.
35-
errCh := make(chan error, 2)
35+
hostErr := make(chan error)
36+
containerErr := make(chan error)
3637
go func() {
38+
defer close(hostErr)
3739
cs, err := st.HostCPU()
3840
if err != nil {
39-
errCh <- err
41+
hostErr <- err
4042
return
4143
}
4244
sr.HostCPU = cs
43-
errCh <- nil
4445
}()
4546
go func() {
47+
defer close(containerErr)
4648
if ok, _ := clistat.IsContainerized(fs); !ok {
47-
errCh <- nil
49+
// don't error if we're not in a container
50+
return
4851
}
4952
cs, err := st.ContainerCPU()
5053
if err != nil {
51-
errCh <- err
54+
containerErr <- err
5255
return
5356
}
5457
sr.ContainerCPU = cs
55-
errCh <- nil
5658
}()
5759

58-
if err1 := <-errCh; err1 != nil {
60+
if err := <-hostErr; err != nil {
5961
return err
6062
}
61-
if err2 := <-errCh; err2 != nil {
63+
if err := <-containerErr; err != nil {
6264
return err
6365
}
64-
close(errCh)
6566

6667
// Host-level stats
6768
ms, err := st.HostMemory()

0 commit comments

Comments
 (0)