Skip to content

Commit 8f3bf0f

Browse files
committed
Fix AppExit panic.
1 parent 28e08f6 commit 8f3bf0f

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

crates/processing_core/src/lib.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,21 @@ thread_local! {
1414
}
1515

1616
pub fn app_mut<T>(cb: impl FnOnce(&mut App) -> error::Result<T>) -> error::Result<T> {
17-
let res = APP.with(|app_cell| {
18-
let mut app_borrow = app_cell.borrow_mut();
17+
// `try_with` so a `Drop` running after the TLS is destroyed sees an
18+
// `AppAccess` error rather than panicking
19+
let res = APP.try_with(|app_cell| {
20+
let mut app_borrow = app_cell
21+
.try_borrow_mut()
22+
.map_err(|_| error::ProcessingError::AppAccess)?;
1923
let app = app_borrow
2024
.as_mut()
2125
.ok_or(error::ProcessingError::AppAccess)?;
2226
cb(app)
23-
})?;
24-
Ok(res)
27+
});
28+
match res {
29+
Ok(inner) => inner,
30+
Err(_) => Err(error::ProcessingError::AppAccess),
31+
}
2532
}
2633

2734
pub fn is_already_init() -> error::Result<bool> {

crates/processing_pyo3/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,7 +1017,7 @@ mod mewnala {
10171017
return Ok(());
10181018
}
10191019

1020-
Python::attach(|py| {
1020+
let result: PyResult<()> = Python::attach(|py| {
10211021
let builtins = PyModule::import(py, "builtins")?;
10221022
let locals = builtins.getattr("locals")?.call0()?;
10231023

@@ -1138,7 +1138,13 @@ mod mewnala {
11381138
}
11391139

11401140
Ok(())
1141-
})
1141+
});
1142+
1143+
// tear the app down here while the TLS is still alive; the eager
1144+
// TLS destructor aborts inside a Bevy resource drop
1145+
let _ = ::processing::exit(0);
1146+
1147+
result
11421148
}
11431149

11441150
#[pyfunction]

0 commit comments

Comments
 (0)