Skip to content

Commit

Permalink
fix(runtime): improve error message for timeouts (#4686)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoyvens authored Oct 10, 2023
1 parent d2a3219 commit 2bfd348
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
13 changes: 6 additions & 7 deletions runtime/test/src/test/abi.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
use graph::prelude::{ethabi::Token, web3::types::U256};
use graph_runtime_wasm::{
asc_abi::class::{
ArrayBuffer, AscAddress, AscEnum, AscEnumArray, EthereumValueKind, StoreValueKind,
TypedArray,
},
TRAP_TIMEOUT,
use graph_runtime_wasm::asc_abi::class::{
ArrayBuffer, AscAddress, AscEnum, AscEnumArray, EthereumValueKind, StoreValueKind, TypedArray,
};

use super::*;
Expand All @@ -23,7 +19,10 @@ async fn test_unbounded_loop(api_version: Version) {
.await
.0;
let res: Result<(), _> = module.get_func("loop").typed().unwrap().call(());
assert!(res.unwrap_err().to_string().contains(TRAP_TIMEOUT));
assert_eq!(
res.unwrap_err().to_string().lines().next().unwrap(),
"wasm trap: interrupt"
);
}

#[tokio::test(flavor = "multi_thread")]
Expand Down
3 changes: 0 additions & 3 deletions runtime/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,3 @@ pub use host::RuntimeHostBuilder;
pub use host_exports::HostExports;
pub use mapping::{MappingContext, ValidModule};
pub use module::{ExperimentalFeatures, WasmInstance};

#[cfg(debug_assertions)]
pub use module::TRAP_TIMEOUT;
16 changes: 10 additions & 6 deletions runtime/wasm/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use graph::data::value::Word;
use graph::slog::SendSyncRefUnwindSafeKV;
use never::Never;
use semver::Version;
use wasmtime::{Memory, Trap};
use wasmtime::{Memory, Trap, TrapCode};

use graph::blockchain::{Blockchain, HostFnCtx};
use graph::data::store;
Expand Down Expand Up @@ -43,8 +43,6 @@ use crate::mapping::ValidModule;
mod into_wasm_ret;
pub mod stopwatch;

pub const TRAP_TIMEOUT: &str = "trap: interrupt";

// Convenience for a 'top-level' asc_get, with depth 0.
fn asc_get<T, C: AscType, H: AscHeap + ?Sized>(
heap: &H,
Expand Down Expand Up @@ -266,8 +264,14 @@ impl<C: Blockchain> WasmInstance<C> {
return Err(MappingError::PossibleReorg(trap.into()));
}

// Treat as a special case to have a better error message.
Err(trap) if trap.to_string().contains(TRAP_TIMEOUT) => {
// Treat timeouts anywhere in the error chain as a special case to have a better error
// message. Any `TrapCode::Interrupt` is assumed to be a timeout.
Err(trap)
if Error::from(trap.clone()).chain().any(|e| {
e.downcast_ref::<Trap>().and_then(|t| t.trap_code())
== Some(TrapCode::Interrupt)
}) =>
{
self.instance_ctx_mut().ctx.state.exit_handler();
return Err(MappingError::Unknown(Error::from(trap).context(format!(
"Handler '{}' hit the timeout of '{}' seconds",
Expand All @@ -280,7 +284,7 @@ impl<C: Blockchain> WasmInstance<C> {
is_trap_deterministic(&trap) || self.instance_ctx().deterministic_host_trap;
let e = Error::from(trap);
match trap_is_deterministic {
true => Some(Error::from(e)),
true => Some(e),
false => {
self.instance_ctx_mut().ctx.state.exit_handler();
return Err(MappingError::Unknown(e));
Expand Down

0 comments on commit 2bfd348

Please sign in to comment.