Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/emitter-handler-type-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-rtc": patch
---

`EventEmitter.emit` logs a `TypeError` raised inside a handler instead of re-raising it. An event with too few arguments for the handler still raises.
17 changes: 16 additions & 1 deletion livekit-rtc/livekit/rtc/event_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def greet(name):

has_varargs = any(p.kind == p.VAR_POSITIONAL for p in params)
if has_varargs:
callback(*args)
callback_args = args
else:
positional_params = [
p
Expand All @@ -67,7 +67,21 @@ def greet(name):
num_args = min(len(args), num_params)
callback_args = args[:num_args]

# an event with too few arguments for the handler raises; a TypeError
# from inside the handler is logged like any other exception.
# once() registers a wrapper that takes any arguments, so the check
# is made against the callback it wraps
once_target = getattr(callback, "_once_callback", None)
if once_target is not None:
try:
sig = inspect.signature(once_target)
except (TypeError, ValueError):
pass # no signature (e.g. a builtin): nothing to check
sig.bind(*callback_args)
try:
callback(*callback_args)
except Exception:
logger.exception(f"failed to emit event {event}")
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Resolved
except TypeError:
raise
except Exception:
Expand Down Expand Up @@ -120,6 +134,7 @@ def once_callback(*args: Any, **kwargs: Any) -> None:
self.off(event, once_callback)
callback(*args, **kwargs)

setattr(once_callback, "_once_callback", callback) # noqa: B010
return self.on(event, once_callback)
else:

Expand Down
59 changes: 59 additions & 0 deletions tests/rtc/test_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,65 @@ def on_error_another() -> None:
assert len(calls) == 2


def test_type_error_in_handler_is_logged() -> None:
EventTypes = Literal["update"]

emitter = EventEmitter[EventTypes]()

calls = []

@emitter.on("update")
def on_update(payload: Any) -> None:
calls.append(True)
payload["user"]["name"]

@emitter.on("update")
def on_update_another(payload: Any) -> None:
calls.append(True)

emitter.emit("update", {"user": None})

assert len(calls) == 2


def test_once_args() -> None:
EventTypes = Literal["whatever"]

emitter = EventEmitter[EventTypes]()

calls = []

@emitter.once("whatever")
def on_whatever(first: Any, second: Any) -> None:
calls.append((first, second))

with pytest.raises(TypeError):
emitter.emit("whatever", 1)

assert calls == []


def test_type_error_in_once_handler_is_logged() -> None:
EventTypes = Literal["update"]

emitter = EventEmitter[EventTypes]()

calls = []

@emitter.once("update")
def on_update(payload: Any) -> None:
calls.append(True)
payload["user"]["name"]

@emitter.on("update")
def on_update_another(payload: Any) -> None:
calls.append(True)

emitter.emit("update", {"user": None})

assert len(calls) == 2


def test_on_does_not_warn() -> None:
"""Registering a callback must not emit a DeprecationWarning.

Expand Down
Loading