Skip to content

fix(tracing): avoid busy-polling in BatchTraceProcessor - #4703

Closed
mikemikimike wants to merge 1 commit into
openai:mainfrom
mikemikimike:codex/issue-4688-event-wait
Closed

fix(tracing): avoid busy-polling in BatchTraceProcessor#4703
mikemikimike wants to merge 1 commit into
openai:mainfrom
mikemikimike:codex/issue-4688-event-wait

Conversation

@mikemikimike

Copy link
Copy Markdown

Fixes #4688nnnBatchTraceProcessor now waits on its shutdown event until the next scheduled export instead of polling with time.sleep(0.2). This removes idle wakeups while preserving the existing 200 ms queue-trigger responsiveness and makes shutdown interrupt the wait immediately.nnTests:n- uv run --frozen pytest tests/test_trace_processor.py::test_batch_trace_processor_shutdown_interrupts_idle_worker -vvn- uv run --frozen ruff format --check src/agents/tracing/processors.py tests/test_trace_processor.pyn- uv run --frozen ruff check src/agents/tracing/processors.py tests/test_trace_processor.pyn- uv run --frozen mypy src/agents/tracing/processors.pyn- git diff --checknnThe full test file was attempted but hung after 13 tests on Windows and was stopped.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8df9c0db6e

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

time.sleep(0.2)
# Wait until the next scheduled export or shutdown, whichever comes first.
timeout = max(0.0, min(0.2, self._next_export_time - time.monotonic()))
self._shutdown_event.wait(timeout)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Publish the shutdown deadline before waking the worker

When shutdown(timeout=...) is called while the worker is waiting with queued work below the trigger threshold, setting _shutdown_event wakes this wait before shutdown() assigns _shutdown_deadline. The worker can therefore enter the final drain with deadline=None and call BackendSpanExporter.export() without the requested deadline; a stalled HTTP request may then keep the worker alive for the client's normal 60-second timeout. Assign the deadline before signaling the event so the awakened worker always observes the finite shutdown budget.

AGENTS.md reference: AGENTS.md:L149-L149

Useful? React with 👍 / 👎.

# Sleep a short interval so we don't busy-wait.
time.sleep(0.2)
# Wait until the next scheduled export or shutdown, whichever comes first.
timeout = max(0.0, min(0.2, self._next_export_time - time.monotonic()))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Wake on enqueue instead of retaining the 200 ms poll

With the default schedule_delay=5.0 and a queue below the export threshold, this timeout remains capped at 0.2 seconds, so Event.wait() expires and the worker rechecks the queue five times per second even when completely idle—exactly the same wakeup cadence as the replaced time.sleep(0.2). This improves shutdown latency but does not remove the idle polling targeted by the change; the worker needs an enqueue/shutdown notification or a blocking queue wait so it can sleep until actual work or the scheduled export.

Useful? React with 👍 / 👎.

@seratch

seratch commented Aug 27, 2026

Copy link
Copy Markdown
Member

Please refer to #4688 (comment)

@seratch seratch closed this Aug 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BatchTraceProcessor worker thread busy-polls with time.sleep(0.2) instead of waiting on its shutdown event

2 participants