{"record":{"id":"b5ba6cdf092f0ef8","repo":"nautechsystems/nautilus_trader","slug":"failed-to-assign-nonce-nonce-to-execution-intent","errorCode":null,"errorMessage":"Failed to assign nonce {nonce} to execution intent: {e}","messagePattern":"Failed to assign nonce (.+?) to execution intent: (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/cache/database.rs","lineNumber":5874,"sourceCode":"        intent_id: i64,\n        nonce: u64,\n    ) -> anyhow::Result<()> {\n        let nonce_db = i64::try_from(nonce)\n            .with_context(|| format!(\"Execution nonce {nonce} exceeds PostgreSQL BIGINT\"))?;\n        let result = sqlx::query(\n            \"\n            UPDATE execution_intent\n            SET nonce = $2, updated_at = NOW()\n            WHERE id = $1\n              AND status = 'prepared'\n              AND (nonce IS NULL OR nonce = $2)\n            \",\n        )\n        .bind(intent_id)\n        .bind(nonce_db)\n        .execute(&self.pool)\n        .await\n        .map_err(|e| anyhow::anyhow!(\"Failed to assign nonce {nonce} to execution intent: {e}\"))?;\n        anyhow::ensure!(\n            result.rows_affected() == 1,\n            \"Execution intent {intent_id} is not prepared for nonce {nonce}\"\n        );\n        Ok(())\n    }\n\n    /// Assigns the canonical nonce and its authorizing verification evidence atomically.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the durable nonce ledger, intent ownership, manifest identity, or\n    /// evidence is inconsistent, or if persistence fails.\n    pub(crate) async fn assign_execution_intent_nonce_verified(\n        &self,\n        assignment: &ExecutionNonceAssignment<'_>,\n    ) -> anyhow::Result<()> {\n        anyhow::ensure!(","sourceCodeStart":5856,"sourceCodeEnd":5892,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/cache/database.rs#L5856-L5892","documentation":"This error is returned by the nonce-assignment database method when the SQL UPDATE that binds a nonce to a prepared execution intent fails at the query level. The anyhow error wraps the underlying sqlx error, so the original cause (connection failure, constraint violation, query error) is embedded in the message. It indicates persistence of the nonce-to-intent mapping could not be completed.","triggerScenarios":"Calling assign_execution_intent_nonce (database.rs ~5874) when the UPDATE ... SET nonce = $ WHERE intent_id = $ query returns an Err: PostgreSQL connection failure, SQL syntax/type error, transaction aborted, or a database constraint violation during the bind/execute.","commonSituations":"Database connectivity loss between intent preparation and nonce assignment; the pool being closed or saturated; a schema migration changing the execution intents table columns/types; i32 overflow converting nonce_db; concurrent transactions aborting the update.","solutions":["Inspect the wrapped sqlx error in the message to identify the root cause (connection vs constraint vs type).","Verify the database is reachable and the pool is healthy (check connection limits, TLS, credentials).","Confirm the execution_intents table schema matches what the query expects (column names and types, especially the nonce column).","Retry the assignment after recovering connectivity; the operation is idempotent as long as the intent is still unassigned."],"exampleFix":"// before: no diagnostics around the call\ndatabase.assign_execution_intent_nonce(&intent_id, nonce).await?;\n// after: surface the wrapped cause and retry transient failures\nmatch database.assign_execution_intent_nonce(&intent_id, nonce).await {\n    Err(e) if is_transient(&e) => { backoff_retry(|| database.assign_execution_intent_nonce(&intent_id, nonce)).await?; }\n    Err(e) => { tracing::error!(%nonce, error = %e, \"nonce assignment failed\"); return Err(e); }\n    Ok(()) => {}\n}","handlingStrategy":"try-catch","validationCode":"// before calling, confirm DB connectivity and intent existence\nlet reachable = sqlx::query(\"SELECT 1\").execute(&pool).await.is_ok();\nlet prepared = sqlx::query(\"SELECT 1 FROM execution_intents WHERE intent_id = $1 AND nonce IS NULL\")\n    .bind(intent_id).fetch_optional(&pool).await?.is_some();","typeGuard":null,"tryCatchPattern":"match database.assign_execution_intent_nonce(&intent_id, nonce).await {\n    Ok(()) => {},\n    Err(e) if e.to_string().contains(\"Failed to assign nonce\") => {\n        tracing::error!(%nonce, error = %e, \"nonce assignment write failed\");\n        if is_transient(&e) { retry_with_backoff(...).await?; } else { return Err(e); }\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Monitor database connectivity and pool health before the assignment flow starts.","Keep the intents table schema and migrations in sync with the query expectations.","Use connection retries/timeouts on the sqlx pool.","Run assignment inside the same transaction/session as intent preparation where possible."],"tags":["database","postgres","sqlx","persistence"],"backgroundTag":"database-write-failed","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}