fix tokio panic: use async lock in init_at/teardown for tokio-mutex modules
blocking_lock() panics when called from within the tokio async runtime. Changed init_at() and teardown() to async fn with .lock().await for all 8 modules using tokio::sync::Mutex (library, reco, api_cache, artist_vectors, musicbrainz, listenbrainz, offline_cache, lyrics). Updated activate/deactivate_user_session to .await these calls.
This commit is contained in:
1 parent
c237ccee50
commit
2dfb014a1e
9 files changed
+48
-48
No files matched your search
@@ -40,20 +40,20 @@ impl ApiCacheState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
let cache_dir = base_dir.join("cache");
|
||||
std::fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| format!("Failed to create cache directory: {}", e))?;
|
||||
let db_path = cache_dir.join("api_cache.db");
|
||||
let new_cache = ApiCache::new(&db_path)?;
|
||||
log::info!("API cache initialized at {:?}", db_path);
|
||||
let mut guard = self.cache.blocking_lock();
|
||||
let mut guard = self.cache.lock().await;
|
||||
*guard = Some(new_cache);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.cache.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.cache.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,20 +46,20 @@ impl ArtistVectorStoreState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
let cache_dir = base_dir.join("cache");
|
||||
std::fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| format!("Failed to create cache directory: {}", e))?;
|
||||
let db_path = cache_dir.join("artist_vectors.db");
|
||||
let new_store = ArtistVectorStore::new(&db_path)?;
|
||||
log::info!("Artist vector store initialized at {:?}", db_path);
|
||||
let mut guard = self.store.blocking_lock();
|
||||
let mut guard = self.store.lock().await;
|
||||
*guard = Some(new_store);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.store.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.store.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,14 +122,14 @@ pub async fn activate_user_session(
|
||||
remote_control_settings.init_at(&data_dir)?;
|
||||
allowed_origins.init_at(&data_dir)?;
|
||||
updates.init_at(&data_dir)?;
|
||||
library.init_at(&data_dir)?;
|
||||
reco.init_at(&data_dir)?;
|
||||
api_cache.init_at(&data_dir)?;
|
||||
artist_vectors.init_at(&data_dir)?;
|
||||
library.init_at(&data_dir).await?;
|
||||
reco.init_at(&data_dir).await?;
|
||||
api_cache.init_at(&data_dir).await?;
|
||||
artist_vectors.init_at(&data_dir).await?;
|
||||
blacklist.init_at(&data_dir)?;
|
||||
offline.init_at(&data_dir)?;
|
||||
musicbrainz.init_at(&data_dir)?;
|
||||
listenbrainz.init_at(&data_dir)?;
|
||||
musicbrainz.init_at(&data_dir).await?;
|
||||
listenbrainz.init_at(&data_dir).await?;
|
||||
|
||||
// Type-alias states (no init_at method — init inline)
|
||||
init_type_alias_state(&*subscription_state, &data_dir, SubscriptionStateStore::new_at)?;
|
||||
@@ -137,8 +137,8 @@ pub async fn activate_user_session(
|
||||
init_type_alias_state(&*legal_settings, &data_dir, LegalSettingsStore::new_at)?;
|
||||
|
||||
// Cache-dir stores:
|
||||
offline_cache.init_at(&cache_dir)?;
|
||||
lyrics.init_at(&cache_dir)?;
|
||||
offline_cache.init_at(&cache_dir).await?;
|
||||
lyrics.init_at(&cache_dir).await?;
|
||||
|
||||
// Run deferred subscription purge check (was removed from startup)
|
||||
let now = std::time::SystemTime::now()
|
||||
@@ -213,16 +213,16 @@ pub async fn deactivate_user_session(
|
||||
remote_control_settings.teardown()?;
|
||||
allowed_origins.teardown()?;
|
||||
updates.teardown();
|
||||
library.teardown();
|
||||
reco.teardown();
|
||||
api_cache.teardown();
|
||||
artist_vectors.teardown();
|
||||
library.teardown().await;
|
||||
reco.teardown().await;
|
||||
api_cache.teardown().await;
|
||||
artist_vectors.teardown().await;
|
||||
blacklist.teardown();
|
||||
offline.teardown();
|
||||
offline_cache.teardown();
|
||||
lyrics.teardown();
|
||||
musicbrainz.teardown();
|
||||
listenbrainz.teardown();
|
||||
offline_cache.teardown().await;
|
||||
lyrics.teardown().await;
|
||||
musicbrainz.teardown().await;
|
||||
listenbrainz.teardown().await;
|
||||
|
||||
// Type-alias states
|
||||
teardown_type_alias_state(&*subscription_state);
|
||||
|
||||
@@ -25,18 +25,18 @@ pub struct LibraryState {
|
||||
}
|
||||
|
||||
impl LibraryState {
|
||||
pub fn init_at(&self, base_dir: &std::path::Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, base_dir: &std::path::Path) -> Result<(), String> {
|
||||
std::fs::create_dir_all(base_dir)
|
||||
.map_err(|e| format!("Failed to create directory: {}", e))?;
|
||||
let db_path = base_dir.join("library.db");
|
||||
let db = LibraryDatabase::open(&db_path).map_err(|e| e.to_string())?;
|
||||
let mut guard = self.db.blocking_lock();
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = Some(db);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.db.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ impl ListenBrainzSharedState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
let cache_dir = base_dir.join("cache");
|
||||
std::fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| format!("Failed to create cache directory: {}", e))?;
|
||||
@@ -108,7 +108,7 @@ impl ListenBrainzSharedState {
|
||||
(token, user_name)
|
||||
};
|
||||
|
||||
let mut guard = self.cache.blocking_lock();
|
||||
let mut guard = self.cache.lock().await;
|
||||
*guard = Some(new_cache);
|
||||
|
||||
if token.is_some() && user_name.is_some() {
|
||||
@@ -118,8 +118,8 @@ impl ListenBrainzSharedState {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.cache.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.cache.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
@@ -79,19 +79,19 @@ impl LyricsState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
let cache_dir = base_dir.join("lyrics");
|
||||
std::fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| format!("Failed to create lyrics cache directory: {}", e))?;
|
||||
let db_path = cache_dir.join("lyrics.db");
|
||||
let new_db = LyricsCacheDb::new(&db_path)?;
|
||||
let mut guard = self.db.blocking_lock();
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = Some(new_db);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.db.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,20 +54,20 @@ impl MusicBrainzSharedState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
let cache_dir = base_dir.join("cache");
|
||||
std::fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| format!("Failed to create cache directory: {}", e))?;
|
||||
let db_path = cache_dir.join("musicbrainz_cache.db");
|
||||
let new_cache = MusicBrainzCache::new(&db_path)?;
|
||||
log::info!("MusicBrainz cache initialized at {:?}", db_path);
|
||||
let mut guard = self.cache.blocking_lock();
|
||||
let mut guard = self.cache.lock().await;
|
||||
*guard = Some(new_cache);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.cache.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.cache.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
@@ -186,7 +186,7 @@ impl OfflineCacheState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_at(&self, cache_base_dir: &std::path::Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, cache_base_dir: &std::path::Path) -> Result<(), String> {
|
||||
let cache_dir = cache_base_dir.join("audio");
|
||||
std::fs::create_dir_all(&cache_dir)
|
||||
.map_err(|e| format!("Failed to create cache directory: {}", e))?;
|
||||
@@ -196,14 +196,14 @@ impl OfflineCacheState {
|
||||
.map_err(|e| format!("Failed to create artwork directory: {}", e))?;
|
||||
let db_path = cache_dir.join("index.db");
|
||||
let new_db = OfflineCacheDb::new(&db_path)?;
|
||||
let mut guard = self.db.blocking_lock();
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = Some(new_db);
|
||||
log::info!("Offline cache initialized at: {:?}", cache_dir);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.db.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
|
||||
|
||||
@@ -106,19 +106,19 @@ impl RecoState {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
pub async fn init_at(&self, base_dir: &Path) -> Result<(), String> {
|
||||
let reco_dir = base_dir.join("reco");
|
||||
std::fs::create_dir_all(&reco_dir)
|
||||
.map_err(|e| format!("Failed to create reco directory: {}", e))?;
|
||||
let db_path = reco_dir.join("events.db");
|
||||
let new_db = RecoStoreDb::new(&db_path)?;
|
||||
let mut guard = self.db.blocking_lock();
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = Some(new_db);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn teardown(&self) {
|
||||
let mut guard = self.db.blocking_lock();
|
||||
pub async fn teardown(&self) {
|
||||
let mut guard = self.db.lock().await;
|
||||
*guard = None;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user