From 43b08a5d7580f94013c6b2df3ba8f48494bd92d2 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Fri, 18 Sep 2026 23:55:47 +0530 Subject: [PATCH] test(sdk): fix flaky BatchProcessor shutdown test (#5663) In test_shutdown_allows_1_export_to_finish, processor.shutdown() interrupts the in-progress export via exporter.shutdown(). Depending on thread scheduling, particularly on Windows and PyPy runners, the worker thread can terminate before shutdown() returns, causing the intermediate assertion assert worker_thread.is_alive() is True to fail intermittently. Fix this by: - Using time.monotonic() instead of time.time() for elapsed time. - Removing the unstable intermediate is_alive() is True check. - Waiting deterministically with worker_thread.join(timeout=1) before asserting that the worker thread has terminated. Fixes #5663 Assisted-by: Gemini 3.8 Flash --- .../tests/shared_internal/test_batch_processor.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py b/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py index 3fa263d1153..49186404350 100644 --- a/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py +++ b/opentelemetry-sdk/tests/shared_internal/test_batch_processor.py @@ -274,15 +274,16 @@ def test_shutdown_allows_1_export_to_finish(self, batch_processor_class, telemet processor._batch_processor.emit(telemetry) processor._batch_processor.emit(telemetry) processor._batch_processor.emit(telemetry) - before = time.time() + before = time.monotonic() processor._batch_processor.shutdown(timeout_millis=3000) - # Shutdown does not kill the thread. - assert processor._batch_processor._worker_thread.is_alive() is True - after = time.time() + after = time.monotonic() assert after - before < 3.3 - # Thread will naturally finish after a little bit. - time.sleep(0.1) + + # The exporter shutdown interrupts the in-progress export. Depending + # on thread scheduling, the worker may stop before or shortly after + # shutdown() returns. + processor._batch_processor._worker_thread.join(timeout=1) assert processor._batch_processor._worker_thread.is_alive() is False # Expect the second call to be interrupted by shutdown, and the third call to never be made. assert exporter.sleep_interrupted is True