Skip to content

Commit

Permalink
Do not use SO_REUSEADDR et all when listening on ephemeral ports (#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Minaev authored and int19h committed Oct 20, 2022
1 parent 1d03820 commit e7fb50a
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/debugpy/common/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def create_server(host, port=0, backlog=socket.SOMAXCONN, timeout=None):

try:
server = _new_sock()
if port != 0:
# If binding to a specific port, make sure that the user doesn't have
# to wait until the OS times out the socket to be able to use that port
# again.if the server or the adapter crash or are force-killed.
if sys.platform == "win32":
server.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
else:
try:
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except (AttributeError, OSError):
pass # Not available everywhere
server.bind((host, port))
if timeout is not None:
server.settimeout(timeout)
Expand All @@ -38,13 +49,6 @@ def create_client():

def _new_sock():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
if sys.platform == "win32":
sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
else:
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
except (AttributeError, OSError):
pass # Not available everywhere

# Set TCP keepalive on an open socket.
# It activates after 1 second (TCP_KEEPIDLE,) of idleness,
Expand Down

0 comments on commit e7fb50a

Please sign in to comment.