Skip to content

Fix BatchProcessor missed wakeup race condition#5401

Open
pranaysb wants to merge 3 commits into
open-telemetry:mainfrom
pranaysb:fix/batch-processor-race-condition
Open

Fix BatchProcessor missed wakeup race condition#5401
pranaysb wants to merge 3 commits into
open-telemetry:mainfrom
pranaysb:fix/batch-processor-race-condition

Conversation

@pranaysb

@pranaysb pranaysb commented Jul 8, 2026

Copy link
Copy Markdown

Description

Fixes a severe threading race condition in BatchProcessor that 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 the wait() 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.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

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

  • Tested with the local reproducer provided in the issue to verify the race condition vanishes and no spans are stuck in the queue.
  • Ran uv run pytest opentelemetry-sdk/tests/shared_internal/test_batch_processor.py (All 17 tests passed).
  • Formatted and verified with ruff check and ruff format.

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@pranaysb pranaysb requested a review from a team as a code owner July 8, 2026 08:47

@DylanRussell DylanRussell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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...

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.

[Bug]: BatchProcessor missed wakeup race condition causes export latency spikes

2 participants