Browse code

layer/layer_store: ensure NewInputTarStream resources are released

In applyTar, if the driver's ApplyDiff returns an error, the function
returns early without calling io.Copy.

As a consequence, the resources (a goroutine and some buffers holding
the uncompressed image, the digest, etc...) allocated or referenced by
NewInputTarStream above aren't released, as the worker goroutine only
finishes when it finds EOF or a closed pipe.

Signed-off-by: Sergio Lopez <slp@redhat.com>

Sergio Lopez authored on 2018/12/21 17:30:09
Showing 1 changed files
... ...
@@ -253,13 +253,14 @@ func (ls *layerStore) applyTar(tx *fileMetadataTransaction, ts io.Reader, parent
253 253
 	}
254 254
 
255 255
 	applySize, err := ls.driver.ApplyDiff(layer.cacheID, parent, rdr)
256
+	// discard trailing data but ensure metadata is picked up to reconstruct stream
257
+	// unconditionally call io.Copy here before checking err to ensure the resources
258
+	// allocated by NewInputTarStream above are always released
259
+	io.Copy(ioutil.Discard, rdr) // ignore error as reader may be closed
256 260
 	if err != nil {
257 261
 		return err
258 262
 	}
259 263
 
260
-	// Discard trailing data but ensure metadata is picked up to reconstruct stream
261
-	io.Copy(ioutil.Discard, rdr) // ignore error as reader may be closed
262
-
263 264
 	layer.size = applySize
264 265
 	layer.diffID = DiffID(digester.Digest())
265 266