diff mbox

[v2] KVM Test: kvm_monitor.py: Close socket explicitly if exception raised in __init__

Message ID 1308812417-566-1-git-send-email-qzhou@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

qzhou@redhat.com June 23, 2011, 7 a.m. UTC
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>
---
 client/virt/kvm_monitor.py |   18 +++++++++++++-----
 1 files changed, 13 insertions(+), 5 deletions(-)

Comments

Lucas Meneghel Rodrigues June 24, 2011, 6:55 p.m. UTC | #1
LGTM, applied, thanks!

http://autotest.kernel.org/changeset/5450

On Thu, Jun 23, 2011 at 4:00 AM, Qingtang Zhou <qzhou@redhat.com> wrote:
> 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>
> ---
>  client/virt/kvm_monitor.py |   18 +++++++++++++-----
>  1 files changed, 13 insertions(+), 5 deletions(-)
>
> diff --git a/client/virt/kvm_monitor.py b/client/virt/kvm_monitor.py
> index aff716a..3980da8 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:
> --
> 1.7.4.1
>
> _______________________________________________
> Autotest mailing list
> Autotest@test.kernel.org
> http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
>
diff mbox

Patch

diff --git a/client/virt/kvm_monitor.py b/client/virt/kvm_monitor.py
index aff716a..3980da8 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: