Enhance fetch_targets with error handling and timeout

Added error handling and timeout to fetch_targets function.
This commit is contained in:
Roy
2026-06-06 07:08:45 -07:00
committed by GitHub
parent be8bd97c55
commit cd124132cc
+21 -8
View File
@@ -1839,14 +1839,27 @@ THEMEMUSIC_CODE = r"""(function () {
# Utility: Fetch debugger targets # Utility: Fetch debugger targets
def fetch_targets(host, port): def fetch_targets(host, port):
conn = http.client.HTTPConnection(host, port) try:
conn.request("GET", "/json") conn = http.client.HTTPConnection(host, port, timeout=5)
resp = conn.getresponse() conn.request("GET", "/json")
if resp.status != 200: resp = conn.getresponse()
raise Exception(f"Failed to fetch targets: {resp.status} {resp.reason}")
data = resp.read() if resp.status != 200:
conn.close() print(f"Failed to fetch targets: {resp.status} {resp.reason}")
return json.loads(data) sys.exit(0)
data = resp.read()
return json.loads(data)
except (ConnectionRefusedError, socket.timeout, http.client.CannotSendRequest, http.client.RemoteDisconnected, Exception) as e:
print(f"Error fetching debugger targets: {e}")
sys.exit(0)
finally:
try:
conn.close()
except:
pass
# Find websocket debugger URL for the target title # Find websocket debugger URL for the target title
def get_ws_url_by_title(host, port, title): def get_ws_url_by_title(host, port, title):