Skip to content

Commit 04a4d1e

Browse files
Copilotyouknowone
andauthored
Prevent array.tofile re-entrant deadlock and add regression snippet (#6559)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
1 parent f8199d7 commit 04a4d1e

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

crates/stdlib/src/array.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -938,8 +938,10 @@ mod array {
938938
/* XXX Make the block size settable */
939939
const BLOCKSIZE: usize = 64 * 1024;
940940

941-
let bytes = self.read();
942-
let bytes = bytes.get_bytes();
941+
let bytes = {
942+
let bytes = self.read();
943+
bytes.get_bytes().to_vec()
944+
};
943945

944946
for b in bytes.chunks(BLOCKSIZE) {
945947
let b = PyBytes::from(b.to_vec()).into_ref(&vm.ctx);

extra_tests/snippets/stdlib_array.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,20 @@ def test_array_frombytes():
126126
a = array("B", [0])
127127
assert a.__contains__(0)
128128
assert not a.__contains__(1)
129+
130+
131+
class _ReenteringWriter:
132+
def __init__(self, arr):
133+
self.arr = arr
134+
self.reentered = False
135+
136+
def write(self, chunk):
137+
if not self.reentered:
138+
self.reentered = True
139+
self.arr.append(0)
140+
return len(chunk)
141+
142+
143+
arr = array("b", range(128))
144+
arr.tofile(_ReenteringWriter(arr))
145+
assert len(arr) == 129

0 commit comments

Comments
 (0)