fluvio
fluvio copied to clipboard
Catch panic in SmartModule
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
How about this? https://crates.io/crates/console_error_panic_hook
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.
Created an issue in wasmtime
https://github.com/bytecodealliance/wasmtime/issues/4054
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.
Stale issue message