]> jfr.im git - yt-dlp.git/commitdiff
[http] Ensure the file handle is always closed
authorpukkandan <redacted>
Sun, 10 Jul 2022 19:43:29 +0000 (01:13 +0530)
committerpukkandan <redacted>
Sun, 10 Jul 2022 19:43:29 +0000 (01:13 +0530)
Closes #4323

yt_dlp/downloader/http.py

index 6b59320b8af9629943a53997d9baaebebd0b5459..27d147513cded8f41a427d7ec3a8d4480d9f7f7c 100644 (file)
@@ -206,6 +206,12 @@ def establish_connection():
             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 @@ def download():
             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 @@ def retry(e):
                 continue
             except SucceedDownload:
                 return True
+            except:  # noqa: E722
+                close_stream()
+                raise
 
         self.report_error('giving up after %s retries' % retries)
         return False