Bug 1376372

Summary: spacecmd crashes when cannot connect to remote server
Product: Red Hat Satellite 5 Reporter: Michal Dekan <mdekan>
Component: APIAssignee: Jiří Dostál <jdostal>
Status: CLOSED CURRENTRELEASE QA Contact: Radovan Drazny <rdrazny>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: 570CC: galtukho, jdostal, mdekan, rdrazny, tkasparek, tlestach
Target Milestone: ---Keywords: Patch
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: spacecmd-2.6.14-1 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-06-21 12:15:43 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1358815    

Description Michal Dekan 2016-09-15 09:05:24 UTC
Description of problem:

This bug was reported by the customer into upstream and he also requested us to submit it to RH bugzilla:

Whenever spacecmd cannot connect to a remote server (examples: server is behind firewall, or satellite is stopped so connections are refused), it crashes:

$ spacecmd -s localhost
Welcome to spacecmd, a command-line interface to Spacewalk.

Type: 'help' for a list of commands
      'help <cmd>' for command-specific help
      'quit' to quit

Traceback (most recent call last):
  File "/usr/bin/spacecmd", line 159, in <module>
    if not shell.do_login(''):
  File "/usr/lib/python2.6/site-packages/spacecmd/misc.py", line 271, in do_login
    self.api_version = self.client.api.getVersion()
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1199, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1489, in __request
    verbose=self.__verbose
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1235, in request
    self.send_content(h, request_body)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1349, in send_content
    connection.endheaders()
  File "/usr/lib64/python2.6/httplib.py", line 930, in endheaders
    self._send_output()
  File "/usr/lib64/python2.6/httplib.py", line 802, in _send_output
    self.send(msg)
  File "/usr/lib64/python2.6/httplib.py", line 761, in send
    self.connect()
  File "/usr/lib64/python2.6/httplib.py", line 1134, in connect
    sock = socket.create_connection((self.host, self.port), self.timeout)
  File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
    raise error, msg
socket.error: [Errno 111] Connection refused
$

This is because in do_login() the try/except block around  api.getVersion() only catches exceptions from xmlrpclib. However, if the destination server cannot be contacted, an exception is thrown by socket module, not xmlrpclib.

A suggested patch would be:
$ cat spacecmd-fix-crash-with-no-options-and-disabled-ipv6.patch
--- spacecmd-2.3.0/src/lib/misc.py      2016-08-19 11:11:52.477464000 +0200
+++ spacecmd-2.3.0/src/lib/misc.py      2016-09-06 12:17:40.930450000 +0200
@@ -270,8 +270,9 @@
     try:
         self.api_version = self.client.api.getVersion()
         logging.debug('Server API Version = %s', self.api_version)
-    except xmlrpclib.Fault, e:
+    except:
         if self.options.debug > 0:
+            e = sys.exc_info()[0]
             logging.exception(e)

$

Where are you experiencing the behavior?  What environment?

This is experienced every time spacecmd cannot contact Satellite server. 
$ rpm -q spacecmd
spacecmd-2.3.0-4.el6sat.noarch
$

Version-Release number of selected component (if applicable):

spacecmd-2.3.0-4.el6sat.noarch

How reproducible:


Steps to Reproduce:
1. stop satellite
2. spacecmd -s localhost
3.

Actual results:

spacecmd fails with traceback due to the socket error

Expected results:

spacecmd should failed to connect without traceback.


Additional info:

This bug is a clone of upstream https://github.com/spacewalkproject/spacewalk/pull/454

Comment 2 Jiří Dostál 2016-09-22 10:11:46 UTC
spacewalk.git 52157d03ed2fcff8627ac0bf2e8bf8d0635d7e87

Comment 5 Radovan Drazny 2016-11-25 09:44:51 UTC
Reproduced with spacecmd-2.3.0-5 using the reproducer from the initial report. Stopped satellite server and then: 

# spacecmd -s localhost
Welcome to spacecmd, a command-line interface to Spacewalk.

Type: 'help' for a list of commands
      'help <cmd>' for command-specific help
      'quit' to quit

Traceback (most recent call last):
  File "/usr/bin/spacecmd", line 159, in <module>
    if not shell.do_login(''):
  File "/usr/lib/python2.6/site-packages/spacecmd/misc.py", line 271, in do_login
    self.api_version = self.client.api.getVersion()
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1199, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1489, in __request
    verbose=self.__verbose
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1235, in request
    self.send_content(h, request_body)
  File "/usr/lib64/python2.6/xmlrpclib.py", line 1349, in send_content
    connection.endheaders()
  File "/usr/lib64/python2.6/httplib.py", line 967, in endheaders
    self._send_output()
  File "/usr/lib64/python2.6/httplib.py", line 831, in _send_output
    self.send(msg)
  File "/usr/lib64/python2.6/httplib.py", line 790, in send
    self.connect()
  File "/usr/lib64/python2.6/httplib.py", line 1171, in connect
    sock = socket.create_connection((self.host, self.port), self.timeout)
  File "/usr/lib64/python2.6/socket.py", line 567, in create_connection
    raise error, msg
socket.error: [Errno 111] Connection refused

On latest Satellite 5.8 compose (Satellite-5.8-RHEL-6-20161124.n.0) with spacecmd-2.5.0-5 and stopped satellite server.

# spacecmd -s localhost
Welcome to spacecmd, a command-line interface to Spacewalk.

Type: 'help' for a list of commands
      'help <cmd>' for command-specific help
      'quit' to quit

ERROR: Failed to connect to https://localhost/rpc/api

VERIFIED