Merge pull request #630 from evandroforks/close_http_client_connections

Fixed HttpClient session not being closed
This commit is contained in:
Damien Elmes 2020-05-21 09:17:07 +10:00 committed by GitHub
commit 8300d8d85c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 0 deletions

View File

@ -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

View File

@ -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)