From d26b7547015e5177eabfb115ff08bc82df1085cd Mon Sep 17 00:00:00 2001 From: damocles Date: Wed, 9 Sep 2026 23:47:21 +0200 Subject: [PATCH] subagent: fix a real test race on the process-wide OTEL_RESOURCE_ATTRIBUTES env var --- hive-subagent-mcp/src/session.rs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/hive-subagent-mcp/src/session.rs b/hive-subagent-mcp/src/session.rs index c9bad27a..8b2c5e90 100644 --- a/hive-subagent-mcp/src/session.rs +++ b/hive-subagent-mcp/src/session.rs @@ -443,10 +443,21 @@ mod tests { ); } + // One test, not two: `subagent_otel_attrs` reads the real process-wide + // `OTEL_RESOURCE_ATTRIBUTES` env var, and cargo runs tests in parallel + // threads by default — two separate tests each mutating that global + // raced each other (and, in this container, lost to the agent's own + // real ambient value). Sequencing both assertions in one test removes + // the race instead of papering over it with a mutex. #[test] - fn otel_attrs_appends_when_ambient_var_is_set() { - // SAFETY: test-only env mutation, single-threaded within this fn's - // scope (no other test in this crate touches this var — checked). + fn otel_attrs_append_ambient_value_or_stand_alone() { + // SAFETY: test-only env mutation; sequenced within this one test so + // no other test's concurrent read/write of the same var can race it. + unsafe { + std::env::remove_var("OTEL_RESOURCE_ATTRIBUTES"); + } + assert_eq!(subagent_otel_attrs("batch-1"), "subagent=batch-1"); + unsafe { std::env::set_var("OTEL_RESOURCE_ATTRIBUTES", "agent=damocles"); } @@ -459,14 +470,6 @@ mod tests { } } - #[test] - fn otel_attrs_stands_alone_when_ambient_var_is_unset() { - unsafe { - std::env::remove_var("OTEL_RESOURCE_ATTRIBUTES"); - } - assert_eq!(subagent_otel_attrs("batch-1"), "subagent=batch-1"); - } - #[test] fn build_config_only_appends_system_prompt_when_given() { let with = build_config("n", None, Some("/tmp/p.md"));