Repository navigation
fix(realtime): run async event handlers registered with connection.on - #925
pcbeingused333 wants to merge 1 commit into
Conversation
Signed-off-by: Alex Castillo <alex.castillog33@gmail.com>
PR SummaryLow Risk Overview
Adds regression tests: async Reviewed by Cursor Bugbot for commit 02ffbfc. Bugbot is set up for automated code reviews on this repo. Configure here. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
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) |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit 02ffbfc. Configure here.


Fixes
async defhandlers registered withRealtimeConnection.on(...)never running. Theclose()docstring shows this usage: a handler forCOMMITTED_TRANSCRIPTthat doesawait connection.close().Cause:
_emitand_emit_closecall each handler and drop the result. For anasync defhandler 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 becauseclose()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 insrc/elevenlabs/realtime/, which is listed in.fernignore.Test:
tests/test_realtime_close_event.pygets two cases: async handlers forOPENandCLOSErun, 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.