8000 Allow getaddrinfo as a fallback in connect_socket when inet_pton can't parse addr or is unavailable by ajdavis · Pull Request #357 · python/asyncio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Allow getaddrinfo as a fallback in connect_socket when inet_pton can't parse addr or is unavailable #357

Closed

Conversation

ajdavis
Copy link
@ajdavis ajdavis commented Jun 3, 2016

This is an approach to http://bugs.python.org/issue27136. It allows getaddrinfo for addresses we can't parse, like the Bluetooth address in that bug report. My theory is that, since getaddrinfo no longer has a mutex on popular platforms, it's ok to call it to confirm that an address has been resolved already, in cases where we can't confirm it in asyncio.

The BaseSelectorEventLoopTests pass with Python 3.4 and 3.5 on Mac, Linux, and Windows; other tests would need to be updated before this can be merged.

Based on @1st1's suggestions: credit is due to him, but blame mistakes on me.

return self.run_in_executor(None, socket.getaddrinfo,
host, port, family, type, proto, flags)

def _ensure_resolved(self, host, port, *,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this a top-level function in base_events.py.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@ajdavis
Copy link
Author
ajdavis commented Jun 4, 2016

Tests pass, but there's a ResourceWarning on Windows unclosed transport <asyncio.sslproto._SSLProtocolTransport> in ProactorEventLoopTests.test_create_server_ssl_verify_failed that I'm having trouble diagnosing.

Nevertheless, this is ready for review.

Here's the general idea:

  • loop.create_connection resolves the host
  • The resolved address is passed to sock_connect
  • Once upon a time, sock_connect called getaddrinfo to check that the host was resolved (in case it was called via a different path)
  • Then, I updated sock_connect to parse the host, verify it's resolved, otherwise assert
  • But we discovered (issue 27136) that parsing all hosts is too hard
  • So here, I fall back to using getaddrinfo if we can't parse the host, getting the best of both previous versions

Since no major platform locks around getaddrinfo any more (issue 26406 and issue 25924), the original motivation for avoiding getaddrinfo is less strong now.

@1st1
Copy link
Member
1st1 commented Jun 6, 2016

Thanks, Jesse, a few notes:

  1. Let's not use ipaddress module in _ipaddr_info.
  2. Add if not hasattr(socket, 'inet_pton'): return at the top of _ipaddr_info
  3. We no longer need lru_cache for _ipaddr_info
  4. What about loop.create_server -- does it need to be updated?

If inet_pton is unavailable, go straight to getaddrinfo without other
logic to transform host info into a getaddrinfo result. Also remove
the LRU cache around _ipaddr_info.
# "host" is wrong IP version for "family".
return None
if af == socket.AF_INET6:
socket.inet_pton(af, host.partition('%')[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also just return None if there is a % character in the host. The zone index should somehow influence IPv6 scope_id, something we can't do here.

@1st1
Copy link
Member
1st1 commented Jun 7, 2016

Thanks, Jesse. Left a couple more comments. Also, the Windows build is failing.

@ajdavis
Copy link
Author
ajdavis commented Jun 7, 2016

@1st1 I think I've fixed everything you commented on? I'm proposing this patch as final.

@1st1
Copy link
Member
1st1 commented Jun 8, 2016

Pushed in ed17848. See also 88c0ba6 and b4e5769.

Hope that we've finally figured this out :) Thanks, Jesse.

@1st1 1st1 closed this Jun 8, 2016
@ajdavis
Copy link
Author
ajdavis commented Jun 8, 2016

Thanks Yury! I appreciate learning and contributing with you.

On Wednesday, June 8, 2016, Yury Selivanov notifications@github.com wrote:

Closed #357 #357.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#357 (comment), or mute the
thread
https://github.com/notifications/unsubscribe/AAFIhepwNWO7kFT8Bj-xyOdNQEFn5J3Oks5qJucxgaJpZM4ItGx7
.

@ajdavis ajdavis changed the title WIP: Begin to allow getaddrinfo as a fallback in connect_socket. Allow getaddrinfo as a fallback in connect_socket when inet_pton can't parse addr or is unavailable Jun 8, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0