[lxc-devel] [lxd/master] Export: Fixes hang in export when invalid --compression argument passed

tomponline on Github lxc-bot at linuxcontainers.org
Tue Apr 28 15:57:50 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 376 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200428/13554cdb/attachment.bin>
-------------- next part --------------
From 5ac84466558c0f48207f0f15807209c758fbf713 Mon Sep 17 00:00:00 2001
From: Thomas Parrott <thomas.parrott at canonical.com>
Date: Tue, 28 Apr 2020 16:56:59 +0100
Subject: [PATCH] lxd/images: Fixes hang in export when invalid --compression
 argument passed

Fixes #7259

Signed-off-by: Thomas Parrott <thomas.parrott at canonical.com>
---
 lxd/images.go | 27 +++++++++++++++++++--------
 1 file changed, 19 insertions(+), 8 deletions(-)

diff --git a/lxd/images.go b/lxd/images.go
index c95f9752cd..c4b43d4be6 100644
--- a/lxd/images.go
+++ b/lxd/images.go
@@ -267,25 +267,36 @@ func imgPostContInfo(d *Daemon, r *http.Request, req api.ImagesPost, op *operati
 		go func() {
 			defer wg.Done()
 			compressErr = compressFile(compress, tarReader, compressWriter)
+
+			// If a compression error occurred, close the writer to end the instance export.
+			if compressErr != nil {
+				imageProgressWriter.Close()
+			}
 		}()
 	} else {
 		imageProgressWriter.WriteCloser = imageFile
 		writer = io.MultiWriter(imageProgressWriter, sha256)
 	}
 
+	// Export instance to writer.
 	err = c.Export(writer, req.Properties)
-	// When compression is used, Close on imageProgressWriter/tarWriter
-	// is required for compressFile/gzip to know it is finished.
-	// Otherwise It is equivalent to imageFile.Close.
+
+	// Clean up file handles.
+	// When compression is used, Close on imageProgressWriter/tarWriter is required for compressFile/gzip to
+	// know it is finished. Otherwise it is equivalent to imageFile.Close.
 	imageProgressWriter.Close()
-	wg.Wait()
-	if err != nil {
-		return nil, err
-	}
+	wg.Wait() // Wait until compression helper has finished if used.
+	imageFile.Close()
+
+	// Check compression errors.
 	if compressErr != nil {
 		return nil, compressErr
 	}
-	imageFile.Close()
+
+	// Check instance export errors.
+	if err != nil {
+		return nil, err
+	}
 
 	fi, err := os.Stat(imageFile.Name())
 	if err != nil {


More information about the lxc-devel mailing list