Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Lib/test/_test_eintr.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ def test_readinto(self):
self.assertEqual(os.readinto(fd, buffer), len(expected))
self.assertEqual(buffer, expected)

@unittest.expectedFailure # TODO: RUSTPYTHON; InterruptedError: [Errno 4] Interrupted system call
def test_write(self):
rd, wr = os.pipe()
self.addCleanup(os.close, wr)
Expand Down
2 changes: 0 additions & 2 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4814,8 +4814,6 @@ def test_finalize(self):
self.assertEqual(result, ['a', 'b', 'd10', 'd03', 'd02', 'd01', 'e'])

@support.requires_resource('cpu')
# TODO: RUSTPYTHON; dict iteration races with concurrent GC mutations
@unittest.expectedFailure
def test_thread_safety(self):
# bpo-24484: _run_finalizers() should be thread-safe
def cb():
Expand Down
19 changes: 13 additions & 6 deletions crates/vm/src/stdlib/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,19 @@ pub(super) mod _os {
}

#[pyfunction]
fn write(
fd: crt_fd::Borrowed<'_>,
data: ArgBytesLike,
vm: &VirtualMachine,
) -> io::Result<usize> {
data.with_ref(|b| vm.allow_threads(|| crt_fd::write(fd, b)))
fn write(fd: crt_fd::Borrowed<'_>, data: ArgBytesLike, vm: &VirtualMachine) -> PyResult<usize> {
data.with_ref(|b| {
loop {
match vm.allow_threads(|| crt_fd::write(fd, b)) {
Ok(n) => return Ok(n),
Err(e) if e.raw_os_error() == Some(libc::EINTR) => {
vm.check_signals()?;
continue;
}
Err(e) => return Err(e.into_pyexception(vm)),
}
}
})
}

#[cfg(not(windows))]
Expand Down
14 changes: 10 additions & 4 deletions crates/vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
self, PyBaseExceptionRef, PyDict, PyDictRef, PyInt, PyList, PyModule, PyStr, PyStrInterned,
PyStrRef, PyTypeRef, PyUtf8Str, PyUtf8StrInterned, PyWeak,
code::PyCode,
dict::{PyDictItems, PyDictValues},
dict::{PyDictItems, PyDictKeys, PyDictValues},
pystr::AsPyStr,
tuple::PyTuple,
},
Expand Down Expand Up @@ -1822,24 +1822,30 @@ impl VirtualMachine {
where
F: Fn(PyObjectRef) -> PyResult<T>,
{
// Extract elements from item, if possible:
// Type-specific fast paths corresponding to _list_extend() in CPython
// Objects/listobject.c. Each branch takes an atomic snapshot to avoid
// race conditions from concurrent mutation (no GIL).
let cls = value.class();
let list_borrow;
let slice = if cls.is(self.ctx.types.tuple_type) {
value.downcast_ref::<PyTuple>().unwrap().as_slice()
} else if cls.is(self.ctx.types.list_type) {
list_borrow = value.downcast_ref::<PyList>().unwrap().borrow_vec();
&list_borrow
} else if cls.is(self.ctx.types.dict_type) {
let keys = value.downcast_ref::<PyDict>().unwrap().keys_vec();
return keys.into_iter().map(func).collect();
} else if cls.is(self.ctx.types.dict_keys_type) {
let keys = value.downcast_ref::<PyDictKeys>().unwrap().dict.keys_vec();
return keys.into_iter().map(func).collect();
} else if cls.is(self.ctx.types.dict_values_type) {
// Atomic snapshot of dict values - prevents race condition during iteration
let values = value
.downcast_ref::<PyDictValues>()
.unwrap()
.dict
.values_vec();
return values.into_iter().map(func).collect();
} else if cls.is(self.ctx.types.dict_items_type) {
// Atomic snapshot of dict items - prevents race condition during iteration
let items = value
.downcast_ref::<PyDictItems>()
.unwrap()
Expand Down
Loading