Skip to content

Commit 3829104

Browse files
mgornyvsajip
andauthored
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 Co-authored-by: Vinay Sajip <[email protected]> --------- Co-authored-by: Vinay Sajip <[email protected]>
1 parent 8174770 commit 3829104

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Lib/asyncio/base_events.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1593,7 +1593,9 @@ async def create_server(
15931593
if reuse_address:
15941594
sock.setsockopt(
15951595
socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
1596-
if reuse_port:
1596+
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
1597+
# on other address families than AF_INET/AF_INET6.
1598+
if reuse_port and af in (socket.AF_INET, socket.AF_INET6):
15971599
_set_reuseport(sock)
15981600
if keep_alive:
15991601
sock.setsockopt(

Lib/socket.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,9 @@ def create_server(address, *, family=AF_INET, backlog=None, reuse_port=False,
937937
# Fail later on bind(), for platforms which may not
938938
# support this option.
939939
pass
940-
if reuse_port:
940+
# Since Linux 6.12.9, SO_REUSEPORT is not allowed
941+
# on other address families than AF_INET/AF_INET6.
942+
if reuse_port and family in (AF_INET, AF_INET6):
941943
sock.setsockopt(SOL_SOCKET, SO_REUSEPORT, 1)
942944
if has_ipv6 and family == AF_INET6:
943945
if dualstack_ipv6:

Lib/socketserver.py

Lines changed: 6 additions & 1 deletion
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()
Lines changed: 3 additions & 0 deletions
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)