From fd30b143eac44ee7c478351a8714dc6964f1da07 Mon Sep 17 00:00:00 2001 From: Stephanie Anderson Date: Fri, 27 Mar 2026 12:23:31 +0100 Subject: [PATCH] fix(tests): Fix flaky continuous profiler test under gevent The `assert_single_transaction_with_profile_chunks` helper now polls for profile_chunk envelope items with a timeout instead of asserting immediately. Under gevent on slow CI runners, the profiler thread may not have flushed its buffer by the time the assertion runs, causing sporadic failures. Co-Authored-By: Claude Opus 4.6 (1M context) --- tests/profiler/test_continuous_profiler.py | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/tests/profiler/test_continuous_profiler.py b/tests/profiler/test_continuous_profiler.py index f0c3b5a27f..f1f9a63227 100644 --- a/tests/profiler/test_continuous_profiler.py +++ b/tests/profiler/test_continuous_profiler.py @@ -135,13 +135,28 @@ def test_continuous_profiler_setup_twice(mode, make_options, teardown_profiling) assert not is_profile_session_sampled() -def assert_single_transaction_with_profile_chunks( - envelopes, thread, max_chunks=None, transactions=1 -): +def _collect_envelope_items(envelopes): items = defaultdict(list) for envelope in envelopes: for item in envelope.items: items[item.type].append(item) + return items + + +def assert_single_transaction_with_profile_chunks( + envelopes, thread, max_chunks=None, transactions=1, timeout=2.0 +): + # Poll for profile_chunk items to arrive, since the profiler thread + # may not have flushed yet when this assertion runs (especially under + # gevent on slow CI runners). + deadline = time.monotonic() + timeout + while True: + items = _collect_envelope_items(envelopes) + if len(items["profile_chunk"]) > 0: + break + if time.monotonic() >= deadline: + break + time.sleep(0.01) assert len(items["transaction"]) == transactions assert len(items["profile_chunk"]) > 0