From 6319e37af8a9a96916592117a3d9ffcaab9cb00c Mon Sep 17 00:00:00 2001 From: iroiro147 Date: Sat, 1 Aug 2026 17:16:14 +0530 Subject: [PATCH] test(relay): deflake trace_context_lookup test via rebuild_interest_cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit trace_context_lookup_does_not_enable_callsites flakes ~1/6 in the full buzz-relay lib suite (clean main), because tracing's per-callsite `interest` cache is global and outlives the thread-local `with_default` dispatcher — a stale cache can bypass our `LevelFilter::OFF` layer and make the `enabled!` assertion race. Call `tracing::callsite::rebuild_interest_cache()` once inside the `with_default` block so the per-test filter is authoritative, and once afterward to restore default interest for downstream tests. This is the tracing-idiomatic escape hatch and avoids both ENV_LOCK (which cannot catch every global-state race) and any per-run naming hack. Verified: 6/6 green in the full lib suite; was 1/6 failing previously. Fixes #3929 Signed-off-by: Sarthak Singh --- crates/buzz-relay/src/telemetry.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/buzz-relay/src/telemetry.rs b/crates/buzz-relay/src/telemetry.rs index 91bd92f0f3..089bce2c0a 100644 --- a/crates/buzz-relay/src/telemetry.rs +++ b/crates/buzz-relay/src/telemetry.rs @@ -473,6 +473,15 @@ mod tests { #[test] fn trace_context_lookup_does_not_enable_callsites() { + // tracing's callsite `interest` cache is global and per-process — + // `with_default` installs a *thread-local* dispatcher, but a static + // callsite's cached interest can be poisoned by another test going + // through the same callsite with a different subscriber active, + // letting our `LevelFilter::OFF` be bypassed (reproduced 1/6 on clean + // `main` under `cargo test -p buzz-relay --lib` before this fix). + // Forcing the cache to be rebuilt while our subscriber is installed + // makes the per-test filter authoritative again. + // See https://github.com/block/buzz/issues/3929. let context_lookup = TraceContextLookup::default(); let subscriber = tracing_subscriber::registry().with( context_lookup @@ -481,6 +490,7 @@ mod tests { ); tracing::subscriber::with_default(subscriber, || { + tracing::callsite::rebuild_interest_cache(); assert!(context_lookup .dispatch .get() @@ -491,8 +501,12 @@ mod tests { tracing::Level::ERROR )); }); + + // Restore interest cache so subsequent tests observe default values. + tracing::callsite::rebuild_interest_cache(); } + #[test] fn test_service_resource_default_when_env_unset() { let _guard = ENV_LOCK.lock().unwrap();