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

Fix for Unhandled exception related to self.host with unix socket #2520

Merged
merged 5 commits into from
Dec 25, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* Enable Lock for asyncio cluster mode
* Fix Sentinel.execute_command doesn't execute across the entire sentinel cluster bug (#2458)
* Added a replacement for the default cluster node in the event of failure (#2463)
* Fix for Unhandled exception related to self.host with unix socket (#2496)

* 4.1.3 (Feb 8, 2022)
* Fix flushdb and flushall (#1926)
Expand Down
34 changes: 23 additions & 11 deletions redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -664,20 +664,31 @@ def _connect(self):
raise err
raise OSError("socket.getaddrinfo returned an empty list")

def _host_error(self):
try:
host_error = f"{self.host}:{self.port}"
except AttributeError:
host_error = "connection"

return host_error

def _error_message(self, exception):
# args for socket.error can either be (errno, "message")
# or just "message"

host_error = self._host_error()

if len(exception.args) == 1:
try:
return f"Error connecting to {self.host}:{self.port}. \
return f"Error connecting to {host_error}. \
{exception.args[0]}."
except AttributeError:
return f"Connection Error: {exception.args[0]}"
else:
try:
return (
f"Error {exception.args[0]} connecting to "
f"{self.host}:{self.port}. {exception.args[1]}."
f"{host_error}. {exception.args[1]}."
)
except AttributeError:
return f"Connection Error: {exception.args[0]}"
Expand Down Expand Up @@ -793,29 +804,30 @@ def can_read(self, timeout=0):
sock = self._sock
if not sock:
self.connect()

host_error = self._host_error()

try:
return self._parser.can_read(timeout)
except OSError as e:
self.disconnect()
raise ConnectionError(
f"Error while reading from {self.host}:{self.port}: {e.args}"
)
raise ConnectionError(f"Error while reading from {host_error}: {e.args}")

def read_response(self, disable_decoding=False):
"""Read the response from a previously sent command"""
try:
hosterr = f"{self.host}:{self.port}"
except AttributeError:
hosterr = "connection"

host_error = self._host_error()

try:
response = self._parser.read_response(disable_decoding=disable_decoding)
except socket.timeout:
self.disconnect()
raise TimeoutError(f"Timeout reading from {hosterr}")
raise TimeoutError(f"Timeout reading from {host_error}")
except OSError as e:
self.disconnect()
raise ConnectionError(f"Error while reading from {hosterr}" f" : {e.args}")
raise ConnectionError(
f"Error while reading from {host_error}" f" : {e.args}"
)
except Exception:
self.disconnect()
raise
Expand Down