Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-111049: Fix crash during garbage collection of the BytesIO buffer …
…object
  • Loading branch information
serhiy-storchaka committed Oct 23, 2023
commit d2d8ed2ab582ae011c970b5369ef6d08da67ca5b
21 changes: 21 additions & 0 deletions Lib/test/test_memoryio.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import unittest
from test import support

import gc
import io
import _pyio as pyio
import pickle
import sys
import weakref

class IntLike:
def __init__(self, num):
Expand Down Expand Up @@ -463,6 +465,25 @@ def test_getbuffer(self):
memio.close()
self.assertRaises(ValueError, memio.getbuffer)

def test_getbuffer_gc_collect(self):
memio = self.ioclass(b"1234567890")
buf = memio.getbuffer()
memiowr = weakref.ref(memio)
bufwr = weakref.ref(buf)
# Create a reference loop.
a = [buf]
a.append(a)
# XXX The Python implementation emits an unraisable exception.
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
with support.catch_unraisable_exception():
del memio
del buf
del a
# XXX The C implementation emits an unraisable exception.
Comment thread
erlend-aasland marked this conversation as resolved.
Outdated
with support.catch_unraisable_exception():
gc.collect()
self.assertIsNone(memiowr())
self.assertIsNone(bufwr())

def test_read1(self):
buf = self.buftype("1234567890")
self.assertEqual(self.ioclass(buf).read1(), buf)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crash during garbage collection of the :class:`io.BytesIO` buffer
object.
15 changes: 5 additions & 10 deletions Modules/_io/bytesio.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ check_exports(bytesio *self)
if (self->exports > 0) {
PyErr_SetString(PyExc_BufferError,
"Existing exports of data: object cannot be re-sized");
// abort();
return 1;
}
return 0;
Expand Down Expand Up @@ -989,7 +990,9 @@ static int
bytesio_clear(bytesio *self)
{
Py_CLEAR(self->dict);
Py_CLEAR(self->buf);
if (self->exports == 0) {
Py_CLEAR(self->buf);
}
Comment thread
erlend-aasland marked this conversation as resolved.
return 0;
}

Expand Down Expand Up @@ -1094,13 +1097,6 @@ bytesiobuf_releasebuffer(bytesiobuf *obj, Py_buffer *view)
b->exports--;
}

static int
bytesiobuf_clear(bytesiobuf *self)
{
Py_CLEAR(self->source);
return 0;
}

static int
bytesiobuf_traverse(bytesiobuf *self, visitproc visit, void *arg)
{
Expand All @@ -1115,15 +1111,14 @@ bytesiobuf_dealloc(bytesiobuf *self)
PyTypeObject *tp = Py_TYPE(self);
/* bpo-31095: UnTrack is needed before calling any callbacks */
PyObject_GC_UnTrack(self);
(void)bytesiobuf_clear(self);
Py_CLEAR(self->source);
tp->tp_free(self);
Py_DECREF(tp);
}

static PyType_Slot bytesiobuf_slots[] = {
{Py_tp_dealloc, bytesiobuf_dealloc},
{Py_tp_traverse, bytesiobuf_traverse},
{Py_tp_clear, bytesiobuf_clear},

// Buffer protocol
{Py_bf_getbuffer, bytesiobuf_getbuffer},
Expand Down