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.
This commit is contained in:
Claude
2025-11-11 13:19:55 +00:00
parent befdd90149
commit cdf24b0143

View File

@@ -152,8 +152,22 @@ pub trait FileCache<C: CacheConfig>: Send + Sync {
let file_path = self.file_path(pk); let file_path = self.file_path(pk);
if !file_path.exists() { if !file_path.exists() {
tracing::debug!("is_valid_pk({}): File does not exist", pk); // Si l'entrée DB existe mais pas le fichier, c'est probablement en cours d'ingestion
return false; // 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 // Vérifier d'abord si le marker de completion existe