From 812b04d36332c7de6cc7e96352bc997462c34086 Mon Sep 17 00:00:00 2001 From: Jason Wilder Date: Tue, 10 Feb 2015 11:20:45 -0700 Subject: [PATCH] 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") + } +}