-
Notifications
You must be signed in to change notification settings - Fork 834
Concurrency in tests result in undefined behavior #288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Thanks for the report, but I fear this isn't sufficient to reproduce the bug. Could you provide e.g. a minimal lib.rs with two tests that show this behaviour? |
Yes I will try |
Hi, please see my repo: https://github.com/Rikorose/test_pyo3 |
Nice example! Thanks. fn _test(size: usize) {
static mut LOCKED: bool = false;
let gil = Python::acquire_gil();
unsafe {
println!("LOCKED_before: {}", LOCKED);
if !LOCKED {
LOCKED = true;
}
}
let mut x = Array1::random(size, F32(Normal::new(-1., 1.)));
let py_x = _test_pyo3(gil.python(), x.clone())
.map_err(|e| {
eprintln!("Error calling _test_pyo3(): {:?}", e);
e.print_and_set_sys_last_vars(gil.python());
})
.unwrap();
x.mapv_inplace(f32::exp);
assert_close(
&x.as_slice().unwrap(),
&py_x.as_slice().unwrap(),
1e-6.into(),
);
unsafe {
println!("LOCKED_after: {}", LOCKED);
if LOCKED {
LOCKED = false;
}
}
} and got
. So it's apparently codes are executed in parallel. |
Hi, is there any solution for this issue? Or we just have to use |
This looks like another case of #756 and can probably be closed. |
I was thinking the same thing. 🎉 (FYI the fix was included in the recent #887 ) |
🐛 Bug Reports
Maybe similar to #19.
I am testing my rust library against a python library. Therefor
pyo3::Python::acquire_gil()
is called in several tests. In each test i am setting up the imports like:When executing my tests with
RUST_BACKTRACE=full RUST_TEST_THREADS=1 cargo test --verbose --test spectrum
everything runs fine.
But for
RUST_BACKTRACE=full RUST_TEST_THREADS=2 cargo test --verbose --test spectrum
for some tests (not all!) fail with
or
(I use
np.abs()
,np
somehow became anint
)full trace
Sometimes I even get a
(signal: 11, SIGSEGV: invalid memory reference)
full trace
The
SIGSEGV
become more likely for more threads, e.g. aprox. 9/10 for 3 threads, while 2 threads its only 1/10.Am I using
pyo3::Python::acquire_gil()
wrong? Is this not thread-safe? As far as I can tell, this is called in every(?) test of this library.🌍 Environment
rustc --version
): rustc 1.32.0-nightly (13c943992 2018-11-18)version = "0.x.y"
withgit = "https://github.com/PyO3/pyo3")?
: I am using pyo3 version0.5.2
, git breaks compatibility withrust-numpy
💥 Reproducing
Please provide a minimal working example. This means both the rust code and the python.
Please also write what exact flags are required to reproduce your results.
The text was updated successfully, but these errors were encountered: