Always close fds returned by tempfile.mkstemp()
authorNikos Skalkotos <skalkoto@grnet.gr>
Tue, 25 Jun 2013 15:25:51 +0000 (18:25 +0300)
committerNikos Skalkotos <skalkoto@grnet.gr>
Tue, 30 Jul 2013 13:43:58 +0000 (16:43 +0300)
image_creator/bundle_volume.py
image_creator/disk.py

index eec084f..215afb2 100644 (file)
@@ -267,8 +267,11 @@ class BundleVolume(object):
         name = os.path.basename(dev) + "_" + uuid.uuid4().hex
         tablefd, table = tempfile.mkstemp()
         try:
-            size = end - start + 1
-            os.write(tablefd, "0 %d linear %s %d" % (size, dev, start))
+            try:
+                size = end - start + 1
+                os.write(tablefd, "0 %d linear %s %d" % (size, dev, start))
+            finally:
+                os.close(tablefd)
             dmsetup('create', "%sp%d" % (name, num), table)
         finally:
             os.unlink(table)
index 76a53aa..5806d6f 100644 (file)
@@ -174,11 +174,14 @@ class Disk(object):
         snapshot = uuid.uuid4().hex
         tablefd, table = tempfile.mkstemp()
         try:
-            os.write(tablefd, "0 %d snapshot %s %s n 8" %
-                              (int(size), sourcedev, cowdev))
+            try:
+                os.write(tablefd, "0 %d snapshot %s %s n 8" %
+                                  (int(size), sourcedev, cowdev))
+            finally:
+                os.close(tablefd)
+
             dmsetup('create', snapshot, table)
             self._add_cleanup(try_fail_repeat, dmsetup, 'remove', snapshot)
-
         finally:
             os.unlink(table)
         self.out.success('done')