From 8ea886be1f3c91410e1a6023bbabf0ef710e4c44 Mon Sep 17 00:00:00 2001 From: TANGBUDU Date: Thu, 19 Nov 2026 01:57:05 +0100 Subject: [PATCH 1/2] fix(rtc): log a TypeError raised inside an EventEmitter handler instead of re-raising it emit re-raised every TypeError so that an event with too few arguments for the handler would raise. A TypeError from inside the handler was re-raised as well, which skipped the remaining handlers and interrupted the caller. The arguments are now checked against the signature before the call, and the call is wrapped on its own. --- .changeset/emitter-handler-type-error.md | 5 +++++ livekit-rtc/livekit/rtc/event_emitter.py | 8 +++++++- tests/rtc/test_emitter.py | 21 +++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 .changeset/emitter-handler-type-error.md diff --git a/.changeset/emitter-handler-type-error.md b/.changeset/emitter-handler-type-error.md new file mode 100644 index 000000000..8604dd52b --- /dev/null +++ b/.changeset/emitter-handler-type-error.md @@ -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. diff --git a/livekit-rtc/livekit/rtc/event_emitter.py b/livekit-rtc/livekit/rtc/event_emitter.py index 38f549011..55f93a391 100644 --- a/livekit-rtc/livekit/rtc/event_emitter.py +++ b/livekit-rtc/livekit/rtc/event_emitter.py @@ -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 @@ -67,7 +67,13 @@ 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 + sig.bind(*callback_args) + try: callback(*callback_args) + except Exception: + logger.exception(f"failed to emit event {event}") except TypeError: raise except Exception: diff --git a/tests/rtc/test_emitter.py b/tests/rtc/test_emitter.py index 4f67f76a4..6d69760c2 100644 --- a/tests/rtc/test_emitter.py +++ b/tests/rtc/test_emitter.py @@ -110,6 +110,27 @@ 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_on_does_not_warn() -> None: """Registering a callback must not emit a DeprecationWarning. From 0b27eb1c73a4be9715116ad56f01b146262cc28e Mon Sep 17 00:00:00 2001 From: TANGBUDU Date: Thu, 19 Nov 2026 02:26:54 +0100 Subject: [PATCH 2/2] check a once() handler's arguments against its callback once() registers a wrapper that takes any arguments, so the signature check passed for it and a once handler given too few arguments was logged instead of raising. The check now uses the callback the wrapper holds. --- livekit-rtc/livekit/rtc/event_emitter.py | 11 ++++++- tests/rtc/test_emitter.py | 38 ++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/livekit-rtc/livekit/rtc/event_emitter.py b/livekit-rtc/livekit/rtc/event_emitter.py index 55f93a391..bff43fda6 100644 --- a/livekit-rtc/livekit/rtc/event_emitter.py +++ b/livekit-rtc/livekit/rtc/event_emitter.py @@ -68,7 +68,15 @@ def greet(name): 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 + # 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) @@ -126,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: diff --git a/tests/rtc/test_emitter.py b/tests/rtc/test_emitter.py index 6d69760c2..2de7d03f5 100644 --- a/tests/rtc/test_emitter.py +++ b/tests/rtc/test_emitter.py @@ -131,6 +131,44 @@ def on_update_another(payload: Any) -> 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.