Forráskód Böngészése

Merge pull request #21620 from LK4D4/fix_libcd_race

libcontainerd: reuse our pkg/locker
Brian Goff 9 éve
szülő
commit
8899896992

+ 7 - 19
libcontainerd/client.go

@@ -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
-	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) {
 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) {
 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
 // must hold a lock for cont.containerID

+ 4 - 3
libcontainerd/remote_linux.go

@@ -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,
-			containerMutexes: make(map[string]*sync.Mutex),
-			containers:       make(map[string]*container),
+			backend:    b,
+			containers: make(map[string]*container),
+			locker:     locker.New(),
 		},
 		},
 		remote:        r,
 		remote:        r,
 		exitNotifiers: make(map[string]*exitNotifier),
 		exitNotifiers: make(map[string]*exitNotifier),

+ 4 - 4
libcontainerd/remote_windows.go

@@ -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,
-			containerMutexes: make(map[string]*sync.Mutex),
-			containers:       make(map[string]*container),
+			backend:    b,
+			containers: make(map[string]*container),
+			locker:     locker.New(),
 		},
 		},
 	}
 	}
 	return c, nil
 	return c, nil