Skip to content

Commit 847eba3

Browse files
goffrieConvex, Inc.
authored andcommitted
isolate2: Catch v8 op panics in tests to improve debugging (#34991)
GitOrigin-RevId: a1a99d3b7152b771fcc58eb864311df21f833d4f
1 parent 45afe81 commit 847eba3

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

crates/isolate/src/isolate2/callback_context.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,16 @@ impl<'callback, 'scope: 'callback> CallbackContext<'callback, 'scope> {
154154
rv: v8::ReturnValue,
155155
) {
156156
let mut ctx = CallbackContext::new(scope);
157-
if let Err(e) = run_op(&mut ctx, args, rv) {
157+
#[cfg(test)]
158+
let r = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
159+
run_op(&mut ctx, args, rv)
160+
})) {
161+
Ok(r) => r,
162+
Err(e) => Err(crate::test_helpers::PanicError::new(e).into()),
163+
};
164+
#[cfg(not(test))]
165+
let r = run_op(&mut ctx, args, rv);
166+
if let Err(e) = r {
158167
ctx.handle_syscall_or_op_error(e);
159168
}
160169
}
@@ -165,7 +174,16 @@ impl<'callback, 'scope: 'callback> CallbackContext<'callback, 'scope> {
165174
rv: v8::ReturnValue,
166175
) {
167176
let mut ctx = CallbackContext::new(scope);
168-
if let Err(e) = start_async_op(&mut ctx, args, rv) {
177+
#[cfg(test)]
178+
let r = match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
179+
start_async_op(&mut ctx, args, rv)
180+
})) {
181+
Ok(r) => r,
182+
Err(e) => Err(crate::test_helpers::PanicError::new(e).into()),
183+
};
184+
#[cfg(not(test))]
185+
let r = start_async_op(&mut ctx, args, rv);
186+
if let Err(e) = r {
169187
ctx.handle_syscall_or_op_error(e);
170188
}
171189
}

crates/isolate/src/isolate2/entered_context.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,14 @@ impl<'enter, 'scope: 'enter> EnteredContext<'enter, 'scope> {
135135
if let Some(failure) = context_state.failure.take() {
136136
match failure {
137137
ContextFailure::UncatchableDeveloperError(js_error) => anyhow::bail!(js_error),
138-
ContextFailure::SystemError(error) => anyhow::bail!(error),
138+
ContextFailure::SystemError(error) => {
139+
#[cfg(test)]
140+
let error = match error.downcast::<crate::test_helpers::PanicError>() {
141+
Ok(panic) => std::panic::resume_unwind(panic.into_inner()),
142+
Err(e) => e,
143+
};
144+
anyhow::bail!(error)
145+
},
139146
}
140147
} else if self.heap_context.oomed() {
141148
// TODO: do the rest of the logging that isolate1 does

crates/isolate/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(const_type_name)]
2+
#![feature(exclusive_wrapper)]
23
#![feature(try_blocks)]
34
#![feature(iterator_try_collect)]
45
#![feature(type_alias_impl_trait)]

crates/isolate/src/test_helpers.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use std::{
2+
any::Any,
23
collections::BTreeMap,
34
fs::File,
45
io::Read,
56
sync::{
67
Arc,
8+
Exclusive,
79
LazyLock,
810
},
911
time::Duration,
@@ -241,6 +243,29 @@ pub fn test_environment_data<RT: Runtime>(rt: RT) -> anyhow::Result<EnvironmentD
241243
})
242244
}
243245

246+
/// Indicates that a V8 op panicked and we should panic the test
247+
#[derive(thiserror::Error)]
248+
#[error("panicked")]
249+
// Wrap the error in `Exclusive` to make this type `Sync` so that it can be
250+
// wrapped by anyhow
251+
pub struct PanicError(Exclusive<Box<dyn Any + Send>>);
252+
253+
impl PanicError {
254+
pub fn new(panic: Box<dyn Any + Send>) -> Self {
255+
Self(Exclusive::new(panic))
256+
}
257+
258+
pub fn into_inner(self) -> Box<dyn Any + Send> {
259+
self.0.into_inner()
260+
}
261+
}
262+
263+
impl std::fmt::Debug for PanicError {
264+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
265+
f.debug_tuple("PanicError").finish()
266+
}
267+
}
268+
244269
pub struct UdfTest<RT: Runtime, P: Persistence> {
245270
pub database: Database<RT>,
246271
pub isolate: IsolateClient<RT>,

0 commit comments

Comments
 (0)