Skip to content

gh-154997: Guard stale self->raw access in bufferedio.c after reentrant detach() - #155017

Open
BHUVANSH855 wants to merge 7 commits into
python:mainfrom
BHUVANSH855:investigate-bufferedio-raw-detach
Open

gh-154997: Guard stale self->raw access in bufferedio.c after reentrant detach()#155017
BHUVANSH855 wants to merge 7 commits into
python:mainfrom
BHUVANSH855:investigate-bufferedio-raw-detach

Conversation

@BHUVANSH855

@BHUVANSH855 BHUVANSH855 commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Fix stale self->raw accesses in buffered I/O objects after a reentrant
detach().

Introduce a shared raw_access_safe() helper and use it before dispatching
to the underlying raw stream from the remaining affected buffered I/O paths.
This preserves the existing detached and uninitialized error semantics while
avoiding dereferences of a stale self->raw pointer.

The guarded call sites include:

  • _io__Buffered_close_impl()
  • _buffered_raw_tell()
  • _buffered_raw_seek()
  • _io__Buffered_truncate_impl()
  • _bufferedreader_raw_read()
  • _bufferedreader_read_all()
  • _bufferedwriter_raw_write()

Tests

Added regression tests covering reentrant detach() during:

  • close()
  • raw read
  • read_all()
  • raw write
  • truncate()
  • post-truncate raw_tell()

These tests verify that the affected operations raise the expected exception
(or preserve the intended behavior for raw_tell()) instead of accessing a
stale raw stream.

The reproducer from the issue was also used to verify the patched code paths.

Notes

raw_seek() was intentionally left unchanged from a testing perspective.
While it now benefits from the shared guard, it was not one of the original
SIGSEGV cases and is outside the scope of this change.

During testing, test_read1_error_does_not_cause_reentrant_failure was found
to abort under ASan even on an unmodified main checkout due to unrelated
allocator behavior, so it is not treated as a regression introduced by this
patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant