fluvio icon indicating copy to clipboard operation
fluvio copied to clipboard

Catch panic in SmartModule

Open galibey opened this issue 2 years ago • 5 comments

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

galibey avatar Apr 19 '22 14:04 galibey

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

sehz avatar Apr 19 '22 20:04 sehz

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 avatar Apr 20 '22 09:04 galibey

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

galibey avatar Apr 20 '22 10:04 galibey

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.

galibey avatar Apr 21 '22 09:04 galibey

Stale issue message

github-actions[bot] avatar Jun 22 '22 11:06 github-actions[bot]