summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlmr <lmr@592f7852-d20e-0410-864c-8624ca9c26a4>2011-06-24 18:53:35 +0000
committerlmr <lmr@592f7852-d20e-0410-864c-8624ca9c26a4>2011-06-24 18:53:35 +0000
commit1794fb11b612bb826c72da56d1655d91c328e9b7 (patch)
tree32924a760b998c46420f3f1a5afb32cc0a1fd8d3
parenta2d3c15afba201d2862174dbc4693a8dd841fa6d (diff)
KVM Test: kvm_monitor.py: Close socket explicitly if exception raised in __init__
monitor socket will be initiated at the beginning of '*Monitor.__init__', if exception occur in this function, socket will not be closed correctly. In this case, socket should be closed explicitly. Signed-off-by: Qingtang Zhou <qzhou@redhat.com> git-svn-id: svn://test.kernel.org/autotest/trunk@5450 592f7852-d20e-0410-864c-8624ca9c26a4
-rw-r--r--client/virt/kvm_monitor.py18
1 files changed, 13 insertions, 5 deletions
diff --git a/client/virt/kvm_monitor.py b/client/virt/kvm_monitor.py
index aff716a5..3980da88 100644
--- a/client/virt/kvm_monitor.py
+++ b/client/virt/kvm_monitor.py
@@ -82,11 +82,7 @@ class Monitor:
def __del__(self):
# Automatically close the connection when the instance is garbage
# collected
- try:
- self._socket.shutdown(socket.SHUT_RDWR)
- except socket.error:
- pass
- self._socket.close()
+ self._close_sock()
# The following two functions are defined to make sure the state is set
@@ -106,6 +102,13 @@ class Monitor:
return self.name, self.filename, True
+ def _close_sock(self):
+ try:
+ self._socket.shutdown(socket.SHUT_RDWR)
+ except socket.error:
+ pass
+ self._socket.close()
+
def _acquire_lock(self, timeout=20):
end_time = time.time() + timeout
while time.time() < end_time:
@@ -171,6 +174,7 @@ class HumanMonitor(Monitor):
# Find the initial (qemu) prompt
s, o = self._read_up_to_qemu_prompt(20)
if not s:
+ self._close_sock()
raise MonitorProtocolError("Could not find (qemu) prompt "
"after connecting to monitor. "
"Output so far: %r" % o)
@@ -179,6 +183,7 @@ class HumanMonitor(Monitor):
self._help_str = self.cmd("help", debug=False)
except MonitorError, e:
+ self._close_sock()
if suppress_exceptions:
logging.warn(e)
else:
@@ -427,6 +432,7 @@ class QMPMonitor(Monitor):
try:
json
except NameError:
+ self._close_sock()
raise MonitorNotSupportedError("QMP requires the json module "
"(Python 2.6 and up)")
@@ -441,12 +447,14 @@ class QMPMonitor(Monitor):
break
time.sleep(0.1)
else:
+ self._close_sock()
raise MonitorProtocolError("No QMP greeting message received")
# Issue qmp_capabilities
self.cmd("qmp_capabilities")
except MonitorError, e:
+ self._close_sock()
if suppress_exceptions:
logging.warn(e)
else: