Skip to content

Commit

Permalink
libcontainerd: reuse our pkg/locker
Browse files Browse the repository at this point in the history
it fixes race with access to containerMutexes

Signed-off-by: Alexander Morozov <[email protected]>
  • Loading branch information
LK4D4 committed Mar 29, 2016
1 parent d334804 commit a7851e2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
26 changes: 7 additions & 19 deletions libcontainerd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions libcontainerd/remote_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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),
Expand Down
8 changes: 4 additions & 4 deletions libcontainerd/remote_windows.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package libcontainerd

import "sync"
import "github.com/docker/docker/pkg/locker"

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
Expand Down

0 comments on commit a7851e2

Please sign in to comment.