Deadlock when `array.fromfile` re-enters `append` via `__index__`
@youknowone ci sta già lavorando.
Dal 29/12/2025.
Valutazione
Questa issue non è ancora stata valutata.
Descrizione
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)
- Lingua principale
- Rust
- Stelle
- 22.4k
- Fork
- 1.5k
- Merge medio
- 14h 42m
- PR unite (30g)
- 174
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di RustPython/RustPython
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
RustPython/RustPython#8470 · 2 commenti ·
-
C-bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
RustPython/RustPython#7995 · 5 commenti ·
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
RustPython/RustPython#5577 ·
-
C-bug z-ca-2026
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
RustPython/RustPython#8765 · 1 reazione ·
-
C-bug
Difficoltà 3/5 1-2 giorni Idoneità per principianti 74/100
RustPython/RustPython#8764 ·
Tutte le issue di RustPython/RustPython
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
bitcoindevkit/bdk-ffi#1125 ·
-
Accept -c in more positions Apertaarea: compat bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
area:ci enhancement requires-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
apache/datafusion-comet#6078 ·
-
area: dogs bug priority: P3 silent failure test-code
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100