Skip to content

Fix missed wakeup race condition in BatchProcessor#5409

Closed
GauthamPrabhuM wants to merge 1 commit into
open-telemetry:mainfrom
GauthamPrabhuM:fix-batch-processor-missed-wakeup
Closed

Fix missed wakeup race condition in BatchProcessor#5409
GauthamPrabhuM wants to merge 1 commit into
open-telemetry:mainfrom
GauthamPrabhuM:fix-batch-processor-missed-wakeup

Conversation

@GauthamPrabhuM

@GauthamPrabhuM GauthamPrabhuM commented Jul 13, 2026

Copy link
Copy Markdown

Description

BatchProcessor.worker() clears _worker_awaken only after its export loop finishes. If an application thread fills a batch and calls _worker_awaken.set() in the window between the export loop draining the queue and the worker calling clear(), the wakeup signal is erased and the full batch sits in the queue for up to schedule_delay (5s by default). This affects both BatchSpanProcessor and BatchLogRecordProcessor since they share this implementation, and shows up as recurring export-latency spikes under high throughput.

This PR clears the event immediately after wait() returns, before exporting, so a set() that arrives while the worker is exporting is preserved for the next wait(). Worst case is one benign spurious wakeup that exports nothing. shutdown() is unaffected because worker exit is gated on the _shutdown flag, not the event.

Regarding the question raised on the earlier attempt (#5401) — whether _should_export_batch already covers this because it keeps exporting while the queue exceeds the batch size: it doesn't cover this window, because the lost signal arrives after the export loop has already exited and before the event is cleared. The regression test demonstrates this deterministically: it fails on main (items stay queued while the worker sleeps) and passes with this change.

Fixes #5400

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

  • New regression test test_wakeup_not_lost_when_batch_fills_during_event_clear in opentelemetry-sdk/tests/shared_internal/test_batch_processor.py, parametrized over BatchLogRecordProcessor and BatchSpanProcessor. It hooks _worker_awaken.clear to inject a full batch at the exact race point; fails without the fix, passes with it (~0.05s, stable across repeated runs).
  • Full opentelemetry-sdk test suite, tox -e precommit (ruff), tox -e lint-opentelemetry-sdk (pylint 10/10), and tox -e typecheck (pyright) all pass.

Does This PR Require a Contrib Repo Change?

  • No.

Checklist:

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

Assisted-by: Claude Fable 5

🤖 Generated with Claude Code

@GauthamPrabhuM GauthamPrabhuM requested a review from a team as a code owner July 13, 2026 07:08
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 13, 2026

Copy link
Copy Markdown

CLA Not Signed

The worker thread cleared _worker_awaken only after its export loop
finished. If an application thread filled a batch and set the event in
the window between the export loop draining the queue and the worker
calling clear(), the wakeup was erased and the full batch sat in the
queue for up to schedule_delay (5s by default).

Clear the event immediately after wait() returns instead, so any set()
that arrives while the worker is exporting is preserved for the next
wait(). This affects both BatchSpanProcessor and BatchLogRecordProcessor
since they share this implementation.

Fixes open-telemetry#5400

Assisted-by: Claude Fable 5
@GauthamPrabhuM GauthamPrabhuM force-pushed the fix-batch-processor-missed-wakeup branch from f9f3348 to 0f105cc Compare July 13, 2026 07:10

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

I described why I thought this wasn't needed and closed the PR in #5401 -- why is this re-opened under a different user now ?

@herin049

Copy link
Copy Markdown
Contributor

I described why I thought this wasn't needed and closed the PR in #5401 -- why is this re-opened under a different user now ?

I'll close it again, we can report the user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

3 participants