diff --git a/pylib/anki/httpclient.py b/pylib/anki/httpclient.py index e0c054626..55842918a 100644 --- a/pylib/anki/httpclient.py +++ b/pylib/anki/httpclient.py @@ -28,6 +28,20 @@ class HttpClient: self.progress_hook = progress_hook self.session = requests.Session() + def __enter__(self): + return self + + def __exit__(self, *args): + self.close() + + def close(self): + if self.session: + self.session.close() + self.session = None + + def __del__(self): + self.close() + def post(self, url: str, data: Any, headers: Optional[Dict[str, str]]) -> Response: data = _MonitoringFile( data, hook=self.progress_hook diff --git a/qt/aqt/editor.py b/qt/aqt/editor.py index c6298c5e7..3ee676cbe 100644 --- a/qt/aqt/editor.py +++ b/qt/aqt/editor.py @@ -803,6 +803,7 @@ to a cloze type first, via Edit>Change Note Type.""" local = False # fetch it into a temporary folder self.mw.progress.start(immediate=not local, parent=self.parentWindow) + r = None ct = None error_msg: Optional[str] = None try: @@ -825,6 +826,8 @@ to a cloze type first, via Edit>Change Note Type.""" error_msg = _("An error occurred while opening %s") % e return finally: + if r: + r.close() self.mw.progress.finish() if error_msg: showWarning(error_msg)