Skip to content

Commit

Permalink
Raise error if EventEmitter used with async callback
Browse files Browse the repository at this point in the history
  • Loading branch information
bcherry committed Nov 21, 2024
1 parent f392454 commit 3e24162
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/participant_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def main(room: rtc.Room) -> None:
)

@room.on("participant_attributes_changed")
def on_participant_attributes_changed(
async def on_participant_attributes_changed(
changed_attributes: dict[str, str], participant: rtc.Participant
):
logging.info(
Expand Down
6 changes: 6 additions & 0 deletions livekit-rtc/livekit/rtc/event_emitter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import asyncio
from typing import Callable, Dict, Set, Optional, Generic, TypeVar

from .log import logger
Expand Down Expand Up @@ -156,6 +157,11 @@ def greet(name):
```
"""
if callback is not None:
if asyncio.iscoroutinefunction(callback):
raise ValueError(
"Cannot register an async callback with `.on()`. Use `asyncio.create_task` within your synchronous callback instead."
)

if event not in self._events:
self._events[event] = set()
self._events[event].add(callback)
Expand Down

0 comments on commit 3e24162

Please sign in to comment.