👷 Close network connections explicitly. Fixes #371

This commit is contained in:
Robert Honz
2025-02-28 17:27:12 +01:00
parent 72f68ab735
commit 303435ad1e
+30 -22
View File
@@ -144,11 +144,16 @@ class Download:
progress_total: int = urls_count progress_total: int = urls_count
block_size: int | None = None block_size: int | None = None
elif urls_count == 1: elif urls_count == 1:
# Get file size and compute progress steps try:
r = requests.head(urls[0], timeout=REQUESTS_TIMEOUT_SEC) # Get file size and compute progress steps
total_size_in_bytes: int = int(r.headers.get("content-length", 0)) r = requests.head(urls[0], timeout=REQUESTS_TIMEOUT_SEC)
block_size: int | None = 1048576 total_size_in_bytes: int = int(r.headers.get("content-length", 0))
progress_total: float = total_size_in_bytes / block_size block_size: int | None = 1048576
progress_total: float = total_size_in_bytes / block_size
except:
raise
finally:
r.close()
else: else:
raise ValueError raise ValueError
@@ -236,27 +241,27 @@ class Download:
error: HTTPError | None = None error: HTTPError | None = None
# Retry download on failed segments, with an exponential delay between retries # Retry download on failed segments, with an exponential delay between retries
s = requests.Session() with requests.Session() as s:
retries = Retry(total=5, backoff_factor=1) # , status_forcelist=[ 502, 503, 504 ]) retries = Retry(total=5, backoff_factor=1) # , status_forcelist=[ 502, 503, 504 ])
s.mount("https://", HTTPAdapter(max_retries=retries)) s.mount("https://", HTTPAdapter(max_retries=retries))
try: try:
# Create the request object with stream=True, so the content won't be loaded into memory at once. # Create the request object with stream=True, so the content won't be loaded into memory at once.
r = s.get(url, stream=True, timeout=REQUESTS_TIMEOUT_SEC) r = s.get(url, stream=True, timeout=REQUESTS_TIMEOUT_SEC)
r.raise_for_status() r.raise_for_status()
# Write the content to disk. If `chunk_size` is set to `None` the whole file will be written at once. # Write the content to disk. If `chunk_size` is set to `None` the whole file will be written at once.
with path_segment.open("wb") as f: with path_segment.open("wb") as f:
for data in r.iter_content(chunk_size=block_size): for data in r.iter_content(chunk_size=block_size):
f.write(data) f.write(data)
# Advance progress bar. # Advance progress bar.
self.progress.advance(p_task) self.progress.advance(p_task)
result = True result = True
except Exception: except Exception:
self.progress.advance(p_task) self.progress.advance(p_task)
# To send the progress to the GUI, we need to emit the percentage. # To send the progress to the GUI, we need to emit the percentage.
if not progress_to_stdout: if not progress_to_stdout:
@@ -585,10 +590,13 @@ class Download:
if url: if url:
try: try:
result = requests.get(url, timeout=REQUESTS_TIMEOUT_SEC).content response: requests.Response = requests.get(url, timeout=REQUESTS_TIMEOUT_SEC)
result = response.content
except Exception as e: except Exception as e:
# TODO: Implement propper logging. # TODO: Implement propper logging.
print(e) print(e)
finally:
response.close()
elif path_file: elif path_file:
try: try:
with open(path_file, "rb") as f: with open(path_file, "rb") as f: