Skip to content

fix(realtime): run async event handlers registered with connection.on - #925

Open
pcbeingused333 wants to merge 1 commit into
elevenlabs:mainfrom
pcbeingused333:fix/realtime-async-handler-ref
Open

pcbeingused333 wants to merge 1 commit into
elevenlabs:mainfrom
pcbeingused333:fix/realtime-async-handler-ref

Conversation

@pcbeingused333

Copy link
Copy Markdown

Fixes async def handlers registered with RealtimeConnection.on(...) never running. The close() docstring shows this usage: a handler for COMMITTED_TRANSCRIPT that does await connection.close().

Cause: _emit and _emit_close call each handler and drop the result. For an async def handler this only creates a coroutine object that is never awaited, so the handler body never runs (Python only emits a "coroutine was never awaited" RuntimeWarning).

Fix: if a handler returns an awaitable, schedule it with asyncio.ensure_future (both emit paths run inside the already-running loop) and keep a reference to the task until it finishes. Exceptions from the scheduled task are reported the same way as synchronous handler errors. Handlers are scheduled rather than awaited inline because close() cancels and awaits the message task, which a handler running inside that task could not do. As a result, async handlers for successive events can overlap if they suspend. Sync handlers behave as before. The change is in src/elevenlabs/realtime/, which is listed in .fernignore.

Test: tests/test_realtime_close_event.py gets two cases: async handlers for OPEN and CLOSE run, and a suspended async handler survives a garbage collection pass and then finishes. Both fail before the change and pass after.


This PR was prepared with the help of an AI assistant (Claude Code) and submitted by Alex.

Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
@cursor

cursor Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Scoped to realtime event dispatch; synchronous handlers behave as before, with clearer execution for previously broken async handlers.

Overview
Fixes async def handlers on RealtimeConnection.on(...) so they actually run instead of creating un-awaited coroutines.

_emit and _emit_close now detect when a handler returns an awaitable, schedule it with asyncio.ensure_future, and retain the task in _handler_tasks until it completes (so suspended handlers are not garbage-collected). Handler exceptions are still logged; sync handlers are unchanged.

Adds regression tests: async OPEN/CLOSE handlers run, and a suspended async handler completes after GC.

Reviewed by Cursor Bugbot for commit 02ffbfc. Bugbot is set up for automated code reviews on this repo. Configure here.

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

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 02ffbfc. Configure here.

if not t.cancelled() and t.exception() is not None:
print(f"Error in event handler for {event}: {t.exception()}")

task.add_done_callback(_report)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Close skips pending async handlers

Medium Severity

_schedule_if_awaitable fires async handlers with ensure_future and never waits for them. close() returns as soon as the message task finishes, so a CLOSE handler that suspends is still pending. asyncio.run then cancels leftover tasks, which drops that cleanup. Sync handlers used to finish inside _emit_close before close() returned.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 02ffbfc. Configure here.

This branch has not been deployed

No deployments
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