Fix BatchProcessor missed wakeup race condition#5401
Conversation
Assisted-by: Claude Opus 4.6
DylanRussell
left a comment
There was a problem hiding this comment.
Can you add a test case illustrating the race condition ? I don't follow why this change is necessary..
Take a look at _should_export_batch -- we will continue to call export as long as the batch size is exceeded (this is confusing because we have EXPORT_WHILE_BATCH_EXCEEDS_THRESHOLD strategy in the enum even though we always do that.. we could remove that value from the enum...)
| batch_processor._batch_processor.emit(telemetry) | ||
| exporter.export.assert_called_once() | ||
|
|
||
| def test_telemetry_not_delayed_by_wakeup_race_condition( |
There was a problem hiding this comment.
did you actually run into this scenario ? it seems incredibly unlikely to occur, to get a big batch immediately after the export loop exits but before clear() is called ?
If we move the clear call to after export like you are proposing we potentially introduce a new issue -- because emit will set the event if we are over the batch size, and then we will immediately wake up without sleeping, but we could still be in the export loop at that point, still draining the queue at that point... does that make sense ?
I think it is correct where it is placed now...
Description
Fixes a severe threading race condition in
BatchProcessorthat wiped the wakeup signal if a batch filled immediately after a check but before the event was cleared. Moving_worker_awaken.clear()to the start of thewait()loop fixes this and eliminates up to 5-second latency spikes in export pipelines under heavy load.Fixes #5400
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
uv run pytest opentelemetry-sdk/tests/shared_internal/test_batch_processor.py(All 17 tests passed).ruff checkandruff format.Does This PR Require a Contrib Repo Change?
Checklist: