From ebc5e168f35924b158a66adbc651a525e35735a9 Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Mon, 23 Jun 2025 08:33:13 -0600 Subject: [PATCH] drop `PyErr` in `do_init` while still holding the GIL This avoids `Cannot drop pointer into Python heap without the GIL being held` errors which tend to prevent the real error from being printed. Fixes #156 Signed-off-by: Joel Dice --- runtime/src/lib.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index c9a9640..68d906a 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -196,12 +196,12 @@ fn componentize_py_module(_py: Python<'_>, module: &Bound) -> PyResult module.add_function(pyo3::wrap_pyfunction!(drop_resource, module)?) } -fn do_init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<()> { +fn do_init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<(), String> { pyo3::append_to_inittab!(componentize_py_module); pyo3::prepare_freethreaded_python(); - Python::with_gil(|py| { + let init = |py: Python| { let app = match py.import(app_name.as_str()) { Ok(app) => app, Err(e) => { @@ -390,15 +390,17 @@ fn do_init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<()> { ARGV.set(argv.into()).unwrap(); - Ok(()) - }) + Ok::<_, Error>(()) + }; + + Python::with_gil(|py| init(py).map_err(|e| format!("{e:?}"))) } struct MyExports; impl Guest for MyExports { fn init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<(), String> { - let result = do_init(app_name, symbols, stub_wasi).map_err(|e| format!("{e:?}")); + let result = do_init(app_name, symbols, stub_wasi); // This tells the WASI Preview 1 component adapter to reset its state. In particular, we want it to forget // about any open handles and re-request the stdio handles at runtime since we'll be running under a brand