Skip to content
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

codes_index is potentially not thread safe #10

Open
Quba1 opened this issue Jan 29, 2024 · 0 comments
Open

codes_index is potentially not thread safe #10

Quba1 opened this issue Jan 29, 2024 · 0 comments

Comments

@Quba1
Copy link
Member

Quba1 commented Jan 29, 2024

ecCodes 2.33.0 manual build with POSIX-threads enabled.

Minimal example:

use std::{ffi::CString, thread};

fn main() {
    let t1 = thread::spawn(|| thread1("T1"));
    let t2 = thread::spawn(|| thread1("T3"));

    t1.join().unwrap();
    t2.join().unwrap();
}

fn thread1(name: &str) {
    let keys = CString::new("shortName, typeOfLevel").unwrap();
    let grib_file = CString::new("./data/iceland-surface.grib").unwrap();

    let mut err = 0;
    let ci;
    unsafe {
        let ctx = eccodes_sys::codes_context_get_default();

        println!("{name}: index_new");
        ci = eccodes_sys::codes_index_new(ctx, keys.as_ptr(), &mut err);
        println!("{name}: index_new: {err:?}");

        println!("{name}: index_add_file");
        err = eccodes_sys::codes_index_add_file(ci, grib_file.as_ptr());
        println!("{name}: index_add_file: {err:?}");

        thread::sleep(std::time::Duration::from_secs(3));

        println!("{name}: index_delete");
        eccodes_sys::codes_index_delete(ci);
        println!("{name}: index deleted");
    }
}

With eventually fail like so:

 cargo run                                                                                                                                
    Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/ec-test`
T1: index_new
T3: index_new
T1: index_new: 0
T1: index_add_file
T3: index_new: 0
T3: index_add_file
T1: index_add_file: -11
[1]    112709 segmentation fault (core dumped)  cargo run
@Quba1 Quba1 mentioned this issue Jan 29, 2024
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

When branches are created from issues, their pull requests are automatically linked.

1 participant