supabase-py icon indicating copy to clipboard operation
supabase-py copied to clipboard

httpx.ReadTimeout: The read operation timed out

Open ak4zh opened this issue 3 years ago • 12 comments
trafficstars

Describe the bug I have 375000 items in my table. I am doing a loop to obtain all id of all items, with API limit set to 20000 items per api call. After 200000 I always start to get httpx.ReadTimeout: The read operation timed out sometime it may reach 240000 but never go ahead.

  • I have tried to have different wait time after each loop.
  • I have tried to change api limit to 10000 as well as increase it to 30000 or 50000 make less calls but in all cases it get's stuck at around 150000 or 200000.
    existing_search_result = supabase.table('vehicles').select('ref_id', count='exact').order('id', desc=False).execute()
    existing_items = []
    range_step = len(existing_search_result.data)
    total_existing_items = existing_search_result.count
    print(total_existing_items)
    while len(existing_items) < total_existing_items:
        try:
            existing_items += (
                supabase.table(
                    'vehicles'
                ).select('ref_id')
                .order('id', desc=False)
                .range(range_start, range_start + range_step)
                .execute()
            ).data
            range_start += range_step
        except Exception as e:
            logging.exception(e)
        print(range_start, len(existing_items))
        time.sleep(0.30)

Error log

2022-10-23 21:04:14,168:ERROR - The read operation timed out
Traceback (most recent call last):
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_exceptions.py", line 8, in map_exceptions
    yield
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/backends/sync.py", line 26, in read
    return self._sock.recv(max_bytes)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/ssl.py", line 1226, in recv
    return self.read(buflen)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/ssl.py", line 1101, in read
    return self._sslobj.read(len)
