redis / redis-py

Redis Python client
MIT License
12.45k stars 2.48k forks source link

Blocking command `timeout` cannot exceed client's `socket_timeout` #2807

Open attie-argentum opened 1 year ago

attie-argentum commented 1 year ago

Version: What redis-py and what redis version is the issue happening on?

Platform: What platform / version? (For example Python 3.5.1 on Windows 7 / Ubuntu 15.10 / Azure)

Description: Description of your issue, stack traces from errors and code that reproduces the issue It seems that the timeout parameter to various blocking calls (specifically .brpop() in my case) cannot be longer than socket_timeout, otherwise a redis.exceptions.TimeoutError will be raised.

NOTE: My application actually uses redis.asyncio.client.Redis, but this behavior can be reproduced with the sync client too, as shown below.

Am I incorrect in thinking these timeouts should be more independent?... i.e: The socket timeout is "low-level", and the BRPOP timeout is "application-level"... regardless of what happens at the socket layer, the BRPOP timeout requested should be honored.

At present, the "If timeout is 0, then block indefinitely." statement in the docs appears to be untrue, because at some point you'll hit the socket timeout and get an exception that needs to be handled - which can also be demonstrated below by swapping for r.brpop('my_key', timeout=0).

root@a30c563c33a3:/# pip install redis
[...]
root@a30c563c33a3:/# python3
Python 3.10.11 (main, May  4 2023, 06:08:16) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from redis import Redis
>>> r = Redis(host='10.192.0.3', socket_timeout=4)
>>> r.ping()
True
>>> r.brpop('my_key', timeout=8)
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 210, in _read_from_socket
    data = self._sock.recv(socket_read_size)
TimeoutError: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/site-packages/redis/commands/core.py", line 2587, in brpop
    return self.execute_command("BRPOP", *keys)
  File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1269, in execute_command
    return conn.retry.call_with_retry(
  File "/usr/local/lib/python3.10/site-packages/redis/retry.py", line 49, in call_with_retry
    fail(error)
  File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1273, in <lambda>
    lambda error: self._disconnect_raise(conn, error),
  File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1259, in _disconnect_raise
    raise error
  File "/usr/local/lib/python3.10/site-packages/redis/retry.py", line 46, in call_with_retry
    return do()
  File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1270, in <lambda>
    lambda: self._send_command_parse_response(
  File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1246, in _send_command_parse_response
    return self.parse_response(conn, command_name, **options)
  File "/usr/local/lib/python3.10/site-packages/redis/client.py", line 1286, in parse_response
    response = connection.read_response()
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 874, in read_response
    response = self._parser.read_response(disable_decoding=disable_decoding)
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 347, in read_response
    result = self._read_response(disable_decoding=disable_decoding)
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 357, in _read_response
    raw = self._buffer.readline()
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 260, in readline
    self._read_from_socket()
  File "/usr/local/lib/python3.10/site-packages/redis/connection.py", line 223, in _read_from_socket
    raise TimeoutError("Timeout reading from socket")
redis.exceptions.TimeoutError: Timeout reading from socket
github-actions[bot] commented 1 month ago

This issue is marked stale. It will be closed in 30 days if it is not updated.