]> git.ipfire.org Git - thirdparty/dnspython.git/commitdiff
Close extra UDP sockets more quickly.
authorBrian Wellington <bwelling@xbill.org>
Wed, 24 Jun 2020 17:01:13 +0000 (10:01 -0700)
committerBrian Wellington <bwelling@xbill.org>
Wed, 24 Jun 2020 17:01:13 +0000 (10:01 -0700)
If any UDP sockets are left open due to the corresponding TCP ports
being in use, close them.

tests/nanonameserver.py

index 555f9cc3f7a3b5c52a43dbe09faa4e2b583dea70..57de2df93ce86819e27b792f41e964c488a57de2 100644 (file)
@@ -98,29 +98,36 @@ class Server(threading.Thread):
         # We're making the sockets now so they can be sent to by the
         # caller immediately (i.e. no race with the listener starting
         # in the thread).
-        open_udp_sockets = []
-        while True:
-            if self.enable_udp:
-                self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
-                self.udp.bind((self.address, self.port))
-                self.udp_address = self.udp.getsockname()
-            if self.enable_tcp:
-                self.tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
-                self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-                if self.port is 0 and self.enable_udp:
-                    try:
-                        self.tcp.bind((self.address, self.udp_address[1]))
-                    except OSError as e:
-                        if e.errno == errno.EADDRINUSE and \
-                           len(open_udp_sockets) < 100:
-                            open_udp_sockets.append(self.udp)
-                            continue
-                        raise
-                else:
-                    self.tcp.bind((self.address, self.port))
-                self.tcp.listen()
-                self.tcp_address = self.tcp.getsockname()
-            break
+        open_sockets = []
+        try:
+            while True:
+                if self.enable_udp:
+                    self.udp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
+                                             0)
+                    self.udp.bind((self.address, self.port))
+                    self.udp_address = self.udp.getsockname()
+                if self.enable_tcp:
+                    self.tcp = socket.socket(socket.AF_INET,
+                                             socket.SOCK_STREAM, 0)
+                    self.tcp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,
+                                        1)
+                    if self.port is 0 and self.enable_udp:
+                        try:
+                            self.tcp.bind((self.address, self.udp_address[1]))
+                        except OSError as e:
+                            if e.errno == errno.EADDRINUSE and \
+                               len(open_udp_sockets) < 100:
+                                open_udp_sockets.append(self.udp)
+                                continue
+                            raise
+                    else:
+                        self.tcp.bind((self.address, self.port))
+                    self.tcp.listen()
+                    self.tcp_address = self.tcp.getsockname()
+                break
+        finally:
+            for udp_socket in open_udp_sockets:
+                udp_socket.close()
         if self.use_thread:
             self.start()
         return self