From 52f4189176842395c0510ee812fc62b5af2f1ce0 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Tue, 6 Oct 2020 19:15:20 +0200 Subject: [PATCH] qt TorDetector: close socket ResourceWarning below is shown when running python3 with "-X dev": ...\electrum\electrum\gui\qt\network_dialog.py:457: ResourceWarning: unclosed if TorDetector.is_tor_port(net_addr): ResourceWarning: Enable tracemalloc to get the object allocation traceback --- electrum/gui/qt/network_dialog.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/electrum/gui/qt/network_dialog.py b/electrum/gui/qt/network_dialog.py index 4f1ecfb44..9287d8178 100644 --- a/electrum/gui/qt/network_dialog.py +++ b/electrum/gui/qt/network_dialog.py @@ -464,13 +464,13 @@ class TorDetector(QThread): @staticmethod def is_tor_port(net_addr: Tuple[str, int]) -> bool: try: - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.settimeout(0.1) - s.connect(net_addr) - # Tor responds uniquely to HTTP-like requests - s.send(b"GET\n") - if b"Tor is not an HTTP Proxy" in s.recv(1024): - return True + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(0.1) + s.connect(net_addr) + # Tor responds uniquely to HTTP-like requests + s.send(b"GET\n") + if b"Tor is not an HTTP Proxy" in s.recv(1024): + return True except socket.error: pass return False