Skip to content

Commit c58cdfe

Browse files
miss-islingtoncmaloneyclaude
authored
[3.15] gh-154997: Reject reentrant detach() on buffered I/O objects (GH-162370) (#163028)
gh-154997: Reject reentrant detach() on buffered I/O objects (GH-162370) Buffered objects already take the lock around every call into the raw stream, so a same-thread callback that re-enters the object gets a RuntimeError. `detach` didn't take the lock which meant it could set self->raw to NULL while other code was using it. Close the gap where it opens: - `.detach()`: Take the lock before clearing. - `.close()`: re-check after `flush()` lock release if the stream has been detached. (cherry picked from commit d9d8cb4) Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 71d4d3c commit c58cdfe

3 files changed

Lines changed: 59 additions & 16 deletions

File tree

‎Lib/test/test_io/test_bufferedio.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,6 +1517,30 @@ def test_args_error(self):
15171517
with self.assertRaisesRegex(TypeError, "BufferedRandom"):
15181518
self.tp(self.BytesIO(), 1024, 1024, 1024)
15191519

1520+
def test_reentrant_detach(self):
1521+
# gh-154997: detach() from a raw callback used to crash.
1522+
def detach(*args):
1523+
bufio.detach()
1524+
return 1 # partial, so the operation calls raw again
1525+
rawio = self.MockRawIO()
1526+
rawio.readinto = rawio.write = detach
1527+
bufio = self.tp(rawio, buffer_size=4)
1528+
bufio.flush = lambda: None # don't fail in detach()'s flush
1529+
self.assertRaisesRegex(RuntimeError, "reentrant", bufio.read, 8)
1530+
self.assertRaisesRegex(RuntimeError, "reentrant", bufio.write, b"x" * 8)
1531+
self.assertIs(bufio.raw, rawio)
1532+
del rawio.readinto, rawio.write
1533+
1534+
def test_detach_during_close(self):
1535+
def flush():
1536+
del bufio.flush
1537+
bufio.detach()
1538+
rawio = self.MockRawIO()
1539+
bufio = self.tp(rawio)
1540+
bufio.flush = flush
1541+
self.assertRaisesRegex(ValueError, "detached", bufio.close)
1542+
self.assertFalse(rawio.closed)
1543+
15201544

15211545
class PyBufferedRandomTest(BufferedRandomTest, PyTestCase):
15221546
tp = pyio.BufferedRandom
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a crash in :meth:`io.BufferedIOBase.detach`.

‎Modules/_io/bufferedio.c‎

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -337,27 +337,29 @@ _enter_buffered_busy(buffered *self)
337337
PyThread_release_lock(self->lock); \
338338
} while(0);
339339

340+
static inline int
341+
buffered_check_initialized(buffered *self)
342+
{
343+
if (self->ok > 0) {
344+
return 0;
345+
}
346+
if (self->detached) {
347+
PyErr_SetString(PyExc_ValueError, "raw stream has been detached");
348+
}
349+
else {
350+
PyErr_SetString(PyExc_ValueError,
351+
"I/O operation on uninitialized object");
352+
}
353+
return -1;
354+
}
355+
340356
#define CHECK_INITIALIZED(self) \
341-
if (self->ok <= 0) { \
342-
if (self->detached) { \
343-
PyErr_SetString(PyExc_ValueError, \
344-
"raw stream has been detached"); \
345-
} else { \
346-
PyErr_SetString(PyExc_ValueError, \
347-
"I/O operation on uninitialized object"); \
348-
} \
357+
if (buffered_check_initialized(self) < 0) { \
349358
return NULL; \
350359
}
351360

352361
#define CHECK_INITIALIZED_INT(self) \
353-
if (self->ok <= 0) { \
354-
if (self->detached) { \
355-
PyErr_SetString(PyExc_ValueError, \
356-
"raw stream has been detached"); \
357-
} else { \
358-
PyErr_SetString(PyExc_ValueError, \
359-
"I/O operation on uninitialized object"); \
360-
} \
362+
if (buffered_check_initialized(self) < 0) { \
361363
return -1; \
362364
}
363365

@@ -587,6 +589,11 @@ _io__Buffered_close_impl(buffered *self)
587589
if (r < 0) {
588590
exc = PyErr_GetRaisedException();
589591
}
592+
/* flush() ran without the lock and may have detached raw. */
593+
if (buffered_check_initialized(self) < 0) {
594+
_PyErr_ChainExceptions1(exc);
595+
goto end;
596+
}
590597

591598
res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close));
592599

@@ -622,10 +629,21 @@ _io__Buffered_detach_impl(buffered *self)
622629
if (_PyFile_Flush((PyObject *)self) < 0) {
623630
return NULL;
624631
}
632+
/* Operations on raw hold the lock, so a reentrant detach() raises
633+
instead of clearing self->raw under them. */
634+
if (!ENTER_BUFFERED(self)) {
635+
return NULL;
636+
}
637+
/* flush() may have detached raw. */
638+
if (buffered_check_initialized(self) < 0) {
639+
LEAVE_BUFFERED(self)
640+
return NULL;
641+
}
625642
raw = self->raw;
626643
self->raw = NULL;
627644
self->detached = 1;
628645
self->ok = 0;
646+
LEAVE_BUFFERED(self)
629647
return raw;
630648
}
631649

0 commit comments

Comments
 (0)