ScaleWeather / eccodes

Unofficial high-level Rust bindings of the latest ecCodes release.
Apache License 2.0
7 stars 6 forks source link

`codes_index` is potentially not thread safe #10

Open Quba1 opened 8 months ago

Quba1 commented 8 months ago

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