Skip to content

Commit 8a8f5d6

Browse files
miss-islingtonmgornyvsajip
authored
[3.12] gh-128916: Do not set SO_REUSEPORT on non-AF_INET* sockets (GH-128933) (#128970)
gh-128916: Do not set `SO_REUSEPORT` on non-`AF_INET*` sockets (GH-128933) * 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 --------- (cherry picked from commit 3829104) Co-authored-by: Michał Górny <[email protected]> Co-authored-by: Vinay Sajip <[email protected]>
1 parent 8ee250b commit 8a8f5d6

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Lib/asyncio/base_events.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,9 @@ async def create_server(
15501550
if reuse_address:
15511551
sock.setsockopt(
15521552
socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
1553-
if reuse_port:
1553+
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
1554+
# on other address families than AF_INET/AF_INET6.
1555+
if reuse_port and af in (socket.AF_INET, socket.AF_INET6):
15541556
_set_reuseport(sock)
15551557
# Disable IPv4/IPv6 dual stack support (enabled by
15561558
# default on Linux) which makes a single socket

Lib/socket.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,9 @@ def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False,
932932
# Fail later on bind(), for platforms which may not
933933
# support this option.
934934
pass
935-
if reuse_port:
935+
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
936+
# on other address families than AF_INET/AF_INET6.
937+
if reuse_port and family in (AF_INET, AF_INET6):
936938
sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
937939
if has_ipv6 and family == AF_INET6:
938940
if dualstack_ipv6:

Lib/socketserver.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,12 @@ def server_bind(self):
468468
"""
469469
if self.allow_reuse_address and hasattr(socket, "SO_REUSEADDR"):
470470
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
471-
if self.allow_reuse_port and hasattr(socket, "SO_REUSEPORT"):
471+
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
472+
# on other address families than AF_INET/AF_INET6.
473+
if (
474+
self.allow_reuse_port and hasattr(socket, "SO_REUSEPORT")
475+
and self.address_family in (socket.AF_INET, socket.AF_INET6)
476+
):
472477
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
473478
self.socket.bind(self.server_address)
474479
self.server_address = self.socket.getsockname()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Do not attempt to set ``SO_REUSEPORT`` on sockets of address families
2+
other than ``AF_INET`` and ``AF_INET6``, as it is meaningless with these
3+
address families, and the call with fail with Linux kernel 6.12.9 and newer.

0 commit comments

Comments
 (0)