gh-138720: Make Buffered closed check match flush by cmaloney · Pull Request #138724 · python/cpython · GitHub
Skip to content

gh-138720: Make Buffered closed check match flush - #138724

Merged
encukou merged 5 commits into
python:mainfrom
cmaloney:bufferedrwpair_gc
Sep 18, 2025
Merged

encukou merged 5 commits into
python:mainfrom
cmaloney:bufferedrwpair_gc

Conversation

@cmaloney

@cmaloney cmaloney commented Sep 10, 2025

Copy link
Copy Markdown
Contributor

In _io__Buffered_flush_impl the macro CHECK_CLOSED is used to check the buffered* is in a good state to be flushed. That differs slightly from buffered_closed.

In some cases, that difference would result in close() (_io__Buffered_close_impl) thinking the file needed to be flushed and closed while flush() thought the file was already closed.

This could happen during GC and would result in an unraisable exception.

In `_io__Buffered_flush_impl` the macro `CHECK_CLOSED` is used to check
the `buffered*` is in a good state to be flushed. That differs slightly
from `buffered_closed`.

In some cases, that difference would result in `close()` thinking the
file needed to be flushed and closed while `flush()` thought the file
was already closed.

This could happen during GC and would result in an unraisable exception.
@cmaloney

cmaloney commented Sep 16, 2025

Copy link
Copy Markdown
Contributor Author

@encukou

encukou commented Sep 17, 2025

Copy link
Copy Markdown
Member

!buildbot PPC64LE.Fedora.Stable.LTO

@bedevere-bot

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @encukou for commit 60fe309 🤖

Results will be shown at:

https://buildbot.python.org/all/#/grid?branch=refs%2Fpull%2F138724%2Fmerge

The command will test the builders whose names match following regular expression: PPC64LE.Fedora.Stable.LTO

The builders matched are:

  • PPC64LE Fedora Stable LTO PR
  • PPC64LE Fedora Stable LTO + PGO PR

@encukou encukou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This looks like a correct change.

Comment on lines +2290 to +2293
# gh-138720: C BufferedRWPair would destruct in a bad order resulting in
# an unraisable exception.
support.gc_collect()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still necessary?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is way more likely to triggered the issue (fixed behavior) with this change.

@cmaloney cmaloney Sep 17, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my dev box (64 bit archlinux) adding the explicit GC takes this from "one in a hundred" -> every time

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK! I couldn't reproduce it myself, but I'll trust the buildbots :)

@encukou
encukou merged commit db68bfc into python:main Sep 18, 2025
51 checks passed
@vstinner

Copy link
Copy Markdown
Member

Should we backport this bugfix?

@encukou

encukou commented Sep 18, 2025

Copy link
Copy Markdown
Member

Can you reproduce it on previous versions? If so, please backport :)

@vstinner

Copy link
Copy Markdown
Member

Can you reproduce it on previous versions? If so, please backport :)

Oh. I'm unable to reproduce the issue on 3.14.

@cmaloney
cmaloney deleted the bufferedrwpair_gc branch September 18, 2025 19:26
@cmaloney

Copy link
Copy Markdown
Contributor Author

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants