Skip to content

Commit d222452

Browse files
committed
gc: root a memoryview's backing object regardless of readonly
1 parent d117de5 commit d222452

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

‎src/runtime/PyMemoryView.cpp‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,14 @@ PyType *PyMemoryView::static_type() const { return types::memoryview(); }
396396
void PyMemoryView::visit_graph(Visitor &visitor)
397397
{
398398
PyObject::visit_graph(visitor);
399-
if (m_view.obj && !m_view.readonly) { visitor.visit(*m_view.obj); }
400-
if (m_managed_buffer && m_managed_buffer->m_main_view.obj
401-
&& !m_managed_buffer->m_main_view.readonly) {
399+
// A view must root its backing object whether or not it is readonly: readonly
400+
// governs writing *through* the view, not whether the view holds a reference.
401+
// Because a memoryview over immutable bytes is always readonly, skipping those
402+
// left BufferedWriter::raw_write's PyBytes unrooted - it was collected during
403+
// the allocations that follow, and the raw stream then copied recycled memory.
404+
// Reproduces with --gc-frequency 1.
405+
if (m_view.obj) { visitor.visit(*m_view.obj); }
406+
if (m_managed_buffer && m_managed_buffer->m_main_view.obj) {
402407
visitor.visit(*m_managed_buffer->m_main_view.obj);
403408
}
404409
}

0 commit comments

Comments
 (0)