]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128916: Do not set `SO_REUSEPORT` on non-`AF_INET*` sockets (GH-128933)
authorMichał Górny <mgorny@gentoo.org>
Sat, 18 Jan 2025 00:49:16 +0000 (01:49 +0100)
committerGitHub <noreply@github.com>
Sat, 18 Jan 2025 00:49:16 +0000 (16:49 -0800)
* gh-128916: Do not set `SO_REUSEPORT` on non-`AF_INET*` sockets

Do not attempt to set ``SO_REUSEPORT`` on sockets of address familifies other
than ``AF_INET`` and ``AF_INET6``, as it is meaningless with these address
families, and the call with fail with Linux kernel 6.12.9 and newer.

* Apply suggestions from code review

Co-authored-by: Vinay Sajip <vinay_sajip@yahoo.co.uk>
---------

Co-authored-by: Vinay Sajip <vinay_sajip@yahoo.co.uk>
Lib/asyncio/base_events.py
Lib/socket.py
Lib/socketserver.py
Misc/NEWS.d/next/Library/2025-01-17-11-46-16.gh-issue-128916.GEePbO.rst [new file with mode: 0644]

index 6e6e5aaac15cafb3203d45c94acea0396b031455..85018797db33bb23eaa56cf4b8c6408c2b36b310 100644 (file)
@@ -1593,7 +1593,9 @@ class BaseEventLoop(events.AbstractEventLoop):
                     if reuse_address:
                         sock.setsockopt(
                             socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
-                    if reuse_port:
+                    # Since Linux 6.12.9, SO_REUSEPORT is not allowed
+                    # on other address families than AF_INET/AF_INET6.
+                    if reuse_port and af in (socket.AF_INET, socket.AF_INET6):
                         _set_reuseport(sock)
                     if keep_alive:
                         sock.setsockopt(
index be37c24d6174a25c65f10fefbfcbb75c18da27fc..727b0e75f0359553b37cc0641cb10b2da8932842 100644 (file)
@@ -937,7 +937,9 @@ def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False,
                 # Fail later on bind(), for platforms which may not
                 # support this option.
                 pass
-        if reuse_port:
+        # Since Linux 6.12.9, SO_REUSEPORT is not allowed
+        # on other address families than AF_INET/AF_INET6.
+        if reuse_port and family in (AF_INET, AF_INET6):
             sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
         if has_ipv6 and family == AF_INET6:
             if dualstack_ipv6:
index cd028ef1c63b8525897ce9e17648813949fb717f..35b2723de3babe41b72e53bb2c56584628b6ce39 100644 (file)
@@ -468,7 +468,12 @@ class TCPServer(BaseServer):
         """
         if self.allow_reuse_address and hasattr(socket, "SO_REUSEADDR"):
             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
-        if self.allow_reuse_port and hasattr(socket, "SO_REUSEPORT"):
+        # Since Linux 6.12.9, SO_REUSEPORT is not allowed
+        # on other address families than AF_INET/AF_INET6.
+        if (
+            self.allow_reuse_port and hasattr(socket, "SO_REUSEPORT")
+            and self.address_family in (socket.AF_INET, socket.AF_INET6)
+        ):
             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
         self.socket.bind(self.server_address)
         self.server_address = self.socket.getsockname()
diff --git a/Misc/NEWS.d/next/Library/2025-01-17-11-46-16.gh-issue-128916.GEePbO.rst b/Misc/NEWS.d/next/Library/2025-01-17-11-46-16.gh-issue-128916.GEePbO.rst
new file mode 100644 (file)
index 0000000..f2db341
--- /dev/null
@@ -0,0 +1,3 @@
+Do not attempt to set ``SO_REUSEPORT`` on sockets of address families
+other than ``AF_INET`` and ``AF_INET6``, as it is meaningless with these
+address families, and the call with fail with Linux kernel 6.12.9 and newer.