Drop the tracing singleton and its context after each test. `TraceCollectionListener` is a singleton, so without this three things leak for the rest of the xdist worker: - `TraceBatchManager.trace_batch_id`, which moves later trace POSTs from `/tracing/ephemeral/batches` to `/tra
()
| 204 | |
| 205 | @pytest.fixture(autouse=True, scope="function") |
| 206 | def reset_tracing_state() -> Generator[None, Any, None]: |
| 207 | """Drop the tracing singleton and its context after each test. |
| 208 | |
| 209 | `TraceCollectionListener` is a singleton, so without this three things leak |
| 210 | for the rest of the xdist worker: |
| 211 | |
| 212 | - `TraceBatchManager.trace_batch_id`, which moves later trace POSTs from |
| 213 | `/tracing/ephemeral/batches` to `/tracing/batches/<id>/events` until some |
| 214 | unrelated cassette stops matching, naming neither the leak nor its source. |
| 215 | - `_listeners_setup`, which makes `setup_listeners` return early |
| 216 | (`trace_listener.py:208`) after `cleanup_event_handlers` has wiped the bus, |
| 217 | so tracing silently registers nothing and collects no events. |
| 218 | - the `_tracing_enabled` context var, which leaves tracing on for later tests |
| 219 | and re-registers `on_task_failed` alongside telemetry's — breaking |
| 220 | `test_task_failure_instrumentation`, which requires one handler per event. |
| 221 | |
| 222 | All three go together: clearing the context vars is what makes dropping the |
| 223 | singleton safe, because the replacement listener then sees tracing disabled |
| 224 | and registers nothing. Dropping the singleton alone re-registers handlers and |
| 225 | breaks the telemetry test. |
| 226 | """ |
| 227 | yield |
| 228 | |
| 229 | from crewai.events.listeners.tracing import utils as tracing_utils |
| 230 | from crewai.events.listeners.tracing.trace_listener import TraceCollectionListener |
| 231 | |
| 232 | tracing_utils._tracing_enabled.set(None) |
| 233 | tracing_utils._tui_mode.set(False) |
| 234 | TraceCollectionListener._instance = None |
| 235 | |
| 236 | |
| 237 | @pytest.fixture(autouse=True, scope="function") |