Skip to content

Commit 476e131

Browse files
committed
fix(cardano-chain-follower): update forever task thread stat
Signed-off-by: bkioshn <[email protected]>
1 parent 8edb23f commit 476e131

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

rust/cardano-chain-follower/src/chain_sync.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,6 @@ async fn live_sync_backfill_and_purge(
533533
///
534534
/// This does not return, it is a background task.
535535
pub(crate) async fn chain_sync(cfg: ChainSyncConfig, rx: mpsc::Receiver<MithrilUpdateMessage>) {
536-
/// Thread name for stats.
537-
const THREAD_NAME: &str = "ChainSync";
538-
539-
stats::start_thread(cfg.chain, THREAD_NAME, true);
540536
debug!(
541537
"Chain Sync for: {} from {} : Starting",
542538
cfg.chain, cfg.relay_address,

rust/cardano-chain-follower/src/chain_sync_config.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! each network. Chain Followers use the data supplied by the Chain-Sync.
55
//! This module configures the chain sync processes.
66
7-
use std::sync::LazyLock;
7+
use std::{panic, sync::LazyLock};
88

99
use cardano_blockchain_types::Network;
1010
use dashmap::DashMap;
@@ -125,6 +125,7 @@ impl ChainSyncConfig {
125125
///
126126
/// `Error`: On error.
127127
pub async fn run(self) -> Result<()> {
128+
const THREAD_NAME: &str = "ChainSync";
128129
debug!(
129130
chain = self.chain.to_string(),
130131
"Chain Synchronization Starting"
@@ -150,8 +151,23 @@ impl ChainSyncConfig {
150151
// Start the Mithril Snapshot Follower
151152
let rx = self.mithril_cfg.run().await?;
152153

153-
// Start Chain Sync
154-
*locked_handle = Some(tokio::spawn(chain_sync(self.clone(), rx)));
154+
// Wrap inside a panic catcher to detect if the task panics.
155+
let result = panic::catch_unwind(|| {
156+
stats::start_thread(self.chain, THREAD_NAME, true);
157+
// Start Chain Sync
158+
tokio::spawn(chain_sync(self.clone(), rx))
159+
});
160+
161+
if let Ok(handle) = result {
162+
*locked_handle = Some(handle);
163+
} else {
164+
// Chain sync panic, stop the thread and log.
165+
error!(
166+
chain = self.chain.to_string(),
167+
"Chain Sync for {} : PANICKED", self.chain
168+
);
169+
stats::stop_thread(self.chain, THREAD_NAME);
170+
}
155171

156172
// sync_map.insert(chain, handle);
157173
debug!("Chain Sync for {} : Started", self.chain);

rust/cardano-chain-follower/src/mithril_snapshot_config.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
//! Configuration for the Mithril Snapshot used by the follower.
22
33
use std::{
4-
path::{Path, PathBuf},
5-
str::FromStr,
6-
sync::LazyLock,
4+
panic, path::{Path, PathBuf}, str::FromStr, sync::LazyLock
75
};
86

97
use anyhow::bail;
@@ -20,11 +18,7 @@ use tokio::{
2018
use tracing::{debug, error};
2119

2220
use crate::{
23-
error::{Error, Result},
24-
mithril_snapshot_data::{latest_mithril_snapshot_id, SnapshotData},
25-
mithril_snapshot_sync::background_mithril_update,
26-
snapshot_id::SnapshotId,
27-
turbo_downloader::DlConfig,
21+
error::{Error, Result}, mithril_snapshot_data::{latest_mithril_snapshot_id, SnapshotData}, mithril_snapshot_sync::background_mithril_update, snapshot_id::SnapshotId, stats, turbo_downloader::DlConfig
2822
};
2923

3024
/// Type we use to manage the Sync Task handle map.
@@ -384,6 +378,9 @@ impl MithrilSnapshotConfig {
384378

385379
/// Run a Mithril Follower for the given network and configuration.
386380
pub(crate) async fn run(&self) -> Result<mpsc::Receiver<MithrilUpdateMessage>> {
381+
/// Thread name for stats.
382+
const THREAD_NAME: &str = "MithrilSnapshotUpdater";
383+
387384
debug!(
388385
chain = self.chain.to_string(),
389386
"Mithril Auto-update : Starting"
@@ -413,7 +410,23 @@ impl MithrilSnapshotConfig {
413410
let (tx, rx) = mpsc::channel::<MithrilUpdateMessage>(2);
414411

415412
// let handle = tokio::spawn(background_mithril_update(chain, self.clone(), tx));
416-
*locked_handle = Some(tokio::spawn(background_mithril_update(self.clone(), tx)));
413+
414+
// Wrap inside a panic catcher to detect if the task panics.
415+
let result = panic::catch_unwind(|| {
416+
stats::start_thread(self.chain, THREAD_NAME, true);
417+
tokio::spawn(background_mithril_update(self.clone(), tx))
418+
});
419+
420+
if let Ok(handle) = result {
421+
*locked_handle = Some(handle);
422+
} else {
423+
// Mithril update panic, stop the thread and log.
424+
error!(
425+
chain = self.chain.to_string(),
426+
"Background Mithril Update for {} : PANICKED", self.chain
427+
);
428+
stats::stop_thread(self.chain, THREAD_NAME);
429+
}
417430

418431
// sync_map.insert(chain, handle);
419432
debug!(

rust/cardano-chain-follower/src/mithril_snapshot_sync.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,10 +694,6 @@ macro_rules! next_iteration {
694694
pub(crate) async fn background_mithril_update(
695695
cfg: MithrilSnapshotConfig, tx: Sender<MithrilUpdateMessage>,
696696
) {
697-
/// Thread name for stats.
698-
const THREAD_NAME: &str = "MithrilSnapshotUpdater";
699-
700-
stats::start_thread(cfg.chain, THREAD_NAME, true);
701697
debug!(
702698
"Mithril Snapshot background updater for: {} from {} to {} : Starting",
703699
cfg.chain,

0 commit comments

Comments
 (0)