Skip to content

corrupted double-linked list #392

Closed
Closed

Description

@maaft

When running below program with rustc 1.86.0 on x86_64 ubuntu 22.04 with TensorRT EP (Needs GPU).

Does not occur every time. Increase num_sessions to increase probability of occuring.

image_resize.onnx (Expires in 7 days).

use ndarray::Array;
use ort::{
    execution_providers::TensorRTExecutionProvider,
    inputs,
    session::{builder::GraphOptimizationLevel, Session},
    value::Value,
};
use std::{sync::Arc, thread};

pub fn main() {
    let model_bytes = std::include_bytes!("image_resize.onnx");

    let mut sessions = vec![];
    let mut handles = vec![];
    let num_sessions = 200;

    for i in 0..num_sessions {
        let session_builder = Session::builder()
            .unwrap()
            .with_optimization_level(GraphOptimizationLevel::Level3)
            .unwrap()
            .with_intra_threads(1)
            .unwrap()
            .with_execution_providers([TensorRTExecutionProvider::default()
                .with_device_id(0)
                .build()
                .error_on_failure()])
            .unwrap();

        let session = Arc::new(
            session_builder
                .commit_from_memory(model_bytes)
                .unwrap_or_else(|e| {
                    panic!("Failed to create session {}: {:?}", i, e);
                }),
        );

        sessions.push(session.clone());
    }
    for (i, session) in sessions.iter().enumerate() {
        let session = session.clone();
        let input_tensor = Value::from_array(
            Array::from_shape_vec(vec![1, 3, 12, 12], vec![1.0f32; 1 * 3 * 12 * 12]).unwrap(),
        )
        .unwrap();
        let target_size_tensor =
            Value::from_array(Array::from_shape_vec(vec![2], vec![6i64, 6]).unwrap()).unwrap();

        let handle = thread::spawn(move || {
            let result = session.run(
                inputs!["image" => input_tensor, "target_size" => target_size_tensor].unwrap(),
            );

            match result {
                Ok(_) => {
                    println!("Thread {} completed successfully.", i);
                }
                Err(e) => {
                    eprintln!("Thread {} session run failed: {:?}", i, e);
                }
            }
        });
        handles.push(handle);
    }

    println!(
        "All {} threads spawned. Waiting for completion...",
        num_sessions
    );

    for (idx, handle) in handles.into_iter().enumerate() {
        match handle.join() {
            Ok(_) => {}
            Err(e) => {
                eprintln!("Thread {} panicked: {:?}", idx, e);
            }
        }
    }

    println!("All session runs completed.");
}

Activity

  1. self-assigned this
    on May 9, 2025
  2. added
    bug
    p: high
    on May 9, 2025
  3. maaft commented on May 12, 2025

    @maaft
    Author

    related (temporary fix):

    This is my attempt for a temporary fix to above bug: Create and use sessions in the same thread. While doing this, another bug occured

    thread '<unnamed>' panicked at libdenkflow-pipeline/src/main.rs:54:29:
    Failed to create session 42: Error { code: GenericFailure, msg: "Trying to add a domain to DomainToVersion map, but the domain is already exist with version range (1, 1000). domain: \"trt.plugins\"\n" }
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    
    thread '<unnamed>' panicked at libdenkflow-pipeline/src/main.rs:54:29:
    Failed to create session 18: Error { code: GenericFailure, msg: "Trying to add a domain to DomainToVersion map, but the domain is already exist with version range (1, 1000). domain: \"trt.plugins\"\n" }
    
    thread '<unnamed>' panicked at libdenkflow-pipeline/src/main.rs:54:29:
    Failed to create session 6: Error { code: GenericFailure, msg: "Trying to add a domain to DomainToVersion map, but the domain is already exist with version range (1, 1000). domain: \"trt.plugins\"\n" }
    
    thread '<unnamed>' panicked at libdenkflow-pipeline/src/main.rs:54:29:
    Failed to create session 14: Error { code: GenericFailure, msg: "Trying to add a domain to DomainToVersion map, but the domain is already exist with version range (1, 1000). domain: \"trt.plugins\"\n" }
    
    thread '<unnamed>' panicked at libdenkflow-pipeline/src/main.rs:54:29:
    Failed to create session 17: Error { code: GenericFailure, msg: "Trying to add a domain to DomainToVersion map, but the domain is already exist with version range (1, 1000). domain: \"trt.plugins\"\n" }
    Thread 159 completed successfully.

    When we guard session creation by a global mutex (see comments), this is also fixed.

    Related upstream issue (but unfortunately just closed without explanation): https://github.com/microsoft/onnxruntime/issues/22663

    use ndarray::Array;
    use ort::{
        execution_providers::{CUDAExecutionProvider, TensorRTExecutionProvider},
        inputs,
        session::{builder::GraphOptimizationLevel, Session},
        value::Value,
    };
    use parking_lot::Mutex;
    use std::{sync::Arc, thread};
    
    pub fn main() {
        let model_bytes = std::include_bytes!("image_resize.onnx");
    
        let mut handles = vec![];
        let num_sessions = 200;
    
        // uncomment for a "fix"
        // let l = Arc::new(Mutex::new(false));
    
        for i in 0..num_sessions {
            // sessions.push(session.clone());
            let input_tensor = Value::from_array(
                Array::from_shape_vec(vec![1, 3, 12, 12], vec![1.0f32; 1 * 3 * 12 * 12]).unwrap(),
            )
            .unwrap();
            let target_size_tensor =
                Value::from_array(Array::from_shape_vec(vec![2], vec![6i64, 6]).unwrap()).unwrap();
    
            // uncomment for a "fix"
            // let l = l.clone();
    
            let handle = thread::spawn(move || {
                let session_builder = Session::builder()
                    .unwrap()
                    .with_optimization_level(GraphOptimizationLevel::Level3)
                    .unwrap()
                    .with_intra_threads(1)
                    .unwrap()
                    .with_execution_providers([TensorRTExecutionProvider::default()
                        .with_device_id(0)
                        .build()
                        .error_on_failure()])
                    .unwrap();
    
                let session = {
                    // uncomment for a "fix"
                    // let mut x = l.lock();
                    // *x = !*x;
    
                    let session = Arc::new(
                        session_builder
                            .commit_from_memory(model_bytes)
                            .unwrap_or_else(|e| {
                                panic!("Failed to create session {}: {:?}", i, e);
                            }),
                    );
    
                    session
                };
    
                let result = session.run(
                    inputs!["image" => input_tensor, "target_size" => target_size_tensor].unwrap(),
                );
    
                match result {
                    Ok(_) => {
                        println!("Thread {} completed successfully.", i);
                    }
                    Err(e) => {
                        eprintln!("Thread {} session run failed: {:?}", i, e);
                    }
                }
            });
            handles.push(handle);
        }
    
        println!(
            "All {} threads spawned. Waiting for completion...",
            num_sessions
        );
    
        for (idx, handle) in handles.into_iter().enumerate() {
            match handle.join() {
                Ok(_) => {}
                Err(e) => {
                    eprintln!("Thread {} panicked: {:?}", idx, e);
                }
            }
        }
    
        println!("All session runs completed.");
    }
  4. added a commit that references this issue on Jul 17, 2025
    77ec493
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugp: high

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions