Skip to content

Waiting for session indefinitely #1373

Description

@evelinagkougklia

Sometimes the library gets stuck waiting for a session to be obtained, and this goes on for however long until the process is stopped. On restart, everything works normally again.

This is a long-running script so I'm not sure how to reproduce, but the gist of it is that it iterates through a number of mailboxes and handles emails in those mailboxes. The error seems to happen when one mailbox is trying to refresh its session, and a Timeout occurs. The next time the session is requested, it goes into this endless loop of waiting for a session and never receiving it.

[2025-09-07 23:17:32,645] [exchangelib.queryset] [DEBUG] Initializing cache
[2025-09-07 23:17:32,646] [exchangelib.folders.collections] [DEBUG] Finding email@email.com items in folders (Messages(Root(<exchangelib.account.Account object at 0x7b0b7dabe250>, '[self]', 'root', 24, 0, 98, None, 'xxxxxxxx', 'xxxxxxx'), None, None, None, None, None, 'xxxxxxx', None),) (shape: IdOnly, depth: Shallow, additional_fields: None, restriction: datetime_received > EWSDateTime(2025, 9, 7, 22, 9, 20, tzinfo=EWSTimeZone(key='UTC')))
[2025-09-07 23:17:32,647] [exchangelib.services.common] [DEBUG] Getting page at offset 0 (max_items 50)
[2025-09-07 23:17:32,648] [exchangelib.services.common] [DEBUG] Calling service FindItem
[2025-09-07 23:17:32,648] [exchangelib.services.common] [DEBUG] Trying API version Exchange2016
[2025-09-07 23:17:32,648] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Got session immediately
[2025-09-07 23:17:32,648] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Got session 75496
[2025-09-07 23:17:32,725] [urllib3.connectionpool] [DEBUG] https://outlook.office365.com:443 "POST /EWS/Exchange.asmx HTTP/11" 200 None
[2025-09-07 23:17:32,726] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Releasing session 75496
[2025-09-07 23:17:32,726] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: session 75496 usage exceeded limit. Discarding
[2025-09-07 23:17:32,726] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Renewing session 75496
[2025-09-07 23:17:32,733] [urllib3.connectionpool] [DEBUG] Starting new HTTPS connection (1): login.microsoftonline.com:443
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Read timed out. (read timeout=120)
    raise ReadTimeout(e, request=request)
  File "/opt/app/.venv/lib/python3.11/site-packages/requests/adapters.py", line 713, in send
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    r = adapter.send(request, **kwargs)
  File "/opt/app/.venv/lib/python3.11/site-packages/requests/sessions.py", line 703, in send
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    resp = self.send(prep, **send_kwargs)
  File "/opt/app/.venv/lib/python3.11/site-packages/requests/sessions.py", line 589, in request
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return super(OAuth2Session, self).request(
  File "/opt/app/.venv/lib/python3.11/site-packages/requests_oauthlib/oauth2_session.py", line 566, in request
        ^^^^^^^^^^^^^
    r = self.request(
  File "/opt/app/.venv/lib/python3.11/site-packages/requests_oauthlib/oauth2_session.py", line 381, in fetch_token
            ^^^^^^^^^^^^^^^^^^^^
    token = session.fetch_token(
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/protocol.py", line 342, in create_oauth2_session
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    session = self.create_oauth2_session()
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/protocol.py", line 312, in create_session
           ^^^^^^^^^^^^^^^^^^^^^
    return self.create_session()
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/protocol.py", line 287, in renew_session
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
    session = self.renew_session(session)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/protocol.py", line 264, in release_session
    self.protocol.release_session(session)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/services/common.py", line 365, in _get_response
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    r = self._get_response(payload=payload, api_version=api_version)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/services/common.py", line 396, in _get_response_xml
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    response = self._get_response_xml(payload=payload)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/services/common.py", line 784, in _response_generator
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    yield from self._response_generator(payload=payload)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/services/common.py", line 300, in _get_elements
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    page_elems = list(self._get_elements(payload=payload))
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/services/common.py", line 898, in _get_pages
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    pages = self._get_pages(payload_func, kwargs, len(paging_infos))
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/services/common.py", line 801, in _paged_call
    for elem in elems:
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/services/common.py", line 216, in _elems_to_objs
    yield from FindItem(account=self.account, page_size=page_size).call(
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/folders/collections.py", line 211, in find_items
            ^^^^^^^^^^^^^^
    first = next(iterable)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/util.py", line 152, in peek
                      ^^^^^^^^^^^
    is_empty, items = peek(items)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/account.py", line 409, in _consume_item_service
    yield from self._consume_item_service(
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/account.py", line 720, in fetch
    for i in iterable:
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/queryset.py", line 345, in _item_yielder
    yield from self._format_items(items=self._query(), return_format=self.return_format)
  File "/opt/app/.venv/lib/python3.11/site-packages/exchangelib/queryset.py", line 270, in __iter__
  File "pphe_email_processor/core/email_handler.py", line 119, in process_emails
During handling of the above exception, another exception occurred:
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='login.microsoftonline.com', port=443): Read timed out. (read timeout=120)
    raise ReadTimeoutError(
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 369, in _raise_timeout
    self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 468, in _make_request
    raise new_e
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 490, in _make_request
               ^^^^^^^^^^^^^^^^^^^
    response = self._make_request(
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 789, in urlopen
    raise value
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/util/util.py", line 39, in reraise
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    raise reraise(type(error), error, _stacktrace)
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/util/retry.py", line 474, in increment
              ^^^^^^^^^^^^^^^^^^
    retries = retries.increment(
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 843, in urlopen
           ^^^^^^^^^^^^^
    resp = conn.urlopen(
  File "/opt/app/.venv/lib/python3.11/site-packages/requests/adapters.py", line 667, in send
The above exception was the direct cause of the following exception:

TimeoutError: _ssl.c:999: The handshake operation timed out
    self._sslobj.do_handshake()
  File "/usr/local/lib/python3.11/ssl.py", line 1382, in do_handshake
    self.do_handshake()
  File "/usr/local/lib/python3.11/ssl.py", line 1104, in _create
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return self.sslsocket_class._create(
  File "/usr/local/lib/python3.11/ssl.py", line 517, in wrap_socket
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 509, in _ssl_wrap_socket_impl
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls, server_hostname)
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 465, in ssl_wrap_socket
               ^^^^^^^^^^^^^^^^
    ssl_sock = ssl_wrap_socket(
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connection.py", line 805, in _ssl_wrap_socket_and_match_hostname
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    sock_and_verified = _ssl_wrap_socket_and_match_hostname(
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connection.py", line 652, in connect
    conn.connect()
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 1095, in _validate_conn
    self._validate_conn(conn)
  File "/opt/app/.venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 466, in _make_request
Traceback (most recent call last):
... # other calls for other mailboxes succeeding
[2025-09-07 23:20:41,307] [exchangelib.queryset] [DEBUG] Initializing cache
[2025-09-07 23:20:41,308] [exchangelib.folders.collections] [DEBUG] Finding email@email.com items in folders (Messages(Root(<exchangelib.account.Account object at 0x7b0b7dabe250>, '[self]', 'root', 24, 0, 98, None, 'xxxxxxxxxxxxxxxxxxxxxxx', 'xxxxxxxxxxxxxxxxxx'), None, None, None, None, None, 'xxxxxxxxxxxxxxxxx', None),) (shape: IdOnly, depth: Shallow, additional_fields: None, restriction: datetime_received > EWSDateTime(2025, 9, 7, 22, 9, 20, tzinfo=EWSTimeZone(key='UTC')))
[2025-09-07 23:20:41,308] [exchangelib.services.common] [DEBUG] Getting page at offset 0 (max_items 50)
[2025-09-07 23:20:41,309] [exchangelib.services.common] [DEBUG] Calling service FindItem
[2025-09-07 23:20:41,309] [exchangelib.services.common] [DEBUG] Trying API version Exchange2016
[2025-09-07 23:20:41,309] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:21:41,309] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:21:41,310] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:22:41,311] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:22:41,310] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:23:41,311] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:23:41,311] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:24:41,312] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:24:41,312] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:25:41,313] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:25:41,313] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:26:41,314] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:26:41,313] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:27:41,314] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:27:41,314] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:28:41,315] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds
[2025-09-07 23:28:41,315] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: Waiting for session
[2025-09-07 23:29:41,316] [exchangelib.protocol] [DEBUG] Server outlook.office365.com: No sessions available for 60 seconds

Maybe relevant information is that I have added this to the script:

Protocol.MAX_SESSION_USAGE_COUNT = 65

As I had seen a big usage of memory and this was suggested as a way to limit it in a different issue here.

exchangelib: 5.4.2, python: 3.11

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions