clockworklabs/SpacetimeDB · error

Trapped while evaluating views during database update

Error message

Trapped while evaluating views during database update

What it means

During a database update (module publish/migration) the host evaluates views inside the update transaction. If that evaluation traps (a WASM trap such as unreachable or abort inside view code), the transaction is rolled back, tx metrics are reported, and the update returns ErrorExecutingMigration with this message. The database keeps the old program and schema.

Solutions

  1. Fix the view logic to tolerate both pre-migration and post-migration row shapes.
  2. Test the update against a copy of production data before deploying.
  3. Remove panic paths from views: no unwrap, unchecked indexing, or overflowing arithmetic.
  4. Retry the publish after fixing; the rollback guarantees a consistent old state.

Example fix

// before (view in module, Rust)
let score: u64 = ctx.args.score.unwrap(); // traps when the field is absent during migration

// after
let score: u64 = ctx.args.score.unwrap_or_default(); // tolerate old rows
Defensive patterns

Strategy: try-catch

Try / catch

Match on UpdateDatabaseResult::ErrorExecutingMigration(err) and branch on 'Trapped while evaluating views': keep the old program running and route the error to a migration-fix workflow instead of retrying.

Prevention

When it happens

Trigger: Publishing an update whose view code traps on existing data - e.g. reads a column the migration removes, unwraps a None, or indexes out of bounds; view logic assuming the new schema while rows still hold old shapes.

Common situations: Schema migrations where views are not updated in tandem; untested views over production data; arithmetic overflow or NaN paths in views.

Related errors


AI-assisted analysis of clockworklabs/SpacetimeDB@eddf9f5014 (2026-09-20). Data as JSON: /api/errors/105d901d539dbb60. Report an issue: GitHub.

Appendix: source

Thrown at crates/core/src/host/wasm_common/module_host_actor.rs:807

                let effects = UpdateEffects::after_migration(res, &tx);
                let res = if effects.refresh_views {
                    // Resolve surviving materializations through the new module,
                    // even when this migration also requires client disconnection.
                    let (out, _, trapped) = self.evaluate_subscribed_views(tx, inst)?;
                    tx = out.tx;
                    if trapped || out.outcome != ViewOutcome::Success {
                        let msg = match trapped {
                            true => "Trapped while evaluating views during database update".to_string(),
                            false => format!(
                                "Views evaluation did not complete successfully during database update: {:?}",
                                out.outcome
                            ),
                        };

                        let (_, tx_metrics, reducer) = stdb.rollback_mut_tx(tx);
                        stdb.report_mut_tx_metrics(reducer, tx_metrics, None);
                        UpdateDatabaseResult::ErrorExecutingMigration(anyhow::anyhow!(msg))
                    } else {
                        let tx_offset = succeed(self.info.clone(), out.execution_budget_used, out.total_duration, tx);
                        effects.committed(tx_offset, durable_offset)
                    }
                } else {
                    let tx_offset = succeed(self.info.clone(), FunctionBudget::ZERO, Duration::ZERO, tx);
                    effects.committed(tx_offset, durable_offset)
                };

                Ok(res)
            }
        }
    }

    /// Apply an environment publication using the installed module instance. No
    /// initialization, migration, program replacement, or scheduler restart occurs.
    fn update_environment<I: WasmInstance>(
        &mut self,

View on GitHub (pinned to eddf9f5014)