Skip to content

fix: clear OpenCensus execution context in TestPatch setUp/tearDown#5262

Open
sridhar-3009 wants to merge 2 commits into
open-telemetry:mainfrom
sridhar-3009:fix/flaky-opencensus-shim-test
Open

fix: clear OpenCensus execution context in TestPatch setUp/tearDown#5262
sridhar-3009 wants to merge 2 commits into
open-telemetry:mainfrom
sridhar-3009:fix/flaky-opencensus-shim-test

Conversation

@sridhar-3009
Copy link
Copy Markdown

Problem

test_install_shim_affects_existing_tracers fails non-deterministically, most often on Windows, with:

AssertionError: <opencensus.trace.tracers.context_tracer.ContextTracer object> is not an instance of NoopTracer

Root cause

TestPatch.setUp calls uninstall_shim() to remove the monkeypatch, but does not clear the OpenCensus thread-local execution context. When test_shim_with_sdk.py runs before test_patch.py (e.g. due to alphabetical ordering on Windows), it leaves a live ContextTracer in the OC thread-local store via execution_context. A subsequent bare Tracer() in test_install_shim_affects_existing_tracers reads that stale context and produces a ContextTracer instead of the expected NoopTracer.

Fix

Call execution_context.clean() in both setUp (after uninstall_shim) and tearDown so each test starts and ends with a fully reset OC thread-local state.

Testing

All 6 tests in TestPatch pass locally.

Fixes #3975

…th single MetricsData field

The callback → _receive_metrics → add_metrics_data → deque → yield chain
always held at most one MetricsData per collect cycle (the callback
triggers exactly one _receive_metrics call before collect drains the
queue). Replace the deque[MetricsData] with an Optional[MetricsData]
field and simplify collect() to consume it directly, removing the
while-loop, popleft(), len() check, and the now-unused `deque` import.

This fixes a latent bug where multiple items in the deque would have
caused collect() to yield duplicate Prometheus metric families.

Fixes open-telemetry#2500
The flaky test_install_shim_affects_existing_tracers failure on Windows
was caused by stale OpenCensus thread-local state (a ContextTracer set
by a previous test, e.g. test_shim_with_sdk.py) leaking into TestPatch.
When that stale tracer was present, a bare Tracer() resolved to
ContextTracer instead of the expected NoopTracer.

Add execution_context.clean() to setUp (after uninstall_shim) and
tearDown to fully reset the OC thread-local store between tests.

Fixes open-telemetry#3975
@sridhar-3009 sridhar-3009 requested a review from a team as a code owner May 31, 2026 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Flaky opencensus shim test

1 participant