{"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":3416,"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    /// Releases an intent when no broadcast attempt can have occurred.\n    ///\n    /// # Errors\n    ///\n    /// Returns an error if the intent advanced to broadcast or persistence fails.\n    pub async fn mark_execution_intent_recoverable(&self, intent_id: i64) -> anyhow::Result<()> {\n        let mut transaction = self.pool.begin().await.map_err(|e| {\n            anyhow::anyhow!(\"Failed to start recoverable execution transition: {e}\")\n        })?;\n        let current_status = sqlx::query_scalar::<_, String>(\n            \"SELECT status FROM execution_intent WHERE id = $1 FOR UPDATE\",","sourceCodeStart":3398,"sourceCodeEnd":3434,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/2114cf6f761429e0adb5ca9596fcd7b895b16011/crates/adapters/blockchain/src/cache/database.rs#L3398-L3434","documentation":"Thrown when the guarded UPDATE in assign_execution_intent_nonce fails at the database level (the zero-rows case is a separate error). Because it is a single UPDATE executed on the pool, failures are connection loss, statement timeout, or row-lock contention with the FOR UPDATE readers in record_execution_status and add_execution_transaction_hash.","triggerScenarios":"A concurrent status transition holding the intent row lock while the nonce is assigned; connection drop mid-UPDATE; aggressive statement_timeout.","commonSituations":"The signer racing the receipt watcher on the same intent; busy nodes with tight timeouts.","solutions":["Retry the assignment with the same nonce - it is idempotent while status is 'prepared' and nonce is NULL or equal","Assign the nonce before enabling watchers on the intent to avoid row-lock races","Raise statement_timeout if lock waits dominate the wrapped {e}"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt in 0..3 {\n    match db.assign_execution_intent_nonce(intent_id, nonce).await {\n        Ok(()) => break,\n        Err(e) if e.downcast_ref::<sqlx::Error>().is_some_and(|se| matches!(se, sqlx::Error::Io(_) | sqlx::Error::ConnectionClosed(_) | sqlx::Error::Database(_))) => {\n            tokio::time::sleep(Duration::from_millis(50u64 << attempt)).await;\n        }\n        Err(e) => return Err(e),\n    }\n}","preventionTips":["Assign the nonce before starting watchers on the intent to avoid row-lock races","Retry with the identical nonce - the assignment is idempotent while status is 'prepared'","Keep nonce assignment on the same task that owns the intent lifecycle"],"tags":["rust","sqlx","postgresql","update-failed","nonce","row-lock"],"backgroundTag":"database-update-failed","analyzedSha":"2114cf6f761429e0adb5ca9596fcd7b895b16011","analyzedAt":"2026-08-21T11:28:30.864Z","schemaVersion":2},"datasetVersion":"2026-08-22T04:17:13.399Z"}