box / box-python-sdk

Box SDK for Python
http://opensource.box.com/box-python-sdk/
Apache License 2.0
417 stars 214 forks source link

Read Timeout #789

Closed ericasaw closed 1 year ago

ericasaw commented 1 year ago

Description of the Issue

After running for ~30 minutes my script failed with a read timeout error. I am trying to run a script that automatically uploads files if they do not already exist on Box. It's a bit unclear to me if this is an error from my chosen method of authentication (CCG--which I thought automatically refreshed tokens) or if it has something to do with how long I was accessing a particular file? In either case, I would ideally like this script to run without any intervention until all of the files are uploaded. What is the best way for me to prevent this read timeout?

Steps to Reproduce

This is the code I am running:

#imports
from boxsdk import Client, CCGAuth
import glob
import numpy as np
import _pickle as pickle

#box authorization
auth = CCGAuth(
  client_id="my_client_id",
  client_secret="my_client_secret",
  user="my_user_id"
)

#generate client
user_client = Client(auth)

#grab the RRISA folders
RRISA_items = user_client.folder(folder_id='179722807767').get_items()

general_folders = []
for item in RRISA_items:
    #get the IDS for indata, outdata, and recipe
    general_folders.append(user_client.folder(folder_id=item.id).get_items())

#get any of the dates with files that have already been uploaded
uploaded = sorted(glob.glob("uploaded_files/*"))
#make a list of civils
done_dates = [fn.split('/')[1].split('.')[0] for fn in uploaded]

#path to files we want uploaded
path = '../../../../../../../Volumes/ExIGRINS5/igplp/outdata/'
#for each date in the outdata RRISA folder
for civil in general_folders[3]:

    #only do all of this if we didnt already upload files for this date
    if civil.name not in done_dates:

        #printing just for bookkeeping
        print(civil.name)

        #get the items in the civil date folder
        civil_folder = user_client.folder(folder_id = civil.id).get_items()

        #a list of all of the files in the civil folder
        names = []
        for file in civil_folder:
            names.append(file.name)

        #a list of all of the local files in the same civil folder
        local_files = glob.glob(path+civil.name+'/*')

        #qa is a folder we don't want to upload
        local_files = [f for f in local_files if 'qa' not in f]

        #find the files that Box is missing
        need = []
        i = 0
        for file in local_files:
            if file.split('/')[12] not in names:
                need.append(i)
            i += 1

        #upload the files Box is missing
        for file in np.array(local_files)[need]:
            uploaded_file = user_client.folder(civil.id).upload(file)

        #dump a a list of the files we uploaded for bookkeeping
        pickle.dump(np.array(local_files)[need], open(f'uploaded_files/{civil.name}.pkl', 'wb'))

and here is the error:

Request "GET https://api.box.com/2.0/folders/182694050800/items" failed with ReadTimeout exception: ReadTimeout(ReadTimeoutError("HTTPSConnectionPool(host='api.box.com', port=443): Read timed out. (read timeout=None)"))
Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 1371, in getresponse
    response.begin()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 319, in begin
    version, status, reason = self._read_status()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 280, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
