Skip to content

Commit 8693514

Browse files
authored
drop PyErr in do_init while still holding the GIL (#157)
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 <[email protected]>
1 parent 56fee39 commit 8693514

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

runtime/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,12 @@ fn componentize_py_module(_py: Python<'_>, module: &Bound<PyModule>) -> PyResult
196196
module.add_function(pyo3::wrap_pyfunction!(drop_resource, module)?)
197197
}
198198

199-
fn do_init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<()> {
199+
fn do_init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<(), String> {
200200
pyo3::append_to_inittab!(componentize_py_module);
201201

202202
pyo3::prepare_freethreaded_python();
203203

204-
Python::with_gil(|py| {
204+
let init = |py: Python| {
205205
let app = match py.import(app_name.as_str()) {
206206
Ok(app) => app,
207207
Err(e) => {
@@ -390,15 +390,17 @@ fn do_init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<()> {
390390

391391
ARGV.set(argv.into()).unwrap();
392392

393-
Ok(())
394-
})
393+
Ok::<_, Error>(())
394+
};
395+
396+
Python::with_gil(|py| init(py).map_err(|e| format!("{e:?}")))
395397
}
396398

397399
struct MyExports;
398400

399401
impl Guest for MyExports {
400402
fn init(app_name: String, symbols: Symbols, stub_wasi: bool) -> Result<(), String> {
401-
let result = do_init(app_name, symbols, stub_wasi).map_err(|e| format!("{e:?}"));
403+
let result = do_init(app_name, symbols, stub_wasi);
402404

403405
// This tells the WASI Preview 1 component adapter to reset its state. In particular, we want it to forget
404406
// about any open handles and re-request the stdio handles at runtime since we'll be running under a brand

0 commit comments

Comments
 (0)