ecederstrand / exchangelib

Python client for Microsoft Exchange Web Services (EWS)
BSD 2-Clause "Simplified" License
1.18k stars 248 forks source link

ConnectionResetError - connection forcibly closed. #719

Closed curtischolon closed 4 years ago

curtischolon commented 4 years ago

Hi,

I'm running into an issue with previously working code, I was able to successfully connect to and iterate through messages on O365, however suddenly I'm receiving the following error:

requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

The offending code is here, but as I mentioned, it previously worked and has all of a sudden started with this behavior. I did reach through other similar issues raised but did not see any specific resolution.

credentials = Credentials(username=login, password=password)
    config = Configuration(server='outlook.office365.com', credentials=credentials)
    account = Account(
        primary_smtp_address=login,
        config=config,
        autodiscover=False,
        access_type=DELEGATE
        )
    print(account.inbox.all().count())
    for email in account.inbox.all():
        sender = email.sender

Traceback:


EWS https://outlook.office365.com/EWS/Exchange.asmx, account ccholon@oa-services.com: Exception in _get_elements: Traceback (most recent call last):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 331, in _error_catcher
    yield
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 637, in read_chunked
    self._update_chunk_length()
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 569, in _update_chunk_length
    line = self._fp.fp.readline()
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1049, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 908, in read
    return self._sslobj.read(len, buffer)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 749, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 461, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 665, in read_chunked
    self._original_response.close()
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 349, in _error_catcher
    raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 66, in _get_elements
    response = self._get_response_xml(payload=payload)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 131, in _get_response_xml
    stream=self.streaming,
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\util.py", line 742, in post_ratelimited
    _raise_response_errors(r, protocol, log_msg, log_vals)  # Always raises an exception
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\util.py", line 824, in _raise_response_errors
    raise response.headers['TimeoutException']
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\util.py", line 687, in post_ratelimited
    stream=stream)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 559, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 662, in send
    r.content
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 827, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 752, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

Traceback (most recent call last):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 331, in _error_catcher
    yield
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 637, in read_chunked
    self._update_chunk_length()
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 569, in _update_chunk_length
    line = self._fp.fp.readline()
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1049, in recv_into
    return self.read(nbytes, buffer)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 908, in read
    return self._sslobj.read(len, buffer)
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 749, in generate
    for chunk in self.raw.stream(chunk_size, decode_content=True):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 461, in stream
    for line in self.read_chunked(amt, decode_content=decode_content):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 665, in read_chunked
    self._original_response.close()
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\urllib3\response.py", line 349, in _error_catcher
    raise ProtocolError('Connection broken: %r' % e, e)
urllib3.exceptions.ProtocolError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/ccholon/PycharmProjects/beer_distribution/curity_report_download.py", line 63, in <module>
    download_curity_report()
  File "C:/Users/ccholon/PycharmProjects/beer_distribution/curity_report_download.py", line 23, in download_curity_report
    for my_email in account.inbox.all():
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\queryset.py", line 286, in __iter__
    for val in self._format_items(items=self._query(), return_format=self.return_format):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\queryset.py", line 363, in _item_yielder
    for i in iterable:
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\account.py", line 597, in fetch
    shape=ID_ONLY,
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\account.py", line 307, in _consume_item_service
    for i in service_cls(account=self, chunk_size=chunk_size).call(**kwargs):
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 541, in _pool_requests
    elems = r.get()
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\pool.py", line 657, in get
    raise self._value
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\multiprocessing\pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 520, in <lambda>
    lambda c: self._get_elements(payload=payload_func(c, **kwargs)),
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 66, in _get_elements
    response = self._get_response_xml(payload=payload)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\services\common.py", line 131, in _get_response_xml
    stream=self.streaming,
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\util.py", line 742, in post_ratelimited
    _raise_response_errors(r, protocol, log_msg, log_vals)  # Always raises an exception
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\util.py", line 824, in _raise_response_errors
    raise response.headers['TimeoutException']
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\exchangelib\util.py", line 687, in post_ratelimited
    stream=stream)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 559, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\sessions.py", line 662, in send
    r.content
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 827, in content
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
  File "C:\Users\ccholon\AppData\Local\Programs\Python\Python37-32\lib\site-packages\requests\models.py", line 752, in generate
    raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ("Connection broken: ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None)", ConnectionResetError(10054, 'An existing connection was forcibly closed by the remote host', None, 10054, None))
ecederstrand commented 4 years ago

ConnectionResetError are not uncommon - they are caused by the remote server killing the TCP session for some reason. That can happen for a lot of reasons.

The recommended solution is to either handle this yourself in your own code, or to define a retry policy as described in https://github.com/ecederstrand/exchangelib#setup-and-connecting

curtischolon commented 4 years ago

Thanks, explicitly setting a retry policy worked.