socket.timeout: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions
    yield
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_transports/default.py", line 204, in handle_request
    resp = self._pool.handle_request(req)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_sync/connection_pool.py", line 253, in handle_request
    raise exc
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_sync/connection_pool.py", line 237, in handle_request
    response = connection.handle_request(request)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_sync/connection.py", line 90, in handle_request
    return self._connection.handle_request(request)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_sync/http11.py", line 102, in handle_request
    raise exc
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_sync/http11.py", line 81, in handle_request
    ) = self._receive_response_headers(**kwargs)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_sync/http11.py", line 143, in _receive_response_headers
    event = self._receive_event(timeout=timeout)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_sync/http11.py", line 172, in _receive_event
    data = self._network_stream.read(
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/backends/sync.py", line 26, in read
    return self._sock.recv(max_bytes)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/contextlib.py", line 135, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpcore/_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc)
httpcore.ReadTimeout: The read operation timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/ak4zh/updater/main.py", line 278, in job
    supabase.table(
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/postgrest/_sync/request_builder.py", line 53, in execute
    r = self.session.request(
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_client.py", line 802, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_client.py", line 889, in send
    response = self._send_handling_auth(
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_client.py", line 917, in _send_handling_auth
    response = self._send_handling_redirects(
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_client.py", line 954, in _send_handling_redirects
    response = self._send_single_request(request)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_client.py", line 990, in _send_single_request
    response = transport.handle_request(request)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_transports/default.py", line 204, in handle_request
    resp = self._pool.handle_request(req)
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/contextlib.py", line 135, in __exit__
    self.gen.throw(type, value, traceback)
  File "/Users/ak4zh/updater/venv/lib/python3.9/site-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadTimeout: The read operation timed out

ak4zh avatar Oct 23 '22 15:10 ak4zh

httpx by default uses timeout=None so there's no timeout for how long a request takes, and since this is looping, does supabase drop your connection request? Nonetheless this is interesting, would love to fix it though

What else have you tried? Have you tried doing less items per call?

rawandahmad698 avatar Oct 23 '22 20:10 rawandahmad698

@rawandahmad698 As mentioned above I have tried with less items per call as well as more items. I have tried many combinations from 5000 per api call to 50000 per api call.

I have also tried different variations of wait time between loops from 0.10 seconds to 3 seconds wait time between each loop.

ak4zh avatar Oct 24 '22 05:10 ak4zh

I noticed something interesting. If I use id to paginate it works better without timeout. The timeout issue only occurs with range feature.

This query worked in first go without any timeout

    existing_search_result = supabase.table('vehicles').select('id', 'ref_id', count='exact').order('id', desc=False).execute()
    min_id = existing_search_result.data[-1]['id']
    existing_items = existing_search_result.data
    total_existing_items = existing_search_result.count
    while len(existing_items) < total_existing_items:
        try:
            existing_items += (
                supabase.table(
                    'vehicles'
                ).select('id', 'ref_id')
                .order('id', desc=False)
                .gte('id', min_id)
                .execute()
            ).data
            min_id = existing_items[-1]['id']
        except Exception as e:
            logging.exception(e)
        time.sleep(0.30)

ak4zh avatar Oct 24 '22 05:10 ak4zh

I'm also running some tests with the range filter, thanks for opening this issue.

rawandahmad698 avatar Oct 24 '22 20:10 rawandahmad698

I am also getting the httpx.ReadTimeout: The read operation timed out error. But mine happens with a relatively small table (14 records). I am upserting new data and deleting old data with a simple function:


# prepare new data
result = df.round(1).to_json(orient="records")
parsed = json.loads(result)
# pprint(parsed)

# upsert fresh data
data = client.table(table_name).upsert(parsed).execute()

# delete any rows that are not in the new data
res = client.table(table_name).select("*").execute()
df = pd.json_normalize(res.data)
ids = df[id_col].unique()
for i in ids:
    if i not in df[id_col].unique():
        client.from_(table_name).delete().match({id_col: i}).execute()

My error log looks similar to the OP's:


Traceback (most recent call last):
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_exceptions.py", line 8, in map_exceptions
    yield
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/backends/sync.py", line 26, in read
    return self._sock.recv(max_bytes)
  File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1259, in recv
    return self.read(buflen)
  File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/ssl.py", line 1132, in read
    return self._sslobj.read(len)
TimeoutError: The read operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 60, in map_httpcore_exceptions
    yield
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 218, in handle_request
    resp = self._pool.handle_request(req)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 253, in handle_request
    raise exc
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_sync/connection_pool.py", line 237, in handle_request
    response = connection.handle_request(request)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_sync/connection.py", line 90, in handle_request
    return self._connection.handle_request(request)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 105, in handle_request
    raise exc
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 84, in handle_request
    ) = self._receive_response_headers(**kwargs)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 148, in _receive_response_headers
    event = self._receive_event(timeout=timeout)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_sync/http11.py", line 177, in _receive_event
    data = self._network_stream.read(
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/backends/sync.py", line 24, in read
    with map_exceptions(exc_map):
  File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpcore/_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc)
httpcore.ReadTimeout: The read operation timed out

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/xxx/xxx/xxx/xxx/store.py", line 166, in <module>
    store_port()
  File "/Users/xxx/xxx/xxx/xxx/store.py", line 48, in store_port
    sp.upsert_df_to_supabase(df=df_out, id_col="symbol", table_name="holdings")
  File "/Users/xxx/xxx/xxx/xxx/cio_data/common/supabase_model.py", line 74, in upsert_df_to_supabase
    data = client.table(table_name).upsert(parsed).execute()
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/postgrest/_sync/request_builder.py", line 53, in execute
    r = self.session.request(
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_client.py", line 815, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_client.py", line 902, in send
    response = self._send_handling_auth(
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_client.py", line 930, in _send_handling_auth
    response = self._send_handling_redirects(
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_client.py", line 967, in _send_handling_redirects
    response = self._send_single_request(request)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_client.py", line 1003, in _send_single_request
    response = transport.handle_request(request)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 217, in handle_request
    with map_httpcore_exceptions():
  File "/opt/homebrew/Cellar/[email protected]/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/contextlib.py", line 153, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/xxx/xxx/xxx/xxx/.venv/lib/python3.10/site-packages/httpx/_transports/default.py", line 77, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ReadTimeout: The read operation timed out

EDIT:

Restarting the project no longer works to prevent this error.

e-gons avatar Oct 27 '22 16:10 e-gons

@rawandahmad698 are you sure the timeout setting is None? It looks like it is set to 5.

In supabase-py/client.py:

        timeout: Union[int, float, Timeout] = DEFAULT_POSTGREST_CLIENT_TIMEOUT,

From postgrest-py/constants.py:

DEFAULT_POSTGREST_CLIENT_TIMEOUT = 5

e-gons avatar Oct 28 '22 02:10 e-gons

I increased timeout to fix it:

from supabase import create_client, Client
from supabase.lib.client_options import ClientOptions

supabase_url = os.environ.get('SUPABASE_URL')
supabase_key = os.environ.get('SUPABASE_KEY')

client_options = ClientOptions(timeout=60)
supabase: Client = create_client(supabase_url, supabase_key, options=client_options)

Maybe it could help you.

ximo1984 avatar Nov 22 '22 07:11 ximo1984

Need to add timeout=options.timeout to this line:

https://github.com/supabase-community/supabase-py/blob/develop/supabase/client.py#L91

starascendin avatar Feb 02 '23 19:02 starascendin

As @starascendin pointed out, the timeout option is currently being ignored in the client. Please add the timeout parameter when calling _init_postgrest_client().

dschnabel avatar Feb 10 '23 11:02 dschnabel

Hey team,

Thanks for flagging this! I've added the timeout option and will test it when a slot frees up - in the meantime, if anyone is readily set up to do this could I trouble you to see if configuring the timeout fixes the issue?

Thanks! Jo

J0 avatar Feb 11 '23 02:02 J0

Hm, I pulled the latest develop (commit https://github.com/supabase-community/supabase-py/commit/abd1abb0eff990ce0749e40903a2010aa6dde905) and tested it like so:

supabase: Client = create_client(project_url, secret, options=ClientOptions(timeout=1))
supabase.auth.sign_up({"email": email, "password": password})

The sign_up() call takes longer than 1 second, however I'm not getting a timeout. Either I'm not setting it correctly or it's still not working yet.

dschnabel avatar Feb 14 '23 10:02 dschnabel

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar Apr 30 '24 00:04 github-actions[bot]