Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 29, 2025

array.fromfile could deadlock when __index__ invoked during element conversion performed a nested append on the same array.

  • Append conversion before locking: Convert elements (capturing array kind) prior to acquiring the write lock, then push with a runtime invariant check to avoid reentrant deadlocks.
  • Stronger invariants: Added typed helpers (ArrayType/ArrayElementValue) with assertive mismatch diagnostics to catch unexpected kind/value divergence.
  • Regression coverage: Extended extra_tests/snippets/stdlib_array.py with a reentrancy scenario where fromfile triggers __index__ that re-appends to the same array.

Example:

import array

a = array.array("b")

class X:
    def __index__(self):
        a.append(0)  # reenters append while fromfile is mid-conversion
        return 0

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

a.fromfile(R(), 1)  # no deadlock; contents now [0, 0, 0]
Original prompt

This section details on the original issue you should resolve

<issue_title>Deadlock when array.fromfile re-enters append via __index__</issue_title>
<issue_description>### 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)

</issue_description>

<agent_instructions>Add the code to stdlib_array.py and fix the problem.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 29, 2025

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copilot AI changed the title [WIP] Fix deadlock issue in array.fromfile during append Prevent array.fromfile reentrancy deadlock in append conversion Dec 29, 2025
Copilot AI requested a review from youknowone December 29, 2025 13:48
Copilot AI and others added 4 commits December 30, 2025 00:25
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
@youknowone youknowone force-pushed the copilot/fix-array-deadlock-issue branch from 20e60ba to 391a83d Compare December 29, 2025 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deadlock when array.fromfile re-enters append via __index__

2 participants