Skip to content

test(server): fix TracingTestGuard drop order and add force_flush - #2604

Open
krishicks wants to merge 1 commit into
mainfrom
hicks/push-mnmulylrwmwv
Open

test(server): fix TracingTestGuard drop order and add force_flush#2604
krishicks wants to merge 1 commit into
mainfrom
hicks/push-mnmulylrwmwv

Conversation

@krishicks

Copy link
Copy Markdown
Collaborator

Summary

Try to fix the flaky tracing tests.

Rust drops struct fields in declaration order. TracingTestGuard previously dropped _default (restoring the thread-local subscriber) before _provider (shutting down the provider). This created a window where the subscriber was gone but the provider was still live: any span closing in that gap could skew the GUARD_STACK thread-local used by tracing-opentelemetry's context_activation, leaving a stale OTel parent context visible to the next test. The result was spans that should be trace roots appearing as children, causing assert_is_root to fail intermittently on CI.

Fix the field order so _provider drops first (while the test subscriber is still installed), then _default restores the subscriber, then _lock releases the serialization mutex.

Also add TracingTestGuard::force_flush() and call it before every span assertion across all tracing tests. SimpleSpanProcessor exports synchronously so this is a no-op today, but it makes the synchronization intent explicit and guards against regressions if the processor type ever changes.

Related Issue

Changes

Testing

  • mise run pre-commit passes
  • Unit tests added/updated
  • E2E tests added/updated (if applicable)

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

Rust drops struct fields in declaration order. TracingTestGuard
previously dropped _default (restoring the thread-local subscriber)
before _provider (shutting down the provider). This created a window
where the subscriber was gone but the provider was still live: any
span closing in that gap could skew the GUARD_STACK thread-local used
by tracing-opentelemetry's context_activation, leaving a stale OTel
parent context visible to the next test. The result was spans that
should be trace roots appearing as children, causing assert_is_root
to fail intermittently on CI.

Fix the field order so _provider drops first (while the test
subscriber is still installed), then _default restores the subscriber,
then _lock releases the serialization mutex.

Also add TracingTestGuard::force_flush() and call it before every span
assertion across all tracing tests. SimpleSpanProcessor exports
synchronously so this is a no-op today, but it makes the
synchronization intent explicit and guards against regressions if the
processor type ever changes.

Signed-off-by: Kris Hicks <khicks@nvidia.com>
@krishicks
krishicks force-pushed the hicks/push-mnmulylrwmwv branch from e0bd740 to 95788b1 Compare August 4, 2026 01:59
/// spans which close during shutdown export to the correct exporter rather
/// than leaking into the thread-local `OTel` context stack. `provider` is
/// therefore declared before `_default` — Rust drops fields in declaration
/// order, so the provider is torn down first.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always something new to learn with rust

impl TracingTestGuard {
/// Flush the provider so all pending spans are exported before reading.
///
/// `SimpleSpanProcessor` exports synchronously, so this is a no-op for

@matthewgrossman matthewgrossman Aug 4, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume rust has a BatchSpanProcessor like other langs that exports otel? using that in "prod" (as opposed to tests) I think is generally the blessed path, not sure if we're only using Simple in tests

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my agent tells me we are indeed using Batch outside the test path 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants