From 978b2b338b34f479b7e3f2e3641f2dce769e6f9a Mon Sep 17 00:00:00 2001 From: Accusys Date: Sat, 22 Aug 2026 16:17:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20P5=20race=20condition=20=E2=80=94=20poll?= =?UTF-8?q?=20for=20trace=5Fprofiles=20before=20VLM=20generation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Add wait_for_traces() helper: polls trace_profiles table every 2s up to 60s, returning when rows appear (TKG may still be writing) - Replace one-shot face_count query in both P5 triggers (has_face + no-face paths) with polling - Verified: 22/22 traces summarized for 9df18017, semantic search returns 10 VLM results with sim 0.601-0.696 --- src/worker/job_worker.rs | 60 +++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/src/worker/job_worker.rs b/src/worker/job_worker.rs index 104178a8..7c52d900 100644 --- a/src/worker/job_worker.rs +++ b/src/worker/job_worker.rs @@ -2966,20 +2966,9 @@ impl JobWorker { let redis_p5 = redis_clone.clone(); let uuid_p5 = uuid_clone.clone(); let output_dir_p5 = output_dir_clone.clone(); + let pool_p5 = db_clone.pool().clone(); tokio::spawn(async move { - // Check if this file has face traces - let tp_table = - crate::core::db::schema::table_name("trace_profiles"); - let face_count: i64 = sqlx::query_scalar(&format!( - "SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1" - )) - .bind(&uuid_p5) - .fetch_optional(db_p5.pool()) - .await - .ok() - .flatten() - .unwrap_or(0); - + let face_count = Self::wait_for_traces(&pool_p5, &uuid_p5, 60).await; if face_count > 0 { vlm_summary::generate_vlm_summaries( &db_p5, @@ -2989,7 +2978,7 @@ impl JobWorker { ) .await; } else { - info!("[VLM] Skipping for {} (no face traces)", uuid_p5); + info!("[VLM] Skipping for {} (no face traces after 60s wait)", uuid_p5); } }); }); @@ -3080,18 +3069,9 @@ impl JobWorker { let redis_p5 = redis_clone.clone(); let uuid_p5 = uuid_clone.clone(); let output_dir_p5 = output_dir_clone.clone(); + let pool_p5 = db_clone.pool().clone(); tokio::spawn(async move { - let tp_table = crate::core::db::schema::table_name("trace_profiles"); - let face_count: i64 = sqlx::query_scalar(&format!( - "SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1" - )) - .bind(&uuid_p5) - .fetch_optional(db_p5.pool()) - .await - .ok() - .flatten() - .unwrap_or(0); - + let face_count = Self::wait_for_traces(&pool_p5, &uuid_p5, 60).await; if face_count > 0 { vlm_summary::generate_vlm_summaries( &db_p5, @@ -3101,7 +3081,7 @@ impl JobWorker { ) .await; } else { - info!("[VLM] Skipping for {} (no face traces)", uuid_p5); + info!("[VLM] Skipping for {} (no face traces after 60s wait)", uuid_p5); } }); }); @@ -3409,6 +3389,34 @@ impl JobWorker { Ok(()) } + /// Poll trace_profiles table until rows appear (TKG may still be writing). + /// Returns count if found within timeout, 0 otherwise. + async fn wait_for_traces(pool: &PgPool, uuid: &str, max_secs: u64) -> i64 { + let tp_table = crate::core::db::schema::table_name("trace_profiles"); + let interval = std::time::Duration::from_secs(2); + let max_attempts = (max_secs / 2).max(1); + + for attempt in 0..max_attempts { + let count: i64 = sqlx::query_scalar(&format!( + "SELECT COUNT(*) FROM {tp_table} WHERE file_uuid = $1" + )) + .bind(uuid) + .fetch_optional(pool) + .await + .ok() + .flatten() + .unwrap_or(0); + if count > 0 { + return count; + } + if attempt == 0 { + info!("[P5-wait] Waiting for trace_profiles to appear for {}", uuid); + } + tokio::time::sleep(interval).await; + } + 0 + } + async fn update_face_track_labels( db: &crate::core::db::PostgresDb, uuid: &str,