{"record":{"id":"105d901d539dbb60","repo":"clockworklabs/SpacetimeDB","slug":"trapped-while-evaluating-views-during-database-upd","errorCode":null,"errorMessage":"Trapped while evaluating views during database update","messagePattern":"Trapped while evaluating views during database update","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/core/src/host/wasm_common/module_host_actor.rs","lineNumber":807,"sourceCode":"\n                let effects = UpdateEffects::after_migration(res, &tx);\n                let res = if effects.refresh_views {\n                    // Resolve surviving materializations through the new module,\n                    // even when this migration also requires client disconnection.\n                    let (out, _, trapped) = self.evaluate_subscribed_views(tx, inst)?;\n                    tx = out.tx;\n                    if trapped || out.outcome != ViewOutcome::Success {\n                        let msg = match trapped {\n                            true => \"Trapped while evaluating views during database update\".to_string(),\n                            false => format!(\n                                \"Views evaluation did not complete successfully during database update: {:?}\",\n                                out.outcome\n                            ),\n                        };\n\n                        let (_, tx_metrics, reducer) = stdb.rollback_mut_tx(tx);\n                        stdb.report_mut_tx_metrics(reducer, tx_metrics, None);\n                        UpdateDatabaseResult::ErrorExecutingMigration(anyhow::anyhow!(msg))\n                    } else {\n                        let tx_offset = succeed(self.info.clone(), out.execution_budget_used, out.total_duration, tx);\n                        effects.committed(tx_offset, durable_offset)\n                    }\n                } else {\n                    let tx_offset = succeed(self.info.clone(), FunctionBudget::ZERO, Duration::ZERO, tx);\n                    effects.committed(tx_offset, durable_offset)\n                };\n\n                Ok(res)\n            }\n        }\n    }\n\n    /// Apply an environment publication using the installed module instance. No\n    /// initialization, migration, program replacement, or scheduler restart occurs.\n    fn update_environment<I: WasmInstance>(\n        &mut self,","sourceCodeStart":789,"sourceCodeEnd":825,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/eddf9f5014579a50d4b67630e28b6e15cad9c4af/crates/core/src/host/wasm_common/module_host_actor.rs#L789-L825","documentation":"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.","triggerScenarios":"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.","commonSituations":"Schema migrations where views are not updated in tandem; untested views over production data; arithmetic overflow or NaN paths in views.","solutions":["Fix the view logic to tolerate both pre-migration and post-migration row shapes.","Test the update against a copy of production data before deploying.","Remove panic paths from views: no unwrap, unchecked indexing, or overflowing arithmetic.","Retry the publish after fixing; the rollback guarantees a consistent old state."],"exampleFix":"// before (view in module, Rust)\nlet score: u64 = ctx.args.score.unwrap(); // traps when the field is absent during migration\n\n// after\nlet score: u64 = ctx.args.score.unwrap_or_default(); // tolerate old rows","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"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.","preventionTips":["Run every publish against a staging clone of production data.","Audit views for panic paths (unwrap, indexing, arithmetic overflow) before schema changes.","Update views and schema in the same publish."],"tags":["update","migration","view","trap","rollback"],"backgroundTag":"migration-failed","analyzedSha":"eddf9f5014579a50d4b67630e28b6e15cad9c4af","analyzedAt":"2026-09-20T12:15:59.611Z","contentChangedAt":"2026-09-20T12:15:59.611Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}