From 9b81498be8360010414d7d1555ccd428d92cf84b Mon Sep 17 00:00:00 2001 From: Vitaliy Mysak Date: Tue, 9 Jul 2019 20:56:14 +0000 Subject: [PATCH] rpc.py: check if port is None during client init RPC client constructor accepts optional port, but does not check if it's None in case of remote connection. This patch adds the check and new error message related to it. Case where port is unexpectedly None can be reproduced by starting spdkcli when SPDK socket does not exist. Change-Id: I46e0b99547204c6fdeac421e5de9d6991387e207 Signed-off-by: Vitaliy Mysak Reviewed-on: https://review.gerrithub.io/c/spdk/spdk/+/460974 Tested-by: SPDK CI Jenkins Reviewed-by: Darek Stojaczyk Reviewed-by: Karol Latecki Reviewed-by: Shuhei Matsumoto --- scripts/rpc/client.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/scripts/rpc/client.py b/scripts/rpc/client.py index 41395a5c4..6861f4d29 100644 --- a/scripts/rpc/client.py +++ b/scripts/rpc/client.py @@ -38,16 +38,19 @@ class JSONRPCClient(object): self._logger.debug("Trying to connect to UNIX socket: %s", addr) self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self.sock.connect(addr) - elif ':' in addr: - self._logger.debug("Trying to connect to IPv6 address addr:%s, port:%i", addr, port) - for res in socket.getaddrinfo(addr, port, socket.AF_INET6, socket.SOCK_STREAM, socket.SOL_TCP): - af, socktype, proto, canonname, sa = res - self.sock = socket.socket(af, socktype, proto) - self.sock.connect(sa) + elif port: + if ':' in addr: + self._logger.debug("Trying to connect to IPv6 address addr:%s, port:%i", addr, port) + for res in socket.getaddrinfo(addr, port, socket.AF_INET6, socket.SOCK_STREAM, socket.SOL_TCP): + af, socktype, proto, canonname, sa = res + self.sock = socket.socket(af, socktype, proto) + self.sock.connect(sa) + else: + self._logger.debug("Trying to connect to IPv4 address addr:%s, port:%i'", addr, port) + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.sock.connect((addr, port)) else: - self._logger.debug("Trying to connect to IPv4 address addr:%s, port:%i'", addr, port) - self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - self.sock.connect((addr, port)) + raise socket.error("Unix socket '%s' does not exist" % addr) except socket.error as ex: raise JSONRPCException("Error while connecting to %s\n" "Error details: %s" % (addr, ex))