Skip to content

gzip.GzipFile creates reference cycle that requires a deep garbage collection cycle to cleanup. #129640

Closed

Description

@fowczarek

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

  1. Disable garbage collection temporarily to make sure we are the ones who catch it
  2. Set the garbage collector's debug level to DEBUG_LEAK
  3. Open GzipFile.
  4. 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

Linked PRs

Activity

  1. added
    type-bug
    on Feb 4, 2025
  2. added
    stdlib
    on Feb 4, 2025
  3. added a commit that references this issue on Mar 6, 2025
  4. cmaloney commented on Mar 7, 2025

    @cmaloney
    Contributor

    I think this is the equivalent to gh-129726? (Hadn't seen this issue until just now). If so, has a fix which has been backported to 3.12. Similar concept, but broke refloop by adding a weakref (PR: #130055)

  5. Mr-Sunglasses commented on Mar 8, 2025

    @Mr-Sunglasses
    Contributor

    This Issue is fixed in (PR: #130055) by @cmaloney . The (PR: #130916) add's some tests for it.

  6. hauntsaninja commented on Mar 9, 2025

    @hauntsaninja
    Contributor

    Additional test PRs still welcome, but going ahead and closing this since the issue is resolved by #130055 (and backported)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibtype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions