If debug server is unreachable, program hangs
I'm not sure if this is simply due to having switched to PyCharm after using PyDev in Eclipse/LiClipse for several years, but by far the biggest headache that I get from debugging these days is that the call to pydevd.settrace() will hang if the client cannot connect to the server.
It didn't used to do this, though. The code would just keep running, with no debugging happening, when I was using PyDev in Eclipse. I would really like to find a solution to this, because every time it happens, I have to reboot my debug server and my gunicorn instances (in that order) to get my webserver to respond again.
Would agree that a pydevd.settrace(..., timeout_secs=3) or something would be fantastic. We can brew our own, but it seems like a common-enough flow.
On local machine it is possible to user psutil to check if port is opened and then conditionally settrace.
import psutil
def is_listening_local(port=5678):
els = psutil.net_connections()
for el in els:
if el.laddr.port == port:
return True
else:
return False
When we would like to check if remote port is opened, then following code could work:
def is_listening_remote(host, port=5678):
s = socket(AF_INET, SOCK_STREAM)
try:
s.settimeout(0.01)
s.connect((host, port))
s.close()
return True
except Exception as e:
return False
But using such connection check causes remote pydevd server to terminate after couple of connects. Which would be nice to get fixed.