Fixed HttpClient session not being closed

This commit is contained in:
evandrocoan 2020-05-20 11:24:39 -03:00
parent 300fba156e
commit b388c43454
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)