diff mbox

[RFC,03/18] qmp.py: Cleanly handle unexpectedly closed socket

Message ID 20180329213857.15499-4-ehabkost@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Eduardo Habkost March 29, 2018, 9:38 p.m. UTC
QEMUMonitorProtocol.cmd() returns None if the socket was closed,
so callers must handle this case explicltly.

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
 scripts/qmp/qmp.py | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Philippe Mathieu-Daudé April 4, 2018, 8:26 a.m. UTC | #1
On 03/29/2018 06:38 PM, Eduardo Habkost wrote:
> QEMUMonitorProtocol.cmd() returns None if the socket was closed,
> so callers must handle this case explicltly.
> 
> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>

Reviewed-by: Philippe Mathieu-Daudé <f4bug@amsat.org>

> ---
>  scripts/qmp/qmp.py | 6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py
> index e9cb6b2683..91b537ea04 100644
> --- a/scripts/qmp/qmp.py
> +++ b/scripts/qmp/qmp.py
> @@ -73,6 +73,8 @@ class QEMUMonitorProtocol(object):
>              raise QMPConnectError
>          # Greeting seems ok, negotiate capabilities
>          resp = self.cmd('qmp_capabilities')
> +        if resp is None:
> +            raise QMPConnectError("QMP connection unexpectedly closed")
>          if "return" in resp:
>              return greeting
>          raise QMPCapabilitiesError
> @@ -182,6 +184,8 @@ class QEMUMonitorProtocol(object):
>          @param name: command name (string)
>          @param args: command arguments (dict)
>          @param cmd_id: command id (dict, list, string or int)
> +        @return QMP response as a Python dict or None if the connection has
> +                been closed
>          """
>          qmp_cmd = {'execute': name}
>          if args:
> @@ -195,6 +199,8 @@ class QEMUMonitorProtocol(object):
>          Build and send a QMP command to the monitor, report errors if any
>          """
>          ret = self.cmd(cmd, kwds)
> +        if ret is None:
> +            raise QMPConnectError("QMP connection unexpectedly closed")
>          if "error" in ret:
>              raise Exception(ret['error']['desc'])
>          return ret['return']
>
diff mbox

Patch

diff --git a/scripts/qmp/qmp.py b/scripts/qmp/qmp.py
index e9cb6b2683..91b537ea04 100644
--- a/scripts/qmp/qmp.py
+++ b/scripts/qmp/qmp.py
@@ -73,6 +73,8 @@  class QEMUMonitorProtocol(object):
             raise QMPConnectError
         # Greeting seems ok, negotiate capabilities
         resp = self.cmd('qmp_capabilities')
+        if resp is None:
+            raise QMPConnectError("QMP connection unexpectedly closed")
         if "return" in resp:
             return greeting
         raise QMPCapabilitiesError
@@ -182,6 +184,8 @@  class QEMUMonitorProtocol(object):
         @param name: command name (string)
         @param args: command arguments (dict)
         @param cmd_id: command id (dict, list, string or int)
+        @return QMP response as a Python dict or None if the connection has
+                been closed
         """
         qmp_cmd = {'execute': name}
         if args:
@@ -195,6 +199,8 @@  class QEMUMonitorProtocol(object):
         Build and send a QMP command to the monitor, report errors if any
         """
         ret = self.cmd(cmd, kwds)
+        if ret is None:
+            raise QMPConnectError("QMP connection unexpectedly closed")
         if "error" in ret:
             raise Exception(ret['error']['desc'])
         return ret['return']