From 5425a5ab842894d7eb7e26bb706a04be40b67075 Mon Sep 17 00:00:00 2001 From: yangshukui Date: Thu, 22 Jun 2017 16:47:47 +0800 Subject: [PATCH] fix mem leak in libcontainerd/queue/append Signed-off-by: yangshukui --- libcontainerd/queue_unix.go | 6 ++++++ libcontainerd/queue_unix_test.go | 33 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 libcontainerd/queue_unix_test.go diff --git a/libcontainerd/queue_unix.go b/libcontainerd/queue_unix.go index b848b9872b..66765f75ec 100644 --- a/libcontainerd/queue_unix.go +++ b/libcontainerd/queue_unix.go @@ -27,5 +27,11 @@ func (q *queue) append(id string, f func()) { } f() close(done) + + q.Lock() + if q.fns[id] == done { + delete(q.fns, id) + } + q.Unlock() }() } diff --git a/libcontainerd/queue_unix_test.go b/libcontainerd/queue_unix_test.go new file mode 100644 index 0000000000..bb49a5d4c2 --- /dev/null +++ b/libcontainerd/queue_unix_test.go @@ -0,0 +1,33 @@ +// +build linux solaris + +package libcontainerd + +import ( + "testing" + "time" + + "github.com/stretchr/testify/require" +) + +func TestSerialization(t *testing.T) { + var ( + q queue + serialization = 1 + ) + + q.append("aaa", func() { + //simulate a long time task + time.Sleep(10 * time.Millisecond) + require.EqualValues(t, serialization, 1) + serialization = 2 + }) + q.append("aaa", func() { + require.EqualValues(t, serialization, 2) + serialization = 3 + }) + q.append("aaa", func() { + require.EqualValues(t, serialization, 3) + serialization = 4 + }) + time.Sleep(20 * time.Millisecond) +}