{"record":{"id":"785121ea46625425","repo":"nautechsystems/nautilus_trader","slug":"sync-cancelled","errorCode":null,"errorMessage":"Sync cancelled","messagePattern":"Sync cancelled","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"info","filePath":"crates/adapters/blockchain/src/data/core.rs","lineNumber":427,"sourceCode":"            .request_blocks_stream(from_block, Some(to_block))\n            .await;\n\n        tokio::pin!(blocks_stream);\n\n        let mut metrics = BlockchainSyncReporter::new(\n            BlockchainSyncReportItems::Blocks,\n            from_block,\n            total_blocks,\n            BLOCKS_PROCESS_IN_SYNC_REPORT,\n        );\n\n        let mut batch: Vec<Block> = Vec::with_capacity(BATCH_SIZE);\n\n        let cancellation_token = self.cancellation_token.clone();\n        let sync_result = tokio::select! {\n            () = cancellation_token.cancelled() => {\n                log::debug!(\"Block sync cancelled\");\n                Err(anyhow::anyhow!(\"Sync cancelled\"))\n            }\n            result = async {\n                while let Some(block) = blocks_stream.next().await {\n                    let block_number = block.number;\n                    if self.cache.get_block_timestamp(block_number).is_some() {\n                        continue;\n                    }\n                    batch.push(block);\n\n                    // Process batch when full or last block\n                    if batch.len() >= BATCH_SIZE || block_number >= to_block {\n                        let batch_size = batch.len();\n\n                        self.cache.add_blocks_batch(batch, use_copy_command).await?;\n                        metrics.update(batch_size);\n\n                        // Re-initialize batch vector\n                        batch = Vec::with_capacity(BATCH_SIZE);","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/data/core.rs#L409-L445","documentation":"sync_blocks runs the block fetch loop inside tokio::select! against the client's cancellation token. When cancellation fires (disconnect, reconfiguration, or shutdown), the sync aborts deliberately with Err(\"Sync cancelled\") — it signals an interrupted backfill of historical blocks into the local timestamp cache, not an unexpected failure.","triggerScenarios":"Any cancellation of the token while a sync_blocks stream is active: calling disconnect(), starting a new task generation, or dropping the client mid-sync.","commonSituations":"User-initiated disconnect during a long historical block backfill; reconnect cycles cancelling an in-progress sync; service shutdown while bootstrapping block timestamps.","solutions":["Treat this error as expected cancellation: filter/match for it and log rather than alert.","Keep the client connected until sync_blocks completes if the cache must be fully populated.","Re-run sync_blocks_checked after reconnect — cached block timestamps are skipped via cache lookups, so it resumes cheaply.","If cancellations are unwanted, audit what is triggering the cancellation token (disconnect paths or task restarts)."],"exampleFix":"// before\nclient.sync_blocks_checked(chain, from, to).await?;\n\n// after\nif let Err(e) = client.sync_blocks_checked(chain, from, to).await {\n    if e.to_string() == \"Sync cancelled\" {\n        log::info!(\"block sync cancelled by shutdown\");\n    } else {\n        return Err(e);\n    }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match client.sync_blocks_checked(chain, from, to).await {\n    Ok(()) => {},\n    Err(e) if e.to_string() == \"Sync cancelled\" => log::info!(\"sync cancelled, will resume\"),\n    Err(e) => return Err(e),\n}","preventionTips":["Keep the client connected until the backfill finishes","Remember sync is resumable: cached block timestamps are skipped on re-run","Audit what triggers the cancellation token during long syncs"],"tags":["cancellation","tokio","block-sync","shutdown"],"backgroundTag":"operation-cancelled","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}