diff --git a/scripts/runqemu b/scripts/runqemu
index dee7710fb3..5f1af740c9 100755
--- a/scripts/runqemu
+++ b/scripts/runqemu
@@ -277,17 +277,17 @@ class BaseConfig(object):
 
     def acquire_portlock(self, lockfile):
         logger.debug("Acquiring lockfile %s..." % lockfile)
+        portlock_descriptor = None
         try:
             portlock_descriptor = open(lockfile, 'w')
-            self.portlocks.update({lockfile: portlock_descriptor})
-            fcntl.flock(self.portlocks[lockfile], fcntl.LOCK_EX|fcntl.LOCK_NB)
+            fcntl.flock(portlock_descriptor, fcntl.LOCK_EX|fcntl.LOCK_NB)
         except Exception as e:
             msg = "Acquiring lockfile %s failed: %s" % (lockfile, e)
             logger.info(msg)
-            if lockfile in self.portlocks.keys() and self.portlocks[lockfile]:
-                self.portlocks[lockfile].close()
-                del self.portlocks[lockfile]
+            if portlock_descriptor:
+                portlock_descriptor.close()
             return False
+        self.portlocks[lockfile] = portlock_descriptor
         return True
 
     def release_portlock(self, lockfile=None):
