infinyon / fluvio

Lean and mean distributed stream processing system written in rust and web assembly.
https://www.fluvio.io/
Apache License 2.0
2.81k stars 200 forks source link

Catch panic in SmartModule #2326

Open galibey opened 2 years ago

galibey commented 2 years ago

When the WASM compiled code in SmartModule panics the execution aborts and wasmtime engine returns wasm trap: wasm unreachable instruction executed. We need to discover the solution to handle the panic with proper error reporting to the user.

Related #2286

sehz commented 2 years ago

How about this? https://crates.io/crates/console_error_panic_hook

galibey commented 2 years ago

How about this? https://crates.io/crates/console_error_panic_hook

This is just a panic hook that forwards a message to JS native function. In theory, we could use a hook to store a message to some static memory and then use it to report an error but it's quite a hacky and the hook can conflict with the user's hook.

What we can do is to analyze wasm backtraces returned from func call in wasmtime and lookup for the function __rust_start_panic. If one is found, we report that the user's code panicked without providing any reasons or code positions.

galibey commented 2 years ago

Created an issue in wasmtime https://github.com/bytecodealliance/wasmtime/issues/4054

galibey commented 2 years ago

For WASI targeted modules we can capture stderr and in case of Trap returned from WebAssembly function invocation we can report the output to the user as an error. Example:

...
let shared_stderr = Arc::new(RwLock::new(Cursor::new(vec![])));
let wasi = WasiCtxBuilder::new()
        .stderr(Box::new(WritePipe::from_shared(shared_stderr.clone())))
        .build();
...
let result = instance.get_typed_func::<(), (), _>(&mut store, "filter");
///if result = Err(trap)
let cursor = shared_stderr.read().unwrap();
let error_message = String::from_utf8_lossy(cursor.get_ref());

If the module has no WASI, we can detect if Trap with UnreachableCodeReached and add the message that most probably the code panicked and advise to re-build the code to WASI to be able to debug the issue.

github-actions[bot] commented 2 years ago

Stale issue message