Skip to content

GC of unclosed io.BuffererdRWPair after .readinto results in unraisable exception #138720

@cmaloney

Description

@cmaloney

Bug report

Bug description:

In BufferedRWPairTest.test_readinto a BufferedRWPair is created but not explicitly closed which results in it being Garbage Collected (GC). During GC the destruction order often results in an unraisable exception. This can be reliably reproduced by modifying the test to end with support.gc_collect(). The BufferedRWPair should cleanup reliably in this case.

def test_readinto(self):
for method in ("readinto", "readinto1"):
with self.subTest(method):
pair = self.tp(self.BytesIO(b"abcdef"), self.MockRawIO())
data = byteslike(b'\0' * 5)
self.assertEqual(getattr(pair, method)(data), 5)
self.assertEqual(bytes(data), b"abcde")

Warning -- Unraisable exception
Exception ignored while finalizing file <_io.BufferedRWPair object at 0x7fd9d7577950>:
Traceback (most recent call last):
  File "<checkout_dir>/cpython/Lib/test/support/__init__.py", line 837, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ValueError: flush of closed file
Warning -- Unraisable exception
Exception ignored while finalizing file <_io.BufferedWriter>:
Traceback (most recent call last):
  File "<checkout_dir>/cpython/Lib/test/support/__init__.py", line 837, in gc_collect
    gc.collect()
    ~~~~~~~~~~^^
ValueError: flush of closed file

Found while working on GH-138475

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions