Skip to content

A feature-rich command-line audio/video downloader

[rh:urllib] Eagerly release `BytesIO` responses (#17717)

doe1080f9eee6ba6252
diff --git a/yt_dlp/networking/_urllib.py b/yt_dlp/networking/_urllib.py
index 5e6167dd7a..c3f16c99af 100644
--- a/yt_dlp/networking/_urllib.py
+++ b/yt_dlp/networking/_urllib.py
@@ -313,7 +313,9 @@ class UrllibResponseAdapter(Response):
# urllib's addinfourl does not close the underlying fp automatically when fully read
if isinstance(underlying, io.BytesIO):
# data URLs or in-memory responses (e.g. gzip/deflate/brotli decoded)
- if underlying.tell() >= len(underlying.getbuffer()):
+ with underlying.getbuffer() as view:
+ fully_read = underlying.tell() >= len(view)
+ if fully_read:
self.close()
elif isinstance(underlying, io.BufferedReader) and amt is None:
# file URLs.