From 7500c8cc7288bcd6f32b7fe27ff094b82d5ba528 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Tue, 29 Mar 2016 09:43:12 -0700 Subject: [PATCH] libcontainerd: reuse our pkg/locker it fixes race with access to containerMutexes Signed-off-by: Alexander Morozov (cherry picked from commit a7851e2556edb3e5333b6fe53160755fb5b7d616) --- libcontainerd/client.go | 26 +++++++------------------- libcontainerd/remote_linux.go | 7 ++++--- libcontainerd/remote_windows.go | 8 ++++---- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/libcontainerd/client.go b/libcontainerd/client.go index 4485b75cf9..7e8e47bcfa 100644 --- a/libcontainerd/client.go +++ b/libcontainerd/client.go @@ -4,35 +4,23 @@ import ( "fmt" "sync" - "github.com/Sirupsen/logrus" + "github.com/docker/docker/pkg/locker" ) // clientCommon contains the platform agnostic fields used in the client structure type clientCommon struct { - backend Backend - containers map[string]*container - containerMutexes map[string]*sync.Mutex // lock by container ID - mapMutex sync.RWMutex // protects read/write oprations from containers map - sync.Mutex // lock for containerMutexes map access + backend Backend + containers map[string]*container + locker *locker.Locker + mapMutex sync.RWMutex // protects read/write oprations from containers map } func (clnt *client) lock(containerID string) { - clnt.Lock() - if _, ok := clnt.containerMutexes[containerID]; !ok { - clnt.containerMutexes[containerID] = &sync.Mutex{} - } - clnt.Unlock() - clnt.containerMutexes[containerID].Lock() + clnt.locker.Lock(containerID) } func (clnt *client) unlock(containerID string) { - clnt.Lock() - if l, ok := clnt.containerMutexes[containerID]; ok { - l.Unlock() - } else { - logrus.Warnf("unlock of non-existing mutex: %s", containerID) - } - clnt.Unlock() + clnt.locker.Unlock(containerID) } // must hold a lock for cont.containerID diff --git a/libcontainerd/remote_linux.go b/libcontainerd/remote_linux.go index eddcc7fba3..4df3e1f098 100644 --- a/libcontainerd/remote_linux.go +++ b/libcontainerd/remote_linux.go @@ -16,6 +16,7 @@ import ( "github.com/Sirupsen/logrus" containerd "github.com/docker/containerd/api/grpc/types" + "github.com/docker/docker/pkg/locker" sysinfo "github.com/docker/docker/pkg/system" "github.com/docker/docker/utils" "golang.org/x/net/context" @@ -169,9 +170,9 @@ func (r *remote) Cleanup() { func (r *remote) Client(b Backend) (Client, error) { c := &client{ clientCommon: clientCommon{ - backend: b, - containerMutexes: make(map[string]*sync.Mutex), - containers: make(map[string]*container), + backend: b, + containers: make(map[string]*container), + locker: locker.New(), }, remote: r, exitNotifiers: make(map[string]*exitNotifier), diff --git a/libcontainerd/remote_windows.go b/libcontainerd/remote_windows.go index c313779ee2..ce01f74f25 100644 --- a/libcontainerd/remote_windows.go +++ b/libcontainerd/remote_windows.go @@ -1,6 +1,6 @@ package libcontainerd -import "sync" +import "github.com/docker/docker/pkg/locker" type remote struct { } @@ -8,9 +8,9 @@ type remote struct { func (r *remote) Client(b Backend) (Client, error) { c := &client{ clientCommon: clientCommon{ - backend: b, - containerMutexes: make(map[string]*sync.Mutex), - containers: make(map[string]*container), + backend: b, + containers: make(map[string]*container), + locker: locker.New(), }, } return c, nil