From 812b04d36332c7de6cc7e96352bc997462c34086 Mon Sep 17 00:00:00 2001 From: Jason Wilder Date: Tue, 10 Feb 2015 11:20:45 -0700 Subject: [PATCH 1/2] Don't ignore err when getting CgroupMemDocker stats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes panic: runtime error: index out of range goroutine 10 [running]: testing.funcĀ·006() /usr/local/go/src/testing/testing.go:441 +0x181 github.com/shirou/gopsutil/docker.CgroupMem(0x586b30, 0x6, 0x5a87d0, 0x1c, 0x0, 0x0, 0x0) /home/jwilder/go/src/github.com/shirou/gopsutil/docker/docker_linux.go:119 +0xf48 github.com/shirou/gopsutil/docker.CgroupMemDocker(0x586b30, 0x6, 0x0, 0x0, 0x0) /home/jwilder/go/src/github.com/shirou/gopsutil/docker/docker_linux.go:184 +0x57 If the ID passed to the CGroupMemDocker does not exist, you can get a panic at runtime. This can happen when a container exits before calling the func. --- docker/docker_linux.go | 5 ++++- docker/docker_linux_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docker/docker_linux.go b/docker/docker_linux.go index 0f783d7..a1bd1ea 100644 --- a/docker/docker_linux.go +++ b/docker/docker_linux.go @@ -109,7 +109,10 @@ func CgroupMem(containerid string, base string) (*CgroupMemStat, error) { if len(containerid) == 0 { containerid = "all" } - lines, _ := common.ReadLines(path) + lines, err := common.ReadLines(path) + if err != nil { + return nil, err + } ret := &CgroupMemStat{ContainerID: containerid} for _, line := range lines { fields := strings.Split(line, " ") diff --git a/docker/docker_linux_test.go b/docker/docker_linux_test.go index d5504f7..7b49d7c 100644 --- a/docker/docker_linux_test.go +++ b/docker/docker_linux_test.go @@ -44,3 +44,10 @@ func TestCgroupMem(t *testing.T) { } } } + +func TestCgroupMemInvalidId(t *testing.T) { + _, err := CgroupMemDocker("bad id") + if err == nil { + t.Error("Expected path does not exist error") + } +} From 90c6c3ef3ee32b95b5b51c7540e80fb0e0580159 Mon Sep 17 00:00:00 2001 From: Jason Wilder Date: Tue, 10 Feb 2015 11:36:19 -0700 Subject: [PATCH 2/2] Fix panic in CgroupCPU If an invalid container ID is passed to CgroupCPU, a panic can result. --- docker/docker_linux.go | 5 ++++- docker/docker_linux_test.go | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/docker/docker_linux.go b/docker/docker_linux.go index a1bd1ea..25470b1 100644 --- a/docker/docker_linux.go +++ b/docker/docker_linux.go @@ -71,7 +71,10 @@ func CgroupCPU(containerid string, base string) (*cpu.CPUTimesStat, error) { } path := path.Join(base, containerid, "cpuacct.stat") - lines, _ := common.ReadLines(path) + lines, err := common.ReadLines(path) + if err != nil { + return nil, err + } // empty containerid means all cgroup if len(containerid) == 0 { containerid = "all" diff --git a/docker/docker_linux_test.go b/docker/docker_linux_test.go index 7b49d7c..48cedfb 100644 --- a/docker/docker_linux_test.go +++ b/docker/docker_linux_test.go @@ -31,6 +31,13 @@ func TestCgroupCPU(t *testing.T) { } } +func TestCgroupCPUInvalidId(t *testing.T) { + _, err := CgroupCPUDocker("bad id") + if err == nil { + t.Error("Expected path does not exist error") + } +} + func TestCgroupMem(t *testing.T) { v, _ := GetDockerIDList() for _, id := range v {