Skip to content

Instantly share code, notes, and snippets.

@andriy-s
Created February 28, 2015 15:02
Show Gist options
  • Save andriy-s/ac502e1d27ba4f19912d to your computer and use it in GitHub Desktop.
Save andriy-s/ac502e1d27ba4f19912d to your computer and use it in GitHub Desktop.
--- core/server.py.orig 2015-02-28 16:34:33.000000000 +0200
+++ core/server.py 2015-02-28 16:45:24.000000000 +0200
@@ -178,7 +178,12 @@
if not self._address_info or (datetime.now() - self._address_info_resolved_time).seconds > ADDRESS_INFO_REFRESH_TIME:
# converts addresses tuple to list and adds a 6th parameter for availability (None = not checked, True = available, False=not available) and a 7th parameter for the checking time
try:
- self._address_info = [list(address) + [None, None] for address in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_ADDRCONFIG | socket.AI_V4MAPPED)]
+ try:
+ self._address_info = [list(address) + [None, None] for address in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_ADDRCONFIG | socket.AI_V4MAPPED)]
+ except socket.gaierror as e:
+ if e.errno != socket.EAI_BADFLAGS:
+ raise
+ self._address_info = [list(address) + [None, None] for address in socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_ADDRCONFIG)]
self._address_info_resolved_time = datetime.now()
except Exception:
self._address_info = []
@cannatag
Copy link

Thanks, sorry but I've not tested with freeBSD.
I've slightly modified your patch, can you to substitute the address_info() property with the following code in the server.py?

@property
def address_info(self):
        if not self._address_info or (datetime.now() - self._address_info_resolved_time).seconds > ADDRESS_INFO_REFRESH_TIME:
            # converts addresses tuple to list and adds a 6th parameter for availability (None = not checked, True = available, False=not available) and a 7th parameter for the checking time
            addresses = None
            try:
                addresses = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_ADDRCONFIG | socket.AI_V4MAPPED)
            except socket.gaierror:
                pass

            if not addresses:  # if addresses not found or raised an exception (for example for bad flags) tries again without flags
                try:
                    addresses = socket.getaddrinfo(self.host, self.port, socket.AF_UNSPEC, socket.SOCK_STREAM, socket.IPPROTO_TCP)
                except socket.gaierror:
                    pass

            if addresses:
                self._address_info = [list(address) + [None, None] for address in addresses]
                self._address_info_resolved_time = datetime.now()
            else:
                self._address_info = []
                self._address_info_resolved_time = None
        return self._address_info

Let me know if this works. It tries with the flags and if get a gaierror or the addresses list is empty (because of the AI_ADDRCONFIG) it tries without any flags.

Thanks,
Giovanni

@ecederstrand
Copy link

Giovanni,

I ran into the same issue on FreeBSD. Your patch works for me.

Thanks,
Erik

@andriy-s
Copy link
Author

Hello and sorry for late reply.

I've just tested your new implementation of the address_info property, and it appears to work fine for me.

Thanks,
Andriy.

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