Skip to content

Replace spin-polling with anyio.Event in ToolEventBroadcaster - #1

Closed
mattapperson wants to merge 1 commit into
mainfrom
fix/tool-event-broadcaster-spin-polling
Closed

Replace spin-polling with anyio.Event in ToolEventBroadcaster#1
mattapperson wants to merge 1 commit into
mainfrom
fix/tool-event-broadcaster-spin-polling

Conversation

@mattapperson

Copy link
Copy Markdown
Contributor

Summary

  • Replaced await anyio.sleep(0.001) spin-polling in _BroadcastConsumer.__anext__ with proper anyio.Event signaling
  • push() and complete() now set the event to wake blocked consumers immediately
  • The event is replaced after each set() call to support repeated wake cycles (anyio events are single-use)

Test plan

  • All 82 existing tests pass (asyncio + trio backends)
  • Added test_broadcaster_concurrent_push_and_consume to verify event-driven wake under concurrent producer/consumer
  • Added test_broadcaster_multiple_concurrent_consumers to verify multiple consumers wake correctly
  • ruff check passes
  • mypy passes (no new errors; pre-existing errors in unrelated files)

The _BroadcastConsumer.__anext__ method was using await anyio.sleep(0.001)
in a tight loop, burning CPU while waiting for new events. Replace this with
an anyio.Event that is set on push() and complete(), so consumers block
efficiently until signaled. The event is replaced after each set() to support
repeated wake cycles.

Adds concurrent producer/consumer tests to verify the event-driven behavior.
@mattapperson
mattapperson deleted the fix/tool-event-broadcaster-spin-polling branch April 16, 2026 20:19
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.

1 participant