From 6222fb915e765cf1384e2b4055129f0aefe65998 Mon Sep 17 00:00:00 2001 From: Cody Maloney Date: Sat, 21 Nov 2026 00:58:49 -0800 Subject: [PATCH] gh-154997: Reject reentrant detach() on buffered I/O objects 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. Co-Authored-By: Claude Opus 5.5 --- Lib/test/test_io/test_bufferedio.py | 24 +++++++++ ...-11-21-00-58-44.gh-issue-154997.CJR_Et.rst | 1 + Modules/_io/bufferedio.c | 50 +++++++++++++------ 3 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-21-00-58-44.gh-issue-154997.CJR_Et.rst diff --git a/Lib/test/test_io/test_bufferedio.py b/Lib/test/test_io/test_bufferedio.py index 997e335591b483..eb8963ae067412 100644 --- a/Lib/test/test_io/test_bufferedio.py +++ b/Lib/test/test_io/test_bufferedio.py @@ -1522,6 +1522,30 @@ def test_args_error(self): with self.assertRaisesRegex(TypeError, "BufferedRandom"): self.tp(self.BytesIO(), 1024, 1024, 1024) + def test_reentrant_detach(self): + # gh-154997: detach() from a raw callback used to crash. + def detach(*args): + bufio.detach() + return 1 # partial, so the operation calls raw again + rawio = self.MockRawIO() + rawio.readinto = rawio.write = detach + bufio = self.tp(rawio, buffer_size=4) + bufio.flush = lambda: None # don't fail in detach()'s flush + self.assertRaisesRegex(RuntimeError, "reentrant", bufio.read, 8) + self.assertRaisesRegex(RuntimeError, "reentrant", bufio.write, b"x" * 8) + self.assertIs(bufio.raw, rawio) + del rawio.readinto, rawio.write + + def test_detach_during_close(self): + def flush(): + del bufio.flush + bufio.detach() + rawio = self.MockRawIO() + bufio = self.tp(rawio) + bufio.flush = flush + self.assertRaisesRegex(ValueError, "detached", bufio.close) + self.assertFalse(rawio.closed) + class PyBufferedRandomTest(BufferedRandomTest, PyTestCase): tp = pyio.BufferedRandom diff --git a/Misc/NEWS.d/next/Library/2026-11-21-00-58-44.gh-issue-154997.CJR_Et.rst b/Misc/NEWS.d/next/Library/2026-11-21-00-58-44.gh-issue-154997.CJR_Et.rst new file mode 100644 index 00000000000000..18219ee7af0106 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-21-00-58-44.gh-issue-154997.CJR_Et.rst @@ -0,0 +1 @@ +Fix a crash in :meth:`io.BufferedIOBase.detach`. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index f585bd8483370f..754c10798ff6e5 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -340,27 +340,29 @@ _enter_buffered_busy(buffered *self) PyThread_release_lock(self->lock); \ } while(0); +static inline int +buffered_check_initialized(buffered *self) +{ + if (self->ok > 0) { + return 0; + } + if (self->detached) { + PyErr_SetString(PyExc_ValueError, "raw stream has been detached"); + } + else { + PyErr_SetString(PyExc_ValueError, + "I/O operation on uninitialized object"); + } + return -1; +} + #define CHECK_INITIALIZED(self) \ - if (self->ok <= 0) { \ - if (self->detached) { \ - PyErr_SetString(PyExc_ValueError, \ - "raw stream has been detached"); \ - } else { \ - PyErr_SetString(PyExc_ValueError, \ - "I/O operation on uninitialized object"); \ - } \ + if (buffered_check_initialized(self) < 0) { \ return NULL; \ } #define CHECK_INITIALIZED_INT(self) \ - if (self->ok <= 0) { \ - if (self->detached) { \ - PyErr_SetString(PyExc_ValueError, \ - "raw stream has been detached"); \ - } else { \ - PyErr_SetString(PyExc_ValueError, \ - "I/O operation on uninitialized object"); \ - } \ + if (buffered_check_initialized(self) < 0) { \ return -1; \ } @@ -589,6 +591,11 @@ _io__Buffered_close_impl(buffered *self) if (r < 0) { exc = PyErr_GetRaisedException(); } + /* flush() ran without the lock and may have detached raw. */ + if (buffered_check_initialized(self) < 0) { + _PyErr_ChainExceptions1(exc); + goto end; + } res = PyObject_CallMethodNoArgs(self->raw, &_Py_ID(close)); @@ -624,10 +631,21 @@ _io__Buffered_detach_impl(buffered *self) if (_PyFile_Flush((PyObject *)self) < 0) { return NULL; } + /* Operations on raw hold the lock, so a reentrant detach() raises + instead of clearing self->raw under them. */ + if (!ENTER_BUFFERED(self)) { + return NULL; + } + /* flush() may have detached raw. */ + if (buffered_check_initialized(self) < 0) { + LEAVE_BUFFERED(self) + return NULL; + } raw = self->raw; self->raw = NULL; self->detached = 1; self->ok = 0; + LEAVE_BUFFERED(self) return raw; }