Skip to content

selector_events.BaseSelectorEventLoop.sock_connect should preserve socket type #77859

Closed

Description

@sebastienbourdeauducq
BPO 33678
Nosy @taleinat, @asvetlov, @1st1, @csabella, @twisteroidambassador, @augustogoulart
PRs
  • bpo-35545: Fix mishandling of scoped IPv6 addresses #11403
  • bpo-35545: Fix mishandling of scoped IPv6 addresses #11403
  • Files
  • test.py
  • issue33678.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2018-05-29.09:43:27.288>
    labels = ['type-bug', '3.7', 'expert-asyncio']
    title = 'selector_events.BaseSelectorEventLoop.sock_connect should preserve socket type'
    updated_at = <Date 2020-05-26.10:23:45.487>
    user = 'https://bugs.python.org/sebastienbourdeauducq'

    bugs.python.org fields:

    activity = <Date 2020-05-26.10:23:45.487>
    actor = 'cheryl.sabella'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['asyncio']
    creation = <Date 2018-05-29.09:43:27.288>
    creator = 'sebastien.bourdeauducq'
    dependencies = []
    files = ['47714', '47913']
    hgrepos = []
    issue_num = 33678
    keywords = ['patch']
    message_count = 7.0
    messages = ['318001', '318002', '318047', '322429', '329399', '332405', '369967']
    nosy_count = 8.0
    nosy_names = ['taleinat', 'asvetlov', 'yselivanov', 'sebastien.bourdeauducq', 'apatrushev', 'cheryl.sabella', 'twisteroid ambassador', 'augustogoulart']
    pr_nums = ['11403', '11403']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33678'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    Activity

    1. sebastienbourdeauducq commented on May 29, 2018

      sebastienbourdeauducqmannequin
      MannequinAuthor

      The current behavior causes an exception to be raised when trying to create a datagram socket and _ipaddr_info returns None (since asyncio then calls loop.getaddrinfo with SOCK_STREAM and IPPROTO_UDP).

      Preserving socket type is made difficult by https://bugs.python.org/issue21327; the proposed patch includes a dirty workaround. I do not know yet if it works on Windows.

      --- selector_events.py  2018-05-11 10:01:42.011164837 +0800
      +++ selector_events.py.new      2018-05-29 17:41:03.103155480 +0800
      @@ -439,8 +439,9 @@
                   raise ValueError("the socket must be non-blocking")
       
               if not hasattr(socket, 'AF_UNIX') or sock.family != socket.AF_UNIX:
      +            socktype = sock.type & 0xf  # WA https://bugs.python.org/issue21327
                   resolved = base_events._ensure_resolved(
      -                address, family=sock.family, proto=sock.proto, loop=self)
      +                address, family=sock.family, type=socktype, proto=sock.proto, loop=self)
                   if not resolved.done():
                       yield from resolved
                   _, _, _, _, address = resolved.result()[0]
    2. sebastienbourdeauducq commented on May 29, 2018

      sebastienbourdeauducqmannequin
      MannequinAuthor

      Windows looks fine.

    3. 1st1 commented on May 29, 2018

      @1st1
      Member

      Can you write a unittest that exposes the bug?

    4. apatrushev commented on Jul 26, 2018

      apatrushevmannequin
      Mannequin

      I created small script showing the error.

    5. augustogoulart commented on Nov 7, 2018

      augustogoulartmannequin
      Mannequin

      This error seems to come from base_events.py, lines 142-145, and since it's an OS specific limitation, I can't see Sebastien's suggestion as a feasible solution.
      Maybe we could come up with something along the lines of the following patch. What do you think about it?

      diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
      index 3726c556d4..15ee4d9d1b 100644
      --- a/Lib/asyncio/base_events.py
      +++ b/Lib/asyncio/base_events.py
      @@ -142,6 +142,9 @@ def _ipaddr_info(host, port, family, type, proto):
           if '%' in host:
               # Linux's inet_pton doesn't accept an IPv6 zone index after host,
               # like '::1%lo0'.
      +        if sys.platform.startswith('linux'):
      +            return OSError("Linux's inet_pton doesn't accept an IPv6 "
      +                           "zone index after host")
               return None
       
           for af in afs:
      diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py
      index 6d544d1eda..1b944ff89e 100644
      --- a/Lib/test/test_asyncio/test_base_events.py
      +++ b/Lib/test/test_asyncio/test_base_events.py
      @@ -109,6 +109,9 @@ class BaseEventTests(test_utils.TestCase):
               self.assertIsNone(
                   base_events._ipaddr_info('::3', 1, INET, STREAM, TCP))
       
      +    @unittest.skipIf(sys.platform.startswith('linux'),
      +        "Linux's inet_pton doesn't accept an IPv6 zone index after host")
      +    def test_for(self):
               # IPv6 address with zone index.
               self.assertIsNone(
                   base_events._ipaddr_info('::3%lo0', 1, INET6, STREAM, TCP))
    6. twisteroidambassador commented on Dec 24, 2018

      twisteroidambassadormannequin
      Mannequin

      Looks like this bug is also cause by using _ensure_resolved() more than once for a given host+port, so it can probably be fixed together with https://bugs.python.org/issue35545 .

      Masking sock.type should not be necessary anymore since https://bugs.python.org/issue32331 fixed it.

    7. csabella commented on May 26, 2020

      @csabella
      Contributor

      For reference, please note the comments in #11403 (comment).

    8. transferred this issue fromon Apr 10, 2022
    9. kumaraditya303 commented on Sep 16, 2026

      @kumaraditya303
      Contributor

      Fixed by #31499

    10. moved this from Todo to Done in asyncioon Sep 16, 2026
    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

      Projects

      • Status
        Done

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions