Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asyncio.getaddrinfo manifests in misleading DNS timeouts when thread pool executor is saturated #112169

Closed
alex-kowalczyk opened this issue Nov 16, 2023 · 1 comment
Labels
docs Documentation in the Doc dir topic-asyncio

Comments

@alex-kowalczyk
Copy link
Contributor

alex-kowalczyk commented Nov 16, 2023

Bug report

Bug description:

There is no mention in https://docs.python.org/3/library/asyncio-eventloop.html#dns that asyncio.getaddrinfo is just a ThreadPoolExecutor-based wrapper around sync getaddrinfo (base_events.py:860 - BaseEventLoop.getaddrinfo)
This can lead to mysterious "DNS timeouts" or CancelledErrors during DNS resolution if the executor pool is saturated by other, often totally unrelated code, which is especially not uncommon for any app using asgiref's @sync_to_async decorator.

At the minimum, until this function is converted to real async (if feasible), it would be nice if documentation warned about using shared thread pool executor.

Sample stack trace from httpcore => anyio, which uses asyncio.getaddrinfo under the hood, when the thread pool is saturated:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.11/site-packages/httpcore/_backends/anyio.py", line 114, in connect_tcp
    stream: anyio.abc.ByteStream = await anyio.connect_tcp(
                                   ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.11/site-packages/anyio/_core/_sockets.py", line 192, in connect_tcp
    gai_res = await getaddrinfo(
              ^^^^^^^^^^^^^^^^^^
asyncio.exceptions.CancelledError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/app/.venv/lib/python3.11/site-packages/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "/app/.venv/lib/python3.11/site-packages/httpcore/_backends/anyio.py", line 113, in connect_tcp
    with anyio.fail_after(timeout):
  File "/app/.venv/lib/python3.11/site-packages/anyio/_core/_tasks.py", line 119, in __exit__
    raise TimeoutError
TimeoutError

CPython versions tested on:

3.11

Operating systems tested on:

Linux

Linked PRs

@alex-kowalczyk alex-kowalczyk added the type-bug An unexpected behavior, bug, or error label Nov 16, 2023
@github-project-automation github-project-automation bot moved this to Todo in asyncio Nov 16, 2023
@gvanrossum
Copy link
Member

Thanks for the report!

I believe the reasons why we do it this way are as valid as they were in 2015 — Python has no async DNS API, only a synchronous one. I agree that the executor thing ought to be documented. We could also possibly use a separate executor. Can you submit a PR for a doc update?

The other thing needs a little more thought as e.g. how do we control the capacity of that executor.

Your app could also use its own custom executor, of course — it’s worth mentioning that in the docs update.

alex-kowalczyk added a commit to alex-kowalczyk/cpython that referenced this issue Nov 17, 2023
@kumaraditya303 kumaraditya303 added docs Documentation in the Doc dir and removed type-bug An unexpected behavior, bug, or error labels Jun 24, 2024
kumaraditya303 added a commit that referenced this issue Jun 24, 2024
…sage and implications. (#112191)

Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 24, 2024
…utor usage and implications. (pythonGH-112191)

(cherry picked from commit fc297b4)

Co-authored-by: Alek Kowalczyk <alek.kowalczyk@gmail.com>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 24, 2024
…utor usage and implications. (pythonGH-112191)

(cherry picked from commit fc297b4)

Co-authored-by: Alek Kowalczyk <alek.kowalczyk@gmail.com>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Jun 24, 2024
kumaraditya303 added a commit that referenced this issue Jun 24, 2024
…cutor usage and implications. (GH-112191) (#120936)

gh-112169: Documented getaddrinfo/getnameinfo default loop executor usage and implications. (GH-112191)
(cherry picked from commit fc297b4)

Co-authored-by: Alek Kowalczyk <alek.kowalczyk@gmail.com>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
kumaraditya303 added a commit that referenced this issue Jun 24, 2024
…cutor usage and implications. (GH-112191) (#120935)

gh-112169: Documented getaddrinfo/getnameinfo default loop executor usage and implications. (GH-112191)
(cherry picked from commit fc297b4)

Co-authored-by: Alek Kowalczyk <alek.kowalczyk@gmail.com>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
mrahtz pushed a commit to mrahtz/cpython that referenced this issue Jun 30, 2024
…utor usage and implications. (python#112191)

Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
…utor usage and implications. (python#112191)

Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
…utor usage and implications. (python#112191)

Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Co-authored-by: Carol Willing <carolcode@willingconsulting.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-asyncio
Projects
Status: Done
Development

No branches or pull requests

4 participants