layer: protect mountedLayer.references
Add a mutex to protect concurrent access to mountedLayer.references map.
@@ -2,6 +2,7 @@ package layer // import "github.com/docker/docker/layer"
import (
"io"
+ "sync"
"github.com/docker/docker/pkg/archive"
"github.com/docker/docker/pkg/containerfs"
@@ -15,6 +16,7 @@ type mountedLayer struct {
path string
layerStore *layerStore
+ sync.Mutex
references map[RWLayer]*referencedRWLayer
}
@@ -62,16 +64,24 @@ func (ml *mountedLayer) getReference() RWLayer {
ref := &referencedRWLayer{
mountedLayer: ml,
}
+ ml.Lock()
ml.references[ref] = ref
+ ml.Unlock()
return ref
}
func (ml *mountedLayer) hasReferences() bool {
- return len(ml.references) > 0
+ ml.Lock()
+ ret := len(ml.references) > 0
+ ml.Unlock()
+
+ return ret
}
func (ml *mountedLayer) deleteReference(ref RWLayer) error {
+ ml.Lock()
+ defer ml.Unlock()
if _, ok := ml.references[ref]; !ok {
return ErrLayerNotRetained
}
@@ -81,7 +91,9 @@ func (ml *mountedLayer) deleteReference(ref RWLayer) error {
func (ml *mountedLayer) retakeReference(r RWLayer) {
if ref, ok := r.(*referencedRWLayer); ok {
+ ml.Lock()
ml.references[ref] = ref
+ ml.Unlock()
}
}
