Hello again, folks! I've been developing a scripting system using wasmtime/wasi/wit-bindgen, and it's coming along swimmingly. I have a simple async executor that just polls a list of futures in my guest, but those futures can panic, and I'd like to catch the panic and handle it gracefully (e.g. unschedule the future, print some reporting on the host, continue execution with the other futures.)
I know that wasm32-unknown-unknown defines panic=abort, and I was hoping that this wasn't the case for wasm32-wasi, but it appears to be (catch_unwind
doesn't do anything, and the guest still aborts.) Is there a way around this? I'd settle for having limited / no unwind functionality - the main thing I want is to be able to unschedule the future, so to remove it from the list.
I think this might be doable with a panic hook that does something gnarly with mutable state, but I'm not sure, and I also don't want to rely on any incidental behaviour. What would be the best way to handle this?
My executor is basically just this:
fn process(&mut self) {
self.futures.retain_mut(|f| f.as_mut().poll(&mut Context::from_waker(&self.waker)) == Poll::Pending);
}
where the poll
can panic from the user code.
Wasm doesn't have support for unwinding yet, do panic=abort is the only real option. (Emscripten has the asyncify postprocessing pass to support unwinding, but I don't think you can use it with plain LLVM like rustc does. Also I'm not sure if it supports running destructors, as required for soundness.)
There is an open proposal for adding native unwind support to wasm though.
Hmm, I feared that might be the case - how are people generally handling panics, then? Just structuring code/the execution environment to avoid them where possible?
Yes
@Philpax I am interested in learning more about the sripting system you are building! What is it about and what problems does it solve?
mossaka said:
Philpax I am interested in learning more about the sripting system you are building! What is it about and what problems does it solve?
Heya! I work on https://www.dims.co/ - we're a collaborative open-world game builder/engine in Rust - and I'm building a Wasm-based scripting interface with Rust as a guest, so that you can build gameplay and other features with your friends and iterate quickly. I'm actually planning on writing a post on the company blog about how the scripting works in the next few weeks - are there any specific questions you want/would want answered?
(We were previously using Lua, because I know that's a decent game scripting solution, but we're targeting Wasm now because a) Lua is very hard to make robust (which I can attest from prior experience) and b) it opens up opportunities for other languages (currently interested in TinyGo, Kotlin, and Grain))
That's super awesome! That's all I want to know! Glad to see more wasm plugin use cases~
Last updated: Dec 23 2024 at 13:07 UTC