Skip to content

Deadlock when array.fromfile re-enters append via __index__ #6549

Closed

Description

@jackfromeast

What happened?

array.fromfile calls the user-provided f.read(), and that callback can append objects to the same array while it is being resized. The append path grabs the array's write lock before converting the element, then calls the object's __index__. If __index__ performs another array.append (as in the PoC), the second append blocks forever on the already-held PyRwLock, causing a deadlock instead of raising an error.

Proof of Concept:

import array

a = array.array("b")

class X:
    def __index__(self):
        a.append(0)
        return 0

class R:
    def read(self, n):
        a.append(X())
        return b"\0" * n

a.fromfile(R(), 1)
Affected Versions
RustPython Version Status Exit Code
Python 3.13.0alpha (heads/main-dirty:21300f689, Dec 13 2025, 22:16:49) [RustPython 0.4.0 with rustc 1.90.0-nightly (11ad40bb8 2025-06-28)] Deadlock 124
Vulnerable Code
#[pymethod]
fn fromfile(&self, f: PyObjectRef, n: isize, vm: &VirtualMachine) -> PyResult<()> {
    let b = vm.call_method(&f, "read", (n_bytes,))?; // user f.read runs arbitrary code while the array can still be mutated
    ...
}

#[pymethod]
fn append(zelf: &Py<Self>, x: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
    zelf.try_resizable(vm)?.push(x, vm) // takes a non-reentrant PyRwLock write guard before converting x
}

impl ArrayElement for i8 {
    fn try_into_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self> {
        obj.try_index(vm)?.try_to_primitive(vm) // invokes __index__ while the write lock is held; reentrant array.append inside __index__ deadlocks
    }
}

impl BufferResizeGuard for PyArray {
    fn try_resizable_opt(&self) -> Option<Self::Resizable<'_>> {
        let w = self.write(); // second append blocks here waiting on the same PyRwLock, hanging the interpreter
        (self.exports.load(atomic::Ordering::SeqCst) == 0).then_some(w)
    }
}
Rust Output
Program hangs forever
CPython Output
(No output)

Activity

  1. coderabbitai commented on Dec 27, 2025

    @coderabbitai
    Contributor

    📝 CodeRabbit Plan Mode

    Generate an implementation plan and prompts that you can use with your favorite coding agent.

    • Create Plan
    Examples

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  2. joefarebrother commented on Jan 1, 2026

    @joefarebrother

    The issue is unrelated to fromfile; and occurs for several array operations that can call into user code (__index__) while a read or write lock is held; including append, insert, and count.

    Each of the methods on ArrayContentType are called with a lock held, and so should not call try_into_from_object.

  3. youknowone commented on Oct 6, 2026

    @youknowone
    Member

    The originally deadlocking array.fromfile + re-entrant __index__/append PoC finishes on current main. Closing.

    — commented by Grok 4.6

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions