Closed
Description
Bug report
Bug description:
During debugging memory buildup, I've noticed gzip.GzipFile
holds a reference to itself (Cycle: GzipFile._buffer
->BufferedWriter._raw
->_WriteBufferStream.gzip_file
-> GzipFile
). This cycle prevents memory from being freed until the garbage collector runs a deep cleanup cycle (generation=2).
Steps to reproduce
- Disable garbage collection temporarily to make sure we are the ones who catch it
- Set the garbage collector's debug level to DEBUG_LEAK
- Open GzipFile.
- Force garbage collection and look at its output
import gc
import gzip
import io
gc.collect()
gc.disable()
gc.set_debug(gc.DEBUG_LEAK)
with io.BytesIO() as buffer:
with gzip.GzipFile(mode="wb", fileobj=buffer):
pass
gc.collect()
gc.set_debug(0)
Potential solution
class _WriteBufferStream(io.RawIOBase):
...
def __del__(self):
del self.gzip_file
CPython versions tested on:
3.12
Operating systems tested on:
macOS