feat: add catch_unwind protection to render to prevent Node.js process crashes#396
Merged
feat: add catch_unwind protection to render to prevent Node.js process crashes#396
catch_unwind protection to render to prevent Node.js process crashes#396Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Brooooooklyn
approved these changes
Jan 26, 2026
This comment has been minimized.
This comment has been minimized.
…s crashes - Use catch_unwind in the resvg-js Rust layer to capture panics, converting them into explicit errors to prevent direct panics from crashing the Node.js process. doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html - Remove unwrap() at critical points in the resvg-js rendering pipeline.
yisibl
commented
Jan 27, 2026
| Err(panic) => Err(Error::RenderPanic(panic_to_string(panic)).into()), | ||
| } | ||
| } | ||
| } |
Member
Author
There was a problem hiding this comment.
If future support for unwind is added to Wasm, it could be implemented this way:
// Share panic-to-error handling across Node.js and wasm targets.
macro_rules! render_inner_catch_unwind_impl {
($err_ty:ty) => {
fn render_inner_catch_unwind(&self) -> Result<RenderedImage, $err_ty> {
match std::panic::catch_unwind(AssertUnwindSafe(|| self.render_inner())) {
Ok(result) => result.map_err(Into::into),
Err(panic) => Err(Error::RenderPanic(panic_to_string(panic)).into()),
}
}
};
}
#[cfg(not(target_arch = "wasm32"))]
impl Resvg {
render_inner_catch_unwind_impl!(NapiError);
}
#[cfg(target_arch = "wasm32")]
impl Resvg {
render_inner_catch_unwind_impl!(js_sys::Error);
}For .cargo/config.toml
[target.wasm32-unknown-unknown]
rustflags = ["-Cpanic=unwind", "-Cllvm-args=-wasm-use-legacy-eh=false"]
[unstable]
build-std = ["std", "panic_unwind"]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
When the upstream resvg (Rust) encounters an SVG that cannot be rendered, it may panic. This causes the Node.js process to crash immediately, resulting in a core dump.
✅ Node.js
catch_unwindin the resvg-js Rust layer to capture panics, converting them into explicit errors to prevent direct panics from crashing the Node.js process.https://doc.rust-lang.org/book/ch09-01-unrecoverable-errors-with-panic.html
unwrap()at critical points in the resvg-js rendering pipeline.There are also some upstream PRs working towards this goal, but they haven't been merged yet:
🚧 Wasm
By default the
wasm32-unknown-unknowntarget is compiled with-Cpanic=abort. Historically this was due to the fact that there was no way to catch panics in wasm, but since mid-2025 the WebAssembly exception-handling proposal reached stabilization. LLVM has support for this proposal as well and when this is all combined together it's possible to enable-Cpanic=unwindon wasm targets.See https://doc.rust-lang.org/rustc/platform-support/wasm32-unknown-unknown.html#unwinding
However, considering that this would increase the size of the
wasmfile and require runtime support, we will not enable it at this time.