Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion Lib/test/test_io/test_textio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
import weakref
from collections import UserList
from test import support
from test.support import os_helper, threading_helper
from test.support import (
os_helper,
set_recursion_limit,
threading_helper
)
from test.support.script_helper import assert_python_ok
from .utils import CTestCase, PyTestCase

Expand Down Expand Up @@ -1560,6 +1564,53 @@ def closed(self):
wrapper = self.TextIOWrapper(raw)
wrapper.close() # should not crash

def test_reentrant_detach_during_flush(self):
# gh-143008: Reentrant detach() during flush should not crash.
Comment thread
cmaloney marked this conversation as resolved.
wrapper = None
wrapper_ref = lambda: None

class EvilBuffer(self.BufferedRandom):
detach_on_write = False

def flush(self):
wrapper = wrapper_ref()
if wrapper is not None and not self.detach_on_write:
wrapper.detach()
return super().flush()

def write(self, b):
wrapper = wrapper_ref()
if wrapper is not None and self.detach_on_write:
wrapper.detach()
return len(b)

# Many calls could result in the same null self->buffer crash.
tests = [
('truncate', lambda: wrapper.truncate(0)),
('close', lambda: wrapper.close()),
('detach', lambda: wrapper.detach()),
('seek', lambda: wrapper.seek(0)),
('tell', lambda: wrapper.tell()),
('reconfigure', lambda: wrapper.reconfigure(line_buffering=True)),
]
for name, method in tests:
with self.subTest(name), set_recursion_limit(100):
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.

Why do you reduce the recursion limit?

wrapper = self.TextIOWrapper(EvilBuffer(self.MockRawIO()), encoding='utf-8')
wrapper_ref = weakref.ref(wrapper)
# Used to crash; now will run out of stack.
self.assertRaises(RecursionError, method)
wrapper_ref = lambda: None
del wrapper
Comment on lines +1602 to +1603
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.

Would it work to just assign wrapper to None (and leave wrapper_ref unchanged)?


with self.subTest('read via writeflush'):
EvilBuffer.detach_on_write = True
wrapper = self.TextIOWrapper(EvilBuffer(self.MockRawIO()), encoding='utf-8')
wrapper_ref = weakref.ref(wrapper)
wrapper.write('x')
self.assertRaisesRegex(ValueError, "detached", wrapper.read)
wrapper_ref = lambda: None
del wrapper


class PyTextIOWrapperTest(TextIOWrapperTest, PyTestCase):
shutdown_error = "LookupError: unknown encoding: ascii"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash in :class:`io.TextIOWrapper` when reentrant
:meth:`io.TextIOBase.detach` is called reentrantly from the underlying buffer.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix race conditions when re-initializing a :class:`io.TextIOWrapper` object.
4 changes: 3 additions & 1 deletion Modules/_io/clinic/textio.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading