1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

libcontainerd: reuse our pkg/locker

it fixes race with access to containerMutexes

Signed-off-by: Alexander Morozov <lk4d4@docker.com>
This commit is contained in:
Alexander Morozov 2016-03-29 09:43:12 -07:00
parent d33480474f
commit a7851e2556
3 changed files with 15 additions and 26 deletions

View file

@ -4,35 +4,23 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/Sirupsen/logrus" "github.com/docker/docker/pkg/locker"
) )
// clientCommon contains the platform agnostic fields used in the client structure // clientCommon contains the platform agnostic fields used in the client structure
type clientCommon struct { type clientCommon struct {
backend Backend backend Backend
containers map[string]*container containers map[string]*container
containerMutexes map[string]*sync.Mutex // lock by container ID locker *locker.Locker
mapMutex sync.RWMutex // protects read/write oprations from containers map mapMutex sync.RWMutex // protects read/write oprations from containers map
sync.Mutex // lock for containerMutexes map access
} }
func (clnt *client) lock(containerID string) { func (clnt *client) lock(containerID string) {
clnt.Lock() clnt.locker.Lock(containerID)
if _, ok := clnt.containerMutexes[containerID]; !ok {
clnt.containerMutexes[containerID] = &sync.Mutex{}
}
clnt.Unlock()
clnt.containerMutexes[containerID].Lock()
} }
func (clnt *client) unlock(containerID string) { func (clnt *client) unlock(containerID string) {
clnt.Lock() clnt.locker.Unlock(containerID)
if l, ok := clnt.containerMutexes[containerID]; ok {
l.Unlock()
} else {
logrus.Warnf("unlock of non-existing mutex: %s", containerID)
}
clnt.Unlock()
} }
// must hold a lock for cont.containerID // must hold a lock for cont.containerID

View file

@ -16,6 +16,7 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
containerd "github.com/docker/containerd/api/grpc/types" containerd "github.com/docker/containerd/api/grpc/types"
"github.com/docker/docker/pkg/locker"
sysinfo "github.com/docker/docker/pkg/system" sysinfo "github.com/docker/docker/pkg/system"
"github.com/docker/docker/utils" "github.com/docker/docker/utils"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -169,9 +170,9 @@ func (r *remote) Cleanup() {
func (r *remote) Client(b Backend) (Client, error) { func (r *remote) Client(b Backend) (Client, error) {
c := &client{ c := &client{
clientCommon: clientCommon{ clientCommon: clientCommon{
backend: b, backend: b,
containerMutexes: make(map[string]*sync.Mutex), containers: make(map[string]*container),
containers: make(map[string]*container), locker: locker.New(),
}, },
remote: r, remote: r,
exitNotifiers: make(map[string]*exitNotifier), exitNotifiers: make(map[string]*exitNotifier),

View file

@ -1,6 +1,6 @@
package libcontainerd package libcontainerd
import "sync" import "github.com/docker/docker/pkg/locker"
type remote struct { type remote struct {
} }
@ -8,9 +8,9 @@ type remote struct {
func (r *remote) Client(b Backend) (Client, error) { func (r *remote) Client(b Backend) (Client, error) {
c := &client{ c := &client{
clientCommon: clientCommon{ clientCommon: clientCommon{
backend: b, backend: b,
containerMutexes: make(map[string]*sync.Mutex), containers: make(map[string]*container),
containers: make(map[string]*container), locker: locker.New(),
}, },
} }
return c, nil return c, nil