From 86c6ec26f57dcba5b02f1dbeeaabaf301a467a0e Mon Sep 17 00:00:00 2001 From: mirsal Date: Thu, 15 May 2025 19:30:05 +0000 Subject: [PATCH] state: Send a warning trace whenever the state db lock is poisoned --- src/state.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/state.rs b/src/state.rs index 06c02dd..79df30a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -19,13 +19,13 @@ use std::{ error::Error, - sync::{Arc, Mutex} + sync::{Arc, Mutex, MutexGuard} }; use tokio::fs; use chrono::{ DateTime, Utc }; -use tracing::{ info, debug }; +use tracing::{ warn, info, debug }; use serde_yaml::mapping::Mapping as FeedReaderState; @@ -48,6 +48,13 @@ impl FeedReaderStateDb { Ok(db.clone()) } + fn lock_state(&self) -> MutexGuard { + self.state.lock().unwrap_or_else(|e| { + warn!("State db mutex has been poisoned, continuing..."); + e.into_inner() + }) + } + pub async fn set( &self, uri: &str, @@ -56,7 +63,7 @@ impl FeedReaderStateDb { { debug!("Updating feed reader state"); - self.state.lock().unwrap().insert(uri.into(), dt.timestamp().into()); + self.lock_state().insert(uri.into(), dt.timestamp().into()); } self.persist().await.unwrap(); @@ -65,7 +72,7 @@ impl FeedReaderStateDb { #[tracing::instrument(ret, level="debug")] pub fn get(&self, uri: &str) -> Option> { debug!("Retrieving state for feed {}", uri); - match self.state.lock().unwrap().get(uri) { + match self.lock_state().get(uri) { Some(t) => DateTime::from_timestamp((*t).as_i64().unwrap(), 0), None => None }