GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `gzip.GzipFile` creates reference cycle that requires a deep garbage collection cycle to cleanup. · Issue #129640 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
gzip.GzipFile creates reference cycle that requires a deep garbage collection cycle to cleanup.#129640
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@fowczarek
fowczarek
opened on Feb 4, 2025
Issue body actions

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

  • gh-129640: Add tests for reference cycle in gzip.GzipFile #130916
Reacted by Iga Karbowiak, Patryk Zawadzki, Jakub and Przemysław Łada

Activity

  1. fowczarek
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Feb 4, 2025
  2. picnixz
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Feb 4, 2025
  3. Mr-Sunglasses
    added a commit that references this issue on Mar 6, 2025

    pythongh-129640: Fix reference cycle in `gzip.GzipFile`

    06999db
  4. cmaloney commented on Mar 7, 2025

    @cmaloney
    cmaloney
    on Mar 7, 2025
    Contributor
    More actions

    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)

    Reacted by Shantanu, Kanishk Pachauri and Filip Owczarek
  5. Mr-Sunglasses commented on Mar 8, 2025

    @Mr-Sunglasses
    Mr-Sunglasses
    on Mar 8, 2025
    Contributor
    More actions

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

    Reacted by Cody Maloney and Filip Owczarek
  6. hauntsaninja commented on Mar 9, 2025

    @hauntsaninja
    hauntsaninja
    on Mar 9, 2025
    Contributor
    More actions

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

    Reacted by Cody Maloney
  7. hauntsaninja
    closed this as completedon Mar 9, 2025
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

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.