diff options
author | pukkandan <pukkandan.ytdlp@gmail.com> | 2022-07-11 01:13:29 +0530 |
---|---|---|
committer | pukkandan <pukkandan.ytdlp@gmail.com> | 2022-07-11 01:13:29 +0530 |
commit | 6d645b5577031d0611acab94a5ca3c88db9042f8 (patch) | |
tree | 765960ee8979d3a16deae4375ab44adf337b2860 /yt_dlp/downloader/http.py | |
parent | 563e0bf82a84d2829ef4745dbaf23344e772fadb (diff) | |
download | hypervideo-pre-6d645b5577031d0611acab94a5ca3c88db9042f8.tar.lz hypervideo-pre-6d645b5577031d0611acab94a5ca3c88db9042f8.tar.xz hypervideo-pre-6d645b5577031d0611acab94a5ca3c88db9042f8.zip |
[http] Ensure the file handle is always closed
Closes #4323
Diffstat (limited to 'yt_dlp/downloader/http.py')
-rw-r--r-- | yt_dlp/downloader/http.py | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/yt_dlp/downloader/http.py b/yt_dlp/downloader/http.py index 6b59320b8..27d147513 100644 --- a/yt_dlp/downloader/http.py +++ b/yt_dlp/downloader/http.py @@ -206,6 +206,12 @@ class HttpFD(FileDownloader): except RESPONSE_READ_EXCEPTIONS as err: raise RetryDownload(err) + def close_stream(): + if ctx.stream is not None: + if not ctx.tmpfilename == '-': + ctx.stream.close() + ctx.stream = None + def download(): data_len = ctx.data.info().get('Content-length', None) @@ -239,12 +245,9 @@ class HttpFD(FileDownloader): before = start # start measuring def retry(e): - to_stdout = ctx.tmpfilename == '-' - if ctx.stream is not None: - if not to_stdout: - ctx.stream.close() - ctx.stream = None - ctx.resume_len = byte_counter if to_stdout else os.path.getsize(encodeFilename(ctx.tmpfilename)) + close_stream() + ctx.resume_len = (byte_counter if ctx.tmpfilename == '-' + else os.path.getsize(encodeFilename(ctx.tmpfilename))) raise RetryDownload(e) while True: @@ -382,6 +385,9 @@ class HttpFD(FileDownloader): continue except SucceedDownload: return True + except: # noqa: E722 + close_stream() + raise self.report_error('giving up after %s retries' % retries) return False |