From cdf24b01437064777be012c5f62c0c5f111c9f28 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 11 Nov 2025 13:19:55 +0000 Subject: [PATCH] Fix race condition in is_valid_pk() for files being ingested When add_from_reader() returns after prebuffering, the file may not exist on disk yet due to tokio::spawn() scheduling. This caused "Cache entry not found" errors when playlist tried to validate the pk. Solution: - If DB entry exists but file doesn't, wait up to 1 second for file creation - This handles the race condition between prebuffer completion and File::create() in the background task - Deterministic and robust: either file exists or we timeout with error The fix preserves the progressive caching design while ensuring validation is deterministic. Test: Verified no "Cache entry not found" errors with clean cache. --- pmocache/src/cache_trait.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pmocache/src/cache_trait.rs b/pmocache/src/cache_trait.rs index 1036130c..179efe6c 100644 --- a/pmocache/src/cache_trait.rs +++ b/pmocache/src/cache_trait.rs @@ -152,8 +152,22 @@ pub trait FileCache: Send + Sync { let file_path = self.file_path(pk); if !file_path.exists() { - tracing::debug!("is_valid_pk({}): File does not exist", pk); - return false; + // Si l'entrée DB existe mais pas le fichier, c'est probablement en cours d'ingestion + // Attendre jusqu'à 1 seconde que le fichier soit créé (le tokio::spawn peut mettre un peu de temps) + tracing::debug!("is_valid_pk({}): File does not exist yet, waiting for file creation (ingestion in progress)", pk); + + let mut attempts = 0; + while !file_path.exists() && attempts < 100 { + std::thread::sleep(std::time::Duration::from_millis(10)); + attempts += 1; + } + + if !file_path.exists() { + tracing::warn!("is_valid_pk({}): File not created after 1s despite DB entry existing", pk); + return false; + } + + tracing::debug!("is_valid_pk({}): File created after {}ms", pk, attempts * 10); } // Vérifier d'abord si le marker de completion existe