Browse code

Fix a race condition in TestInterruptedRegister

Solomon Hykes authored on 2013/11/26 13:48:34
Showing 1 changed files
... ...
@@ -131,7 +131,15 @@ func (graph *Graph) Create(layerData archive.Archive, container *Container, comm
131 131
 
132 132
 // Register imports a pre-existing image into the graph.
133 133
 // FIXME: pass img as first argument
134
-func (graph *Graph) Register(jsonData []byte, layerData archive.Archive, img *Image) error {
134
+func (graph *Graph) Register(jsonData []byte, layerData archive.Archive, img *Image) (err error) {
135
+	defer func() {
136
+		// If any error occurs, remove the new dir from the driver.
137
+		// Don't check for errors since the dir might not have been created.
138
+		// FIXME: this leaves a possible race condition.
139
+		if err != nil {
140
+			graph.driver.Remove(img.ID)
141
+		}
142
+	}()
135 143
 	if err := ValidateID(img.ID); err != nil {
136 144
 		return err
137 145
 	}
... ...
@@ -147,6 +155,12 @@ func (graph *Graph) Register(jsonData []byte, layerData archive.Archive, img *Im
147 147
 		return err
148 148
 	}
149 149
 
150
+	// If the driver has this ID but the graph doesn't, remove it from the driver to start fresh.
151
+	// (the graph is the source of truth).
152
+	// Ignore errors, since we don't know if the driver correctly returns ErrNotExist.
153
+	// (FIXME: make that mandatory for drivers).
154
+	graph.driver.Remove(img.ID)
155
+
150 156
 	tmp, err := graph.Mktemp("")
151 157
 	defer os.RemoveAll(tmp)
152 158
 	if err != nil {