Skip to content

Commit

Permalink
Merge pull request #639 from fabioz/keepalive_626
Browse files Browse the repository at this point in the history
Set keepalive for all debugpy sockets. Fixes #626
  • Loading branch information
karthiknadig authored Jun 7, 2021
2 parents 11b7124 + b53ebf7 commit 8d90ef2
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/debugpy/common/sockets.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,27 @@ def _new_sock():
sock.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
else:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Set TCP keepalive on an open socket.
# It activates after 1 second (TCP_KEEPIDLE,) of idleness,
# then sends a keepalive ping once every 3 seconds (TCP_KEEPINTVL),
# and closes the connection after 5 failed ping (TCP_KEEPCNT), or 15 seconds
try:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
except (AttributeError, OSError):
pass # May not be available everywhere.
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1)
except (AttributeError, OSError):
pass # May not be available everywhere.
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 3)
except (AttributeError, OSError):
pass # May not be available everywhere.
try:
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5)
except (AttributeError, OSError):
pass # May not be available everywhere.
return sock


Expand Down

0 comments on commit 8d90ef2

Please sign in to comment.