Skip to content

Commit d8d2a55

Browse files
Lewis-Eclaude
andcommitted
fix: bias mini_agent serve() select toward shutdown over task exits
When shutdown_tx fires, each transport accept loop sees shutdown_rx, drains its joinset, and returns Ok(()). Both the accept loop's JoinHandle and the supervisor's shutdown_rx.changed() then become ready in the same poll cycle. Without biased, the unbiased select! in serve() would pick a *Died arm at random, return Err(...), and abort everything without firing flusher_shutdown_tx — so the stats flusher's shutdown branch never ran and no final stats POST happened. This made test_mini_agent_*_with_real_flushers intermittently time out at 60s waiting for /api/v0.2/stats; CI run 25562651282 captured the smoking gun "Event::PipeDied: Ok(Ok(()))". biased; with shutdown_rx.changed() listed first guarantees Event::Shutdown wins whenever the shutdown signal is observable, so the supervisor always takes the graceful-flush path even if the accept loops happen to resolve first. Production main.rs never sends on shutdown_tx, so this is purely a test-only path. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 634347c commit d8d2a55

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

crates/datadog-trace-agent/src/mini_agent.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,13 +295,19 @@ impl MiniAgent {
295295
};
296296
tokio::pin!(concentrator_exit);
297297

298+
// biased: when shutdown_tx fires, the accept loops also see
299+
// shutdown_rx and exit cleanly. Their JoinHandles resolve in the
300+
// same poll cycle as our shutdown_rx.changed(); without biased,
301+
// a *Died arm can win the race and abort the supervisor before
302+
// it has a chance to signal the stats flusher to flush.
298303
tokio::select! {
304+
biased;
305+
_ = shutdown_rx.changed() => Event::Shutdown,
299306
r = &mut tcp_exit => Event::TcpDied(format!("{r:?}")),
300307
r = &mut pipe_exit => Event::PipeDied(format!("{r:?}")),
301308
r = &mut trace_flusher_handle => Event::TraceFlusherDied(format!("{r:?}")),
302309
r = &mut stats_flusher_handle => Event::StatsFlusherDied(format!("{r:?}")),
303310
r = &mut concentrator_exit => Event::ConcentratorDied(format!("{r:?}")),
304-
_ = shutdown_rx.changed() => Event::Shutdown,
305311
}
306312
};
307313
#[cfg(not(all(windows, feature = "windows-pipes")))]
@@ -322,12 +328,14 @@ impl MiniAgent {
322328
};
323329
tokio::pin!(concentrator_exit);
324330

331+
// biased: see Windows branch above for rationale.
325332
tokio::select! {
333+
biased;
334+
_ = shutdown_rx.changed() => Event::Shutdown,
326335
r = &mut tcp_exit => Event::TcpDied(format!("{r:?}")),
327336
r = &mut trace_flusher_handle => Event::TraceFlusherDied(format!("{r:?}")),
328337
r = &mut stats_flusher_handle => Event::StatsFlusherDied(format!("{r:?}")),
329338
r = &mut concentrator_exit => Event::ConcentratorDied(format!("{r:?}")),
330-
_ = shutdown_rx.changed() => Event::Shutdown,
331339
}
332340
};
333341

0 commit comments

Comments
 (0)