Skip to content

Commit

Permalink
Use SO_REUSEPORT only for AF_INET sockets (#303)
Browse files Browse the repository at this point in the history
The latest python version changes the behavior, now if it's used with
other kind of sockets (ex AF_UNIX) it raises OSError:

python/cpython#128916
  • Loading branch information
danigm authored Feb 28, 2025
1 parent 463478f commit aa05daf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion thriftpy2/contrib/aio/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,9 @@ def _init_sock(self):
_sock = socket.socket(self.socket_family, socket.SOCK_STREAM)

_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, "SO_REUSEPORT"):
# valid socket https://github.com/python/cpython/issues/128916
valid_family = (socket.AF_INET, socket.AF_INET6)
if _sock.family in valid_family and hasattr(socket, "SO_REUSEPORT"):
try:
_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
Expand Down
4 changes: 3 additions & 1 deletion thriftpy2/transport/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ def _init_sock(self):
_sock = socket.socket(self.socket_family, socket.SOCK_STREAM)

_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if hasattr(socket, "SO_REUSEPORT"):
# valid socket https://github.com/python/cpython/issues/128916
valid_family = (socket.AF_INET, socket.AF_INET6)
if _sock.family in valid_family and hasattr(socket, "SO_REUSEPORT"):
try:
_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error as err:
Expand Down

0 comments on commit aa05daf

Please # to comment.