fix: hard task-level timeout as failsafe so in-flight lock always clears

This commit is contained in:
M1 2026-03-18 13:05:43 +04:00
parent 0edce8c555
commit 289ec8e038
1 changed files with 17 additions and 1 deletions

View File

@ -44,7 +44,23 @@ pub async fn fetch_and_run(
let token = token.to_string(); let token = token.to_string();
let in_flight = in_flight.clone(); let in_flight = in_flight.clone();
tokio::spawn(async move { tokio::spawn(async move {
let result = run_check(&client, &monitor, monitor.scheduled_at.clone()).await; let timeout_ms = monitor.timeout_ms.unwrap_or(30000);
// Hard deadline: timeout + 5s buffer, so hung checks always resolve
let deadline = std::time::Duration::from_millis(timeout_ms + 5000);
let result = match tokio::time::timeout(deadline, run_check(&client, &monitor, monitor.scheduled_at.clone())).await {
Ok(r) => r,
Err(_) => PingResult {
monitor_id: monitor.id.clone(),
scheduled_at: monitor.scheduled_at.clone(),
jitter_ms: None,
status_code: None,
latency_ms: Some(timeout_ms as u64),
up: false,
error: Some(format!("timed out after {}ms", timeout_ms)),
cert_expiry_days: None,
meta: None,
},
};
// Remove from in-flight before posting so a fast next cycle can pick it up // Remove from in-flight before posting so a fast next cycle can pick it up
in_flight.lock().await.remove(&monitor.id); in_flight.lock().await.remove(&monitor.id);
if let Err(e) = post_result(&client, &coordinator_url, &token, result).await { if let Err(e) = post_result(&client, &coordinator_url, &token, result).await {