Repository navigation
fix(rtc): reject async callbacks in EventEmitter.once #903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "livekit-rtc": patch | ||
| --- | ||
|
|
||
| `EventEmitter.once()` now raises `ValueError` for an async callback, as `.on()` already did, instead of registering a handler that never ran. Both methods also reject an instance whose class defines `async def __call__`. Code that passed an async callback to `.once()`, including `@room.once(...)` on an `async def`, now fails at registration and should use a synchronous callback that starts a task. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import asyncio | ||
| import functools | ||
| import inspect | ||
| from typing import Any, Callable, Dict, Optional, Generic, TypeVar | ||
|
|
||
|
|
@@ -13,6 +14,34 @@ | |
| # function in 3.16, and a bare attribute access would then fail at import time. | ||
| _ASYNCIO_COROUTINE_MARKER = getattr(asyncio.coroutines, "_is_coroutine", None) | ||
|
|
||
|
|
||
| def _check_async_callback(callback: Callable, method_name: str) -> None: | ||
| # A partial is checked by what it wraps, since that is what calling it runs. | ||
| target = callback | ||
| while isinstance(target, functools.partial): | ||
| target = target.func | ||
|
|
||
| if ( | ||
| inspect.iscoroutinefunction(target) | ||
| or ( | ||
| _ASYNCIO_COROUTINE_MARKER is not None | ||
| and getattr(target, "_is_coroutine", None) is _ASYNCIO_COROUTINE_MARKER | ||
| ) | ||
| # An instance whose class defines `async def __call__`. A class itself is left | ||
| # alone: calling it only constructs an instance, which is synchronous. | ||
| or ( | ||
| not inspect.isclass(target) | ||
| and inspect.iscoroutinefunction(getattr(target, "__call__", None)) | ||
| ) | ||
|
NabiBukhsh-AI marked this conversation as resolved.
Resolved
|
||
| ): | ||
| raise ValueError( | ||
| f"Cannot register an async callback with `.{method_name}()`. Use " | ||
| "`asyncio.create_task` within your synchronous callback instead, and keep a " | ||
| "reference to the returned task until it finishes: the event loop only holds " | ||
| "a weak reference, so an unreferenced task can be garbage-collected early." | ||
| ) | ||
|
|
||
|
|
||
| T_contra = TypeVar("T_contra", contravariant=True) | ||
|
|
||
|
|
||
|
|
@@ -115,6 +144,7 @@ def greet_once(name): | |
| ``` | ||
| """ | ||
| if callback is not None: | ||
| _check_async_callback(callback, "once") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This is a user-visible behavior change. Passing an async callback to |
||
|
|
||
| def once_callback(*args: Any, **kwargs: Any) -> None: | ||
| self.off(event, once_callback) | ||
|
|
@@ -169,13 +199,7 @@ def greet(name): | |
| ``` | ||
| """ | ||
| if callback is not None: | ||
| if inspect.iscoroutinefunction(callback) or ( | ||
| _ASYNCIO_COROUTINE_MARKER is not None | ||
| and getattr(callback, "_is_coroutine", None) is _ASYNCIO_COROUTINE_MARKER | ||
| ): | ||
| raise ValueError( | ||
| "Cannot register an async callback with `.on()`. Use `asyncio.create_task` within your synchronous callback instead." | ||
| ) | ||
| _check_async_callback(callback, "on") | ||
|
|
||
| if event not in self._events: | ||
| self._events[event] = {} | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-existing, optional: an instance of a class with
async def __call__still passes this check for both.on()and.once(), since neitherinspect.iscoroutinefunction(callback)nor the_is_coroutinemarker looks atcallback.__call__. Onemitthe coroutine is created and dropped with a "never awaited" warning, and the handler never runs.Since this helper is now shared, it would be a cheap place to also reject
inspect.iscoroutinefunction(getattr(callback, "__call__", None)).