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

create_connection and create_server in asyncio didn't work with a Python build with IPv6 disabled #121913

Closed
aisk opened this issue Jul 17, 2024 · 3 comments
Labels
stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@aisk
Copy link
Member

aisk commented Jul 17, 2024

Bug report

Bug description:

Build Python with the configuration --disable-ipv6, and run the test with ./python.exe -m test test_asyncio.test_events -m 'test_create_connection_local_addr_skip_different_family' -v. You will encounter the following error stack:

ERROR: test_create_connection_local_addr_skip_different_family (test.test_asyncio.test_events.SelectEventLoopTests.test_create_connection_local_addr_skip_different_family)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/asaka/Codes/cpython/Lib/asyncio/base_events.py", line 1026, in _connect_sock
    sock.bind(laddr)
OSError: bind(): bad family

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/asaka/Codes/cpython/Lib/test/test_asyncio/test_events.py", line 695, in test_create_connection_local_addr_skip_different_family
    self.loop.run_until_complete(f)
  File "/Users/asaka/Codes/cpython/Lib/asyncio/base_events.py", line 721, in run_until_complete
    return future.result()
  File "/Users/asaka/Codes/cpython/Lib/asyncio/base_events.py", line 1138, in create_connection
    sock = await self._connect_sock(exceptions, addrinfo, laddr_infos)
  File "/Users/asaka/Codes/cpython/Lib/asyncio/base_events.py", line 1032, in _connect_sock
    f'{exc.strerror.lower()}'
AttributeError: 'NoneType' object has no attribute 'lower'

This issue not only affects the test but also user's code.

After some research, I found that it's caused by these two functions, which use getaddrinfo to get a list of connection infos and then attempt to call bind for each of the infos, ignoring OSError until a working one is found.

However, in the iteration code, it attempts to use OSError().strerror to create a new error. Unfortunately, some OSError instances have a strerror field of None. The error raised by sock.bind with AF_INET6 when --disable-ipv6 is used does not have this field, leading to an error that prevents the iteration loop from trying the next potential solution.

There are already reports for the missing strerror / errno fields issues: #109601 and #50720 with a draft PR #109720.

But before we fix this issue, should we provide a workaround for this specific problem, like using str(exc) instead of exc.strerror, or skip this loop if not socket.has_ipv6 and current family is AF_INET6?

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

@kumaraditya303
Copy link
Contributor

Using str(exc) makes sense to me, can you create a PR for it @aisk ?

@aisk
Copy link
Member Author

aisk commented Jul 25, 2024

Sure, working on it.

@kumaraditya303
Copy link
Contributor

Thanks

@github-project-automation github-project-automation bot moved this from Todo to Done in asyncio Jul 25, 2024
kumaraditya303 pushed a commit that referenced this issue Jul 25, 2024
…se_events` (GH-122269) (#122279)

gh-121913: Use str(exc) instead of exc.strerror in `asyncio.base_events` (GH-122269)
(cherry picked from commit 070f1e2)

Co-authored-by: AN Long <[email protected]>
kumaraditya303 pushed a commit that referenced this issue Jul 25, 2024
…se_events` (GH-122269) (#122278)

gh-121913: Use str(exc) instead of exc.strerror in `asyncio.base_events` (GH-122269)
(cherry picked from commit 070f1e2)

Co-authored-by: AN Long <[email protected]>
shadchin added a commit to shadchin/uvloop that referenced this issue Aug 11, 2024
After python/cpython#121913 error message `[errno 98] address already in use`
fantix pushed a commit to MagicStack/uvloop that referenced this issue Aug 13, 2024
After python/cpython#121913 error message `[errno 98] address already in use`
edgarrmondragon pushed a commit to edgarrmondragon/uvloop that referenced this issue Aug 19, 2024
After python/cpython#121913 error message `[errno 98] address already in use`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

2 participants