Skip to content

Commit 217e79a

Browse files
cmaloneyclaude
andauthored
[3.14] gh-154997: Reject reentrant detach() on buffered I/O objects (GH-162370) (#163029)
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: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b3c4771 commit 217e79a

3 files changed

Lines changed: 59 additions & 16 deletions

File tree

‎Lib/test/test_io.py‎

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

2723+
def test_reentrant_detach(self):
2724+
# gh-154997: detach() from a raw callback used to crash.
2725+
def detach(*args):
2726+
bufio.detach()
2727+
return 1 # partial, so the operation calls raw again
2728+
rawio = self.MockRawIO()
2729+
rawio.readinto = rawio.write = detach
2730+
bufio = self.tp(rawio, buffer_size=4)
2731+
bufio.flush = lambda: None # don't fail in detach()'s flush
2732+
self.assertRaisesRegex(RuntimeError, "reentrant", bufio.read, 8)
2733+
self.assertRaisesRegex(RuntimeError, "reentrant", bufio.write, b"x" * 8)
2734+
self.assertIs(bufio.raw, rawio)
2735+
del rawio.readinto, rawio.write
2736+
2737+
def test_detach_during_close(self):
2738+
def flush():
2739+
del bufio.flush
2740+
bufio.detach()
2741+
rawio = self.MockRawIO()
2742+
bufio = self.tp(rawio)
2743+
bufio.flush = flush
2744+
self.assertRaisesRegex(ValueError, "detached", bufio.close)
2745+
self.assertFalse(rawio.closed)
2746+
27232747

27242748
class PyBufferedRandomTest(BufferedRandomTest):
27252749
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
@@ -336,27 +336,29 @@ _enter_buffered_busy(buffered *self)
336336
PyThread_release_lock(self->lock); \
337337
} while(0);
338338

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

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

@@ -578,6 +580,11 @@ _io__Buffered_close_impl(buffered *self)
578580
if (r < 0) {
579581
exc = PyErr_GetRaisedException();
580582
}
583+
/* flush() ran without the lock and may have detached raw. */
584+
if (buffered_check_initialized(self) < 0) {
585+
_PyErr_ChainExceptions1(exc);
586+
goto end;
587+
}
581588

582589
res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close));
583590

@@ -613,10 +620,21 @@ _io__Buffered_detach_impl(buffered *self)
613620
if (_PyFile_Flush((PyObject *)self) < 0) {
614621
return NULL;
615622
}
623+
/* Operations on raw hold the lock, so a reentrant detach() raises
624+
instead of clearing self->raw under them. */
625+
if (!ENTER_BUFFERED(self)) {
626+
return NULL;
627+
}
628+
/* flush() may have detached raw. */
629+
if (buffered_check_initialized(self) < 0) {
630+
LEAVE_BUFFERED(self)
631+
return NULL;
632+
}
616633
raw = self->raw;
617634
self->raw = NULL;
618635
self->detached = 1;
619636
self->ok = 0;
637+
LEAVE_BUFFERED(self)
620638
return raw;
621639
}
622640

0 commit comments

Comments
 (0)