Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connection: fix logging of non-IP sockets #279

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

avelanarius
Copy link

Before this fix, the debug log would crash _connect_socket for UNIX domain sockets. getsockname() for UNIX domain sockets returns a single string instead of a tuple (as is the case for IPv4/IPv6). Therefore the code could crash as it tried to get the second element of a non-tuple (empty string):

Traceback (most recent call last):
  File "/home/margdoc/Workspace/scylla/maintenance_mode_testing.py", line 5, in <module>
    s = c.connect()
      ^^^^^^^^^^^
  File "cassandra/cluster.py", line 1750, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1776, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 1763, in cassandra.cluster.Cluster.connect
  File "cassandra/cluster.py", line 3581, in cassandra.cluster.ControlConnection.connect
  File "cassandra/cluster.py", line 3642, in cassandra.cluster.ControlConnection._reconnect_internal
cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'test_socket': IndexError('string index out of range')})

Fix the issue by not unpacking those values and just printing them as-is, relying on %s formatter to print all elements of a tuple (host, port) for IP sockets and string for UNIX domain sockets. The printed log is not formatted as nice as before, however this is a DEBUG print so few users will ever see it. The new approach should work with any format of getsockname().

Fixes #278

Before this fix, the debug log would crash _connect_socket for UNIX 
domain sockets. getsockname() for UNIX domain sockets returns a single 
string instead of a tuple (as is the case for IPv4/IPv6). Therefore the 
code could crash as it tried to get the second element of a non-tuple 
(empty string):

  Traceback (most recent call last):
    File "/home/margdoc/Workspace/scylla/maintenance_mode_testing.py", line 5, in <module>
      s = c.connect()
        ^^^^^^^^^^^
    File "cassandra/cluster.py", line 1750, in cassandra.cluster.Cluster.connect
    File "cassandra/cluster.py", line 1776, in cassandra.cluster.Cluster.connect
    File "cassandra/cluster.py", line 1763, in cassandra.cluster.Cluster.connect
    File "cassandra/cluster.py", line 3581, in cassandra.cluster.ControlConnection.connect
    File "cassandra/cluster.py", line 3642, in cassandra.cluster.ControlConnection._reconnect_internal
  cassandra.cluster.NoHostAvailable: ('Unable to connect to any servers', {'test_socket': IndexError('string index out of range')})

Fix the issue by not unpacking those values and just printing them
as-is, relying on %s formatter to print all elements of a tuple (host,
port) for IP sockets and string for UNIX domain sockets. The printed log
is not formatted as nice as before, however this is a DEBUG print 
so few users will ever see it. The new approach should work with any 
format of getsockname().

Fixes scylladb#278
@avelanarius avelanarius requested a review from Lorak-mmk December 8, 2023 15:47
@mykaul
Copy link

mykaul commented Dec 8, 2023

Do we have tests for Unix sockets?

@avelanarius
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Connecting to Unix domain socket fails
3 participants