fix: let Environment drop, closes #441

Let it die, let it die! You shall die

Instead of creating an environment that lives throughout the duration of the process, we now hold the environment options we commit, and create the environment from those options (or grab the current env) whenever we need it. When all holders of the environment are dropped, the environment is dropped as well.

Previously, we held `Environment` as a static variable. Statics are never dropped, but ONNX Runtime's own destructors assumes that the environment is long gone by the time the process exits, which was not the case in `ort`! This led to issues like #441 and the dumb `0003-leak-logger-mutex.patch` from `ort-artifacts`.
This commit is contained in:
Carson M.
2025-08-31 18:57:38 -05:00
parent 6c0a32a029
commit 317be209b1
24 changed files with 146 additions and 139 deletions

View File

@@ -77,7 +77,7 @@ impl Operator for CustomOpTwo {
}
fn main() -> ort::Result<()> {
let _env = ort::init().with_execution_providers([CPUExecutionProvider::default().build()]).commit()?;
ort::init().with_execution_providers([CPUExecutionProvider::default().build()]).commit();
let mut session = Session::builder()?
.with_operators(OperatorDomain::new("test.customop")?.add(CustomOpOne)?.add(CustomOpTwo)?)?

View File

@@ -12,7 +12,7 @@ use ort::{
fn mnist_5() -> ort::Result<()> {
const IMAGE_TO_LOAD: &str = "mnist_5.jpg";
ort::init().with_name("integration_test").commit()?;
ort::init().with_name("integration_test").commit();
let mut session = Session::builder()?
.with_optimization_level(GraphOptimizationLevel::Level1)?

View File

@@ -17,7 +17,7 @@ use ort::{
fn squeezenet_mushroom() -> ort::Result<()> {
const IMAGE_TO_LOAD: &str = "mushroom.png";
ort::init().with_name("integration_test").commit()?;
ort::init().with_name("integration_test").commit();
let mut session = Session::builder()?
.with_optimization_level(GraphOptimizationLevel::Level1)?

View File

@@ -40,7 +40,7 @@ struct StdThreadManager {
impl ThreadManager for StdThreadManager {
type Thread = StdThread;
fn create(&mut self, worker: ThreadWorker) -> ort::Result<Self::Thread> {
fn create(&self, worker: ThreadWorker) -> ort::Result<Self::Thread> {
Ok(StdThread::spawn(worker, &self.stats))
}
@@ -62,7 +62,7 @@ fn global_thread_manager() -> ort::Result<()> {
.with_intra_threads(2)?
.with_thread_manager(StdThreadManager { stats: Arc::clone(&stats) })?
)
.commit()?;
.commit();
assert_eq!(stats.active_threads.load(Ordering::Acquire), 4);

View File

@@ -49,7 +49,7 @@ fn convert_image_to_cow_array(img: &RgbImage) -> CowArray<'_, f32, Ix4> {
fn upsample() -> ort::Result<()> {
const IMAGE_TO_LOAD: &str = "mushroom.png";
ort::init().with_name("integration_test").commit()?;
ort::init().with_name("integration_test").commit();
let session_data =
std::fs::read(Path::new(env!("CARGO_MANIFEST_DIR")).join("tests").join("data").join("upsample.onnx")).expect("Could not open model from file");
@@ -92,7 +92,7 @@ fn upsample() -> ort::Result<()> {
fn upsample_with_ort_model() -> ort::Result<()> {
const IMAGE_TO_LOAD: &str = "mushroom.png";
ort::init().with_name("integration_test").commit()?;
ort::init().with_name("integration_test").commit();
let session_data =
std::fs::read(Path::new(env!("CARGO_MANIFEST_DIR")).join("tests").join("data").join("upsample.ort")).expect("Could not open model from file");