@@ -266,7 +266,8 @@ def remove_listener(self, listener: RealtimeModelListener) -> None:
266266
267267 async def _emit_event (self , event : RealtimeModelEvent ) -> None :
268268 """Emit an event to the listeners."""
269- for listener in self ._listeners :
269+ # Copy list to avoid modification during iteration
270+ for listener in list (self ._listeners ):
270271 await listener .on_event (event )
271272
272273 async def _listen_for_messages (self ):
@@ -431,7 +432,7 @@ async def _send_interrupt(self, event: RealtimeModelSendInterrupt) -> None:
431432 and session .audio is not None
432433 and session .audio .input is not None
433434 and session .audio .input .turn_detection is not None
434- and session .audio .input .turn_detection .interrupt_response is True ,
435+ and session .audio .input .turn_detection .interrupt_response is True
435436 )
436437 if not automatic_response_cancellation_enabled :
437438 await self ._cancel_response ()
@@ -516,6 +517,10 @@ async def close(self) -> None:
516517 self ._websocket = None
517518 if self ._websocket_task :
518519 self ._websocket_task .cancel ()
520+ try :
521+ await self ._websocket_task
522+ except asyncio .CancelledError :
523+ pass
519524 self ._websocket_task = None
520525
521526 async def _cancel_response (self ) -> None :
@@ -616,7 +621,7 @@ async def _handle_ws_event(self, event: dict[str, Any]):
616621 and session .audio is not None
617622 and session .audio .input is not None
618623 and session .audio .input .turn_detection is not None
619- and session .audio .input .turn_detection .interrupt_response is True ,
624+ and session .audio .input .turn_detection .interrupt_response is True
620625 )
621626 if not automatic_response_cancellation_enabled :
622627 await self ._cancel_response ()
0 commit comments