Skip to content

fix: make sure we don't panic when an exception is thrown in an async context #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ test: compile-examples
echo "Got: $$error_msg"; \
exit 1; \
fi
@output=$$(extism call examples/async_exception.wasm main --wasi --log-level info 2>&1); \
if echo "$$output" | grep -q "wasm error: unreachable"; then \
echo "Test failed - found 'wasm error: unreachable' in output"; \
echo "Output: $$output"; \
exit 1; \
else \
echo "Test passed - no unreachable error in async_exception"; \
fi

compile-examples: cli
cd examples/react && npm install && npm run build && cd ../..
Expand All @@ -82,6 +90,7 @@ compile-examples: cli
./target/release/extism-js examples/console/script.js -i examples/console/script.d.ts -o examples/console.wasm
./target/release/extism-js examples/base64/script.js -i examples/base64/script.d.ts -o examples/base64.wasm
./target/release/extism-js examples/try-catch/script.js -i examples/try-catch/script.d.ts -o examples/try-catch.wasm
cd examples/async_exception && npm install && npm run build && cd ../..

kitchen:
cd examples/kitchen-sink && npm install && npm run build && cd ../..
Expand Down
25 changes: 21 additions & 4 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,26 @@ static CALL_ARGS: std::sync::Mutex<Vec<Vec<ArgType>>> = std::sync::Mutex::new(ve
fn check_exception(this: &Ctx, err: rquickjs::Error) -> anyhow::Error {
let s = match err {
rquickjs::Error::Exception => {
let err = this.catch().into_exception().unwrap();
let msg = err.message().unwrap_or_default();
format!("Exception: {}\n{}", msg, err.stack().unwrap_or_default())
// Check if we got a valid exception or if it's uninitialized (possibly due to async code)
// - https://github.com/DelSkayn/rquickjs/issues/421
// - https://github.com/DelSkayn/rquickjs/pull/422
// - https://github.com/quickjs-ng/quickjs/issues/39
// - https://github.com/quickjs-ng/quickjs/pull/1038

match this.catch().into_exception() {
Some(err) => {
let msg = err.message().unwrap_or_default();
let stack = err.stack().unwrap_or_default();
format!("Exception: {}\n{}", msg, stack)
}
None => {
format!(
"Uncaught exception in async context. \
Async/Promise operations are not supported in this environment. \
Please use only synchronous code."
)
}
}
}
err => err.to_string(),
};
Expand Down Expand Up @@ -102,7 +119,7 @@ fn invoke<'a, T, F: for<'b> Fn(Ctx<'b>, Value<'b>) -> T>(
let function_invocation_result = function.call_arg(args);

while ctx.execute_pending_job() {
continue
continue;
}

match function_invocation_result {
Expand Down
2 changes: 2 additions & 0 deletions examples/async_exception/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
dist/
12 changes: 12 additions & 0 deletions examples/async_exception/esbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const esbuild = require('esbuild');

esbuild
.build({
entryPoints: ['src/index.ts'],
outdir: 'dist',
bundle: true,
sourcemap: true,
minify: false, // might want to use true for production build
format: 'cjs', // needs to be CJS for now
target: ['es2020'] // don't go over es2020 because quickjs doesn't support it
})
Loading
Loading