TimeoutError: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 451, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 357, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.box.com', port=443): Read timed out. (read timeout=None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/Documents/GitHub/superlog/Python_Superlog/v1/upload_rtell.py", line 47, in <module>
    for file in civil_folder:
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/pagination/box_object_collection.py", line 81, in next
    return next(self._all_items)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/pagination/box_object_collection.py", line 87, in _items_generator
    response_object = self._load_next_page()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/pagination/box_object_collection.py", line 124, in _load_next_page
    box_response = self._session.get(self._url, params=params)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 90, in get
    return self.request('GET', url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 134, in request
    response = self._prepare_and_send_request(method, url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 325, in _prepare_and_send_request
    network_response = self._send_request(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 528, in _send_request
    return super()._send_request(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 436, in _send_request
    network_response = self._network_layer.request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/network/default_network.py", line 40, in request
    request_response=self._session.request(method, url, **kwargs),
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/adapters.py", line 532, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.box.com', port=443): Read timed out. (read timeout=None)

Versions Used

Python SDK: 3.3.0 Python: 3.9

antusus commented 1 year ago

Hi, is this timeout happening always after 30 minutes, or happened once? Can you upgrade your SDK to newer version?

ericasaw commented 1 year ago

This read timeout error happened this once, but I just wanted to make sure that there wasnt something I could put in my code that would prevent this error from happening in the future. Overnight my code broke with a "Connection reset by peer" error instead of the read timeout error. I can update to the more recent SDK once what I currently have running finishes!

antusus commented 1 year ago

Latest SDK version should solve the "Connection reset error". The newer SDK improves handling of some of the connection issues. Maybe it will help in this case as well.

ericasaw commented 1 year ago

Good to know! I just updated it, so I'll let you know if the problem persists.

ericasaw commented 1 year ago

Unfortunately I am still getting the "Connection reset by peer" error, which happens after around an hour.

"POST https://upload.box.com/api/2.0/files/content" 401 0
{'Server': 'nginx', 'Date': 'Fri, 06 Jan 2023 18:06:21 GMT', 'Content-Type': 'application/octet-stream; charset=UTF-8', 'Content-Length': '0', 'X-Envoy-Upstream-Service-Time': '85', 'WWW-Authenticate': 'Bearer realm="Service", error="invalid_token", error_description="The access token provided is invalid."', 'X-Box-Original-Ingress-ADC-Host': 'prod-b-traffic-manager-4w6q', 'Strict-Transport-Security': 'max-age=31536000', 'Via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'}
<No content or content unavailable for logging>

Request "POST https://api.box.com/oauth2/token" failed with ConnectionError exception: ConnectionError(ProtocolError('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer')))
Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 1371, in getresponse
    response.begin()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 319, in begin
    version, status, reason = self._read_status()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 280, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 54] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/packages/six.py", line 769, in reraise
    raise value.with_traceback(tb)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 1371, in getresponse
    response.begin()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 319, in begin
    version, status, reason = self._read_status()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 280, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
urllib3.exceptions.ProtocolError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/Documents/GitHub/superlog/Python_Superlog/v1/upload_rtell.py", line 66, in <module>
    uploaded_file = user_client.folder(civil.id).upload(file)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/util/api_call_decorator.py", line 63, in call
    return method(*args, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/object/folder.py", line 388, in upload
    return self.upload_stream(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/util/api_call_decorator.py", line 63, in call
    return method(*args, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/object/folder.py", line 322, in upload_stream
    file_response = self._session.post(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 100, in post
    return self.request('POST', url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 136, in request
    response = self._prepare_and_send_request(method, url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 346, in _prepare_and_send_request
    retry = self._get_retry_request_callable(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 539, in _get_retry_request_callable
    self._renew_session(request.access_token)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 510, in _renew_session
    new_access_token, _ = self._oauth.refresh(access_token_used)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/auth/oauth2.py", line 221, in refresh
    access_token, refresh_token = self._refresh(access_token_to_refresh)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/auth/server_auth.py", line 41, in _refresh
    new_access_token = self.authenticate_user()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/auth/server_auth.py", line 71, in authenticate_user
    return self._authenticate(sub, self.USER_SUBJECT_TYPE)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/auth/server_auth.py", line 123, in _authenticate
    return self._fetch_access_token(subject_id, subject_type, date)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/auth/ccg_auth.py", line 19, in _fetch_access_token
    return self.send_token_request(data, access_token=None, expect_refresh_token=False)[0]
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/auth/oauth2.py", line 349, in send_token_request
    token_response = self._execute_token_request(data, access_token, expect_refresh_token)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/auth/oauth2.py", line 287, in _execute_token_request
    network_response = self._session.request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 136, in request
    response = self._prepare_and_send_request(method, url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 351, in _prepare_and_send_request
    raise raised_exception
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 333, in _prepare_and_send_request
    network_response = self._send_request(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 471, in _send_request
    network_response = self._network_layer.request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/network/default_network.py", line 43, in request
    request_response=self._session.request(method, url, **kwargs),
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/adapters.py", line 501, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(54, 'Connection reset by peer'))
ericasaw commented 1 year ago

I looked at threads #770, #766, and #763 and none of the solutions discussed resolved the issue I am having.

antusus commented 1 year ago

Sorry to hear that. I'll forward this to our Python expert and we will see what we can do.

lukaszsocha2 commented 1 year ago

Hi @ericasaw, the fix for your issue is now released with new boxsdk 3.6.1 release. Can you update your boxsdk to use that version, test it and reopen this issue if the problem still occurs? But I hope that it should not be happening anymore. Best, @lukaszsocha2

ericasaw commented 1 year ago

I updated to 3.6.1 today but still ran into the same Read Timeout issue. This was the traceback:

Request "GET https://api.box.com/2.0/folders/182667593092/items" failed with ReadTimeout exception: ReadTimeout(ReadTimeoutError("HTTPSConnectionPool(host='api.box.com', port=443): Read timed out. (read timeout=None)"))
Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 449, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 444, in _make_request
    httplib_response = conn.getresponse()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 1371, in getresponse
    response.begin()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 319, in begin
    version, status, reason = self._read_status()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 280, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
TimeoutError: [Errno 60] Operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/util/retry.py", line 550, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 451, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/connectionpool.py", line 357, in _raise_timeout
    raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='api.box.com', port=443): Read timed out. (read timeout=None)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/Documents/GitHub/superlog/Python_Superlog/v1/upload_rtell.py", line 47, in <module>
    for file in civil_folder:
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/pagination/box_object_collection.py", line 81, in next
    return next(self._all_items)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/pagination/box_object_collection.py", line 87, in _items_generator
    response_object = self._load_next_page()
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/pagination/box_object_collection.py", line 124, in _load_next_page
    box_response = self._session.get(self._url, params=params)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 92, in get
    return self.request('GET', url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 136, in request
    response = self._prepare_and_send_request(method, url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 333, in _prepare_and_send_request
    network_response = self._send_request(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 565, in _send_request
    return super()._send_request(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 469, in _send_request
    network_response = self._network_layer.request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/network/default_network.py", line 43, in request
    request_response=self._session.request(method, url, **kwargs),
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/adapters.py", line 532, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='api.box.com', port=443): Read timed out. (read timeout=None)

Any other ideas for what could be going on? I also can't seem to figure out how to reopen the issue, so I hope this still notifies!

lukaszsocha2 commented 1 year ago

Hi @ericasaw, you can try to extend read timeout by passing extra_network_parameters to a call, eg.

extra_network_parameters = {'timeout': 5}
folder.upload('file.txt', extra_network_parameters=extra_network_parameters)

The values from extra_network_parameters will be passed directly to a call of a request method from requests library. Details of requests library timeout settings you can find here: https://requests.readthedocs.io/en/latest/user/advanced/#timeouts Let me know if it helped. Best, @lukaszsocha2

ericasaw commented 1 year ago

Hi @lukaszsocha2, I tried this but still ended up with the timeout error occurring faster than it had before. I will try again with the timeout parameter set to None to see if that helps.

ericasaw commented 1 year ago

Hi @lukaszsocha2, After playing with your solution a little bit, I found that it just sort of makes things worse overall. I tried adding the extra network parameters to wherever .get_items() is being used as well which didn't work great. Even setting the timeout to None resulted in a timeout, usually faster than what was occurring previously. I changed all the code back to the way it was originally, but now I am running into a near constant Connection Reset by Peer error, here is the traceback:

"POST https://upload.box.com/api/2.0/files/content" 502 60707
{'Server': 'nginx', 'Date': 'Tue, 24 Jan 2023 18:43:37 GMT', 'Content-Type': 'text/html', 'Content-Length': '60707', 'ETag': '"5ddb8f87-ed23"', 'Via': '1.1 google', 'Alt-Svc': 'h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'}
<No content or content unavailable for logging>

Request "POST https://upload.box.com/api/2.0/files/content" failed with ChunkedEncodingError exception: ChunkedEncodingError(ProtocolError("Connection broken: ConnectionResetError(54, 'Connection reset by peer')", ConnectionResetError(54, 'Connection reset by peer')))
Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/response.py", line 438, in _error_catcher
    yield
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/response.py", line 519, in read
    data = self._fp.read(amt) if not fp_closed else b""
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 462, in read
    n = self.readinto(b)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/http/client.py", line 506, in readinto
    n = self.fp.readinto(b)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/socket.py", line 704, in readinto
    return self._sock.recv_into(b)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1241, in recv_into
    return self.read(nbytes, buffer)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/ssl.py", line 1099, in read
    return self._sslobj.read(len, buffer)
ConnectionResetError: [Errno 54] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/models.py", line 760, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/response.py", line 576, in stream
    data = self.read(amt=amt, decode_content=decode_content)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/response.py", line 541, in read
    raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/contextlib.py", line 137, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/urllib3/response.py", line 455, in _error_catcher
    raise ProtocolError("Connection broken: %r" % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(54, 'Connection reset by peer')", ConnectionResetError(54, 'Connection reset by peer'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/eas5439/Documents/GitHub/superlog/Python_Superlog/v1/upload_rtell.py", line 66, in <module>
    uploaded_file = user_client.folder(civil.id).upload(file)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/util/api_call_decorator.py", line 63, in call
    return method(*args, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/object/folder.py", line 388, in upload
    return self.upload_stream(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/util/api_call_decorator.py", line 63, in call
    return method(*args, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/object/folder.py", line 322, in upload_stream
    file_response = self._session.post(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 100, in post
    return self.request('POST', url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 136, in request
    response = self._prepare_and_send_request(method, url, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 333, in _prepare_and_send_request
    network_response = self._send_request(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 565, in _send_request
    return super()._send_request(request, **kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/session/session.py", line 469, in _send_request
    network_response = self._network_layer.request(
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/boxsdk/network/default_network.py", line 43, in request
    request_response=self._session.request(method, url, **kwargs),
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/sessions.py", line 687, in send
    r.content
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/models.py", line 838, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "/Users/eas5439/opt/miniconda3/envs/photometry/lib/python3.9/site-packages/requests/models.py", line 763, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(54, 'Connection reset by peer')", ConnectionResetError(54, 'Connection reset by peer'))

The "no content or content unavailable for logging" only started popping up more frequently once I started playing with the request timeout, but I'm not sure I fully understand the warning message there.

lukaszsocha2 commented 1 year ago

Hi, unfortunately I cannot reproduce your error and that is why it's very hard for me to fix it. For now I've added Connection broken error to the list of retried errors here: https://github.com/box/box-python-sdk/pull/794. We will release this change soon in 3.6.2 version. Hope this will solve your error with Connection reset by peer, because new connection should be established on retry.

But this pr won't do anything with timeout error. Maybe you can try solution proposed here:

        from urllib3.connection import HTTPConnection

        HTTPConnection.default_socket_options = ( 
            HTTPConnection.default_socket_options + [
            (socket.SOL_SOCKET, socket.SO_SNDBUF, 1000000), #1MB in byte
            (socket.SOL_SOCKET, socket.SO_RCVBUF, 1000000)
        ])

Can you also tell me what operating system you are you using ? Do you have stable network connection? Best, @lukaszsocha2

ericasaw commented 1 year ago

HI @lukaszsocha2, I tried the above solution previously, but I get an error with socket being undefined. When I remove anything that doesn't reference socket, the code doesn't prevent the timeout or connection reset from happening. I am using MacOS Big Sur 11.6.5 on an M1 Mac Mini. The Mac is directly connected to the internet via ethernet cable, so I would hope that the connection is stable. It's worth noting I more often run into the "Connection reset by peer" error rather than the "Connection Timeout" Error. Is there something else I can do to help you troubleshoot further? I'd like to use this uploading method for things in the future, but with the timeout issues that won't be feasible. Thanks! Erica

lukaszsocha2 commented 1 year ago

Hi @ericasaw , can you download most recent sdk code from repo: https://github.com/box/box-python-sdk and use local python sdk files in your code to verify if Connection reset error still occurs after my recent change? You can also try to add Read time out error to the list of retryable errors here: https://github.com/box/box-python-sdk/blob/f1a0aa434369f06e80654a9f5c4b796100881aa6/boxsdk/session/session.py#L341 and see if it helps. Let me know about the result. Best, @lukaszsocha2

lukaszsocha2 commented 1 year ago

Hi @ericasaw, any news from your side? Python SDK 3.6.2 was released. You can try this one to see if connection errors still occur. Best, @lukaszsocha2

ericasaw commented 1 year ago

Hi @lukaszsocha2, Thanks for checking in! I was unable to work last week due to a winter storm, so I just tried implementing the fix through editing session.py. I will let you know how that works out! Thanks, Erica

ericasaw commented 1 year ago

Hi @lukaszsocha2, Using your suggested edit to session.py, I added "Read timed out" to the list on line 341 and this seems to have fixed my issue! Here is an example of the terminal output that leads me to believe it worked. I print out the dates for the folders when they have finished uploading, but you can see that there was a read time out error that refreshed and the script continued!

20201206
20201207
20201208
Request "GET https://api.box.com/2.0/folders/182652936562/items" failed with ReadTimeout exception: ReadTimeout(ReadTimeoutError("HTTPSConnectionPool(host='api.box.com', port=443): Read timed out. (read timeout=None)"))
Request "POST https://upload.box.com/api/2.0/files/content" failed with ReadTimeout exception: ReadTimeout(ReadTimeoutError("HTTPSConnectionPool(host='upload.box.com', port=443): Read timed out. (read timeout=None)"))
20201209
20201212
20201213

I do not seem to be getting the "Connection reset by peer" error anymore (I'm assuming that was fixed in one of the new releases?). Thanks for your help! Erica

lukaszsocha2 commented 1 year ago

Hi @ericasaw, that is a great news. Adding 'Connection reset by peer' exception to the list of retried exceptions was released in 3.6.2 version. We will also consider adding "Read timed out" to this list in next release. If you come across some other error, don't hesitate to open a new issue. Thanks again for cooperation. Best, @lukaszsocha2