-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gzip raising exception when closing with buffer backed by BytesIO #129726
Comments
Explicit The same behavior occurs with an explicit reference to the def explicit_reference():
bio = io.BytesIO()
buffer = gzip.GzipFile(fileobj=bio, mode="w") Can work around it so no longer error at least by updating |
Thanks for testing these, I cannot use the context manager but I may be able to close the buffer explictly.
Yeah, that is my impression too
Yeah would be nice to keep the code working instead of not just crashing :) The gzip code should already have references to the fileobj so to me is surprising it gets deallocated while we are in in GzipFile.close. |
So my original code when getting the gzipped content is this so am not sure I can workaround this other than stop using GzipFile.
Irony of the comment 😅 The documentation explicitly says that calling
|
I still haven't found what is causing the fileobj to be closed... The line which is causing the exception print was added to 3.12 in https://github.com/python/cpython/pull/105920/files it looks like to fix #105808 |
@cmaloney the one you linked touches |
I think it's the move to using a BufferedWriter (gh-89550) changed the behavior around flush and close in some unintended ways (https://github.com/python/cpython/blob/main/Modules/_io/bufferedio.c#L543-L598 being one of the parts). That the stream is being marked as "closed", but later gets flushed to. Still investigating / don't have any verifiable answers yet, just hunches. |
Bisected this issue to #105104 |
Ok so with 3.12 and development mode I see the same warning, don't see the warning in 3.10 and 3.11 though:
So there is something that changed the behavior in 3.12. |
@danifus thanks for your bisection. Any chance you can bisect with the |
I bisected with Could this be due to the GC delaying the finalization of the The reference cycle is: |
Confirmed that breaking the reference cyle (adding a self._buffer = io.BufferedWriter(_WriteBufferStream(weakref.proxy(self)),
buffer_size=self._buffer_size) |
A reference loop was resulting in the `fileobj` held by the `GzipFile` being closed before the `GzipFile`. The issue started with pythongh-89550 in 3.12, but was hidden in most cases until 3.13 when pythongh-62948 made it more visible.
A reference loop was resulting in the `fileobj` held by the `GzipFile` being closed before the `GzipFile`. The issue started with pythongh-89550 in 3.12, but was hidden in most cases until 3.13 when pythongh-62948 made it more visible. (cherry picked from commit 7f39137) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
A reference loop was resulting in the `fileobj` held by the `GzipFile` being closed before the `GzipFile`. The issue started with pythongh-89550 in 3.12, but was hidden in most cases until 3.13 when pythongh-62948 made it more visible. (cherry picked from commit 7f39137) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Fixed by change 7f39137. Backports to 3.12 and 3.13 will follow. |
Thanks a lot! |
Maybe GzipFile should emit a |
…130670) gh-129726: Break `gzip.GzipFile` reference loop (GH-130055) A reference loop was resulting in the `fileobj` held by the `GzipFile` being closed before the `GzipFile`. The issue started with gh-89550 in 3.12, but was hidden in most cases until 3.13 when gh-62948 made it more visible. (cherry picked from commit 7f39137) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
…130669) gh-129726: Break `gzip.GzipFile` reference loop (GH-130055) A reference loop was resulting in the `fileobj` held by the `GzipFile` being closed before the `GzipFile`. The issue started with gh-89550 in 3.12, but was hidden in most cases until 3.13 when gh-62948 made it more visible. (cherry picked from commit 7f39137) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
I implemented a ResourceWarning on |
The test suite can be fixed. The risk of losing data justify emitting ResourceWarning. |
Bug report
Bug description:
Hello,
the following snippet raises an exception in Python 3.13.2 while it's fine with Python 3.12.
Running this with python3.12 is silent, with Python 3.13.2 instead:
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
gzip.GzipFile
reference loop #130055gzip.GzipFile
reference loop (GH-130055) #130669gzip.GzipFile
reference loop (GH-130055) #130670The text was updated successfully, but these errors were encountered: