Stream: wasmtime

Topic: Catching/handling panics in wasm32-wasi


view this post on Zulip Philpax (Jul 28 2022 at 14:25):

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.

view this post on Zulip bjorn3 (Jul 28 2022 at 14:54):

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.)

view this post on Zulip bjorn3 (Jul 28 2022 at 14:55):

There is an open proposal for adding native unwind support to wasm though.

view this post on Zulip Philpax (Jul 28 2022 at 15:35):

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?

view this post on Zulip bjorn3 (Jul 28 2022 at 20:05):

Yes

view this post on Zulip Mossaka (Joe) (Jul 29 2022 at 17:14):

@Philpax I am interested in learning more about the sripting system you are building! What is it about and what problems does it solve?

view this post on Zulip Philpax (Jul 29 2022 at 20:52):

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))

Dims.co is a UGC platform where you create open world games with your friends in realtime.

view this post on Zulip Mossaka (Joe) (Jul 29 2022 at 21:26):

That's super awesome! That's all I want to know! Glad to see more wasm plugin use cases~


Last updated: Oct 23 2024 at 20:03 UTC