Skip to content

gh-154997: Reject reentrant detach() on buffered I/O objects - #162370

Open
cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:exp/gh154997
Open

cmaloney wants to merge 1 commit into
python:mainfrom
cmaloney:exp/gh154997

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

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.

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 <noreply@anthropic.com>
@cmaloney cmaloney added 3.14 bugs and security fixes 3.15 bugs and security fixes labels Nov 21, 2026
@cmaloney cmaloney added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes and removed 3.14 bugs and security fixes 3.15 bugs and security fixes labels Nov 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant