Skip to content

Commit d8a4426

Browse files
miss-islingtonmgornyvsajip
authored
[3.13] gh-128916: Do not set SO_REUSEPORT on non-AF_INET* sockets (GH-128933) (#128969)
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 77e29c7 commit d8a4426

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
@@ -1590,7 +1590,9 @@ async def create_server(
15901590
if reuse_address:
15911591
sock.setsockopt(
15921592
socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
1593-
if reuse_port:
1593+
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
1594+
# on other address families than AF_INET/AF_INET6.
1595+
if reuse_port and af in (socket.AF_INET, socket.AF_INET6):
15941596
_set_reuseport(sock)
15951597
if keep_alive:
15961598
sock.setsockopt(

Lib/socket.py

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