asyncio.run() cannot be called from a running event loop
dspdhckr opened this issue · comments
Confirmation
- I have read the README.md on the project homepage
- I have checked if identical issue already exists
- I have tried downgrading to find version that can be used as a workaround
The problem
I am not able to go beyond adding Plex user. After adding Plex user, it just throws this error where I can not go and edit anything.
Steps to reproduce the behavior
- Download the package
- Login with Plex credentials
Error trace / logs
root@dubai---1:/home/ubuntu# plextraktsync sync
INFO PlexTraktSync [0.30.4]
Please enter your Plex username or e-mail (): my email
Please enter your Plex password:
Enter a 2FA code if enabled, or leave blank otherwise:
╭─ Plex Login ───────────────────╮
│ │
│ Login to MyPlex was successful │
│ │
╰────────────────────────────────╯
ERROR asyncio.run() cannot be called from a running event loop
╭─────────────────────────────────────────────────────────────── Traceback (most recent call last) ───────────────────────────────────────────────────────────────╮
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/cli.py:26 in wrap │
│ │
│ 23 │ │ │ cmd = getattr(module, name) │
│ 24 │ │ │ │
│ 25 │ │ │ try: │
│ ❱ 26 │ │ │ │ cmd(*args, **kwargs) │
│ 27 │ │ │ except EOFError as e: │
│ 28 │ │ │ │ raise ClickException(f"Program requested terminal, No terminal is │
│ connected: {e}") │
│ 29 │ │ │ except ClickException as e: │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/decorators/coro.py:13 in wrapper │
│ │
│ 10 │ """ │
│ 11 │ @wraps(f) │
│ 12 │ def wrapper(*args, **kwargs): │
│ ❱ 13 │ │ return asyncio.run(f(*args, **kwargs)) │
│ 14 │ │
│ 15 │ return wrapper │
│ 16 │
│ │
│ /usr/lib/python3.10/asyncio/runners.py:44 in run │
│ │
│ 41 │ │ events.set_event_loop(loop) │
│ 42 │ │ if debug is not None: │
│ 43 │ │ │ loop.set_debug(debug) │
│ ❱ 44 │ │ return loop.run_until_complete(main) │
│ 45 │ finally: │
│ 46 │ │ try: │
│ 47 │ │ │ _cancel_all_tasks(loop) │
│ │
│ /usr/lib/python3.10/asyncio/base_events.py:649 in run_until_complete │
│ │
│ 646 │ │ if not future.done(): │
│ 647 │ │ │ raise RuntimeError('Event loop stopped before Future completed.') │
│ 648 │ │ │
│ ❱ 649 │ │ return future.result() │
│ 650 │ │
│ 651 │ def stop(self): │
│ 652 │ │ """Stop running the event loop. │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/commands/sync.py:48 in sync │
│ │
│ 45 │ │ ) │
│ 46 │ │ config.update(batch_delay=batch_delay) │
│ 47 │ │
│ ❱ 48 │ ensure_login() │
│ 49 │ wc = factory.walk_config.update(movies=movies, shows=shows, watchlist=watchlist) │
│ 50 │ w = factory.walker │
│ 51 │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/commands/login.py:10 in ensure_login │
│ │
│ 7 │
│ 8 def ensure_login(): │
│ 9 │ if not factory.has_plex_token: │
│ ❱ 10 │ │ plex_login_autoconfig() │
│ 11 │ │
│ 12 │ if not has_trakt_token(): │
│ 13 │ │ trakt_login_autoconfig() │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/commands/plex_login.py:187 in plex_login_autoconfig │
│ │
│ 184 def plex_login_autoconfig(): │
│ 185 │ username = environ.get("PLEX_USERNAME", CONFIG["PLEX_USERNAME"]) │
│ 186 │ password = environ.get("PLEX_PASSWORD", None) │
│ ❱ 187 │ login(username, password) │
│ 188 │
│ 189 │
│ 190 def plex_login(username, password): │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/commands/plex_login.py:203 in login │
│ │
│ 200 │ print(Panel.fit("Login to MyPlex was successful", title="Plex Login", │
│ 201 │ │ │ │ │ title_align="left", padding=1, border_style="bright_blue")) │
│ 202 │ │
│ ❱ 203 │ (server, plex) = choose_server(account) │
│ 204 │ print(success(f"Connection to {plex.friendlyName} established successfully!")) │
│ 205 │ │
│ 206 │ token = server.accessToken │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/commands/plex_login.py:163 in choose_server │
│ │
│ 160 def choose_server(account: MyPlexAccount) -> tuple[MyPlexResource, PlexServer]: │
│ 161 │ while True: │
│ 162 │ │ try: │
│ ❱ 163 │ │ │ server = pick_server(account) │
│ 164 │ │ │ if not server: │
│ 165 │ │ │ │ raise ClickException("Unable to find server from Plex account") │
│ 166 │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/commands/plex_login.py:150 in pick_server │
│ │
│ 147 │ if len(servers) == 1: │
│ 148 │ │ return servers[0] │
│ 149 │ │
│ ❱ 150 │ server_name = prompt_server(servers) │
│ 151 │ │
│ 152 │ # Sanity check, even the user can't choose invalid resource │
│ 153 │ server = account.resource(server_name) │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/plextraktsync/commands/plex_login.py:139 in prompt_server │
│ │
│ 136 │ │ default=None, │
│ 137 │ │ qmark="", │
│ 138 │ │ pointer=">", │
│ ❱ 139 │ ).execute() │
│ 140 │
│ 141 │
│ 142 def pick_server(account: MyPlexAccount) -> MyPlexResource | None: │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/InquirerPy/base/simple.py:330 in execute │
│ │
│ 327 │ │ Raises: │
│ 328 │ │ │ KeyboardInterrupt: When `ctrl-c` is pressed and `raise_keyboard_interrupt` │
│ is True. │
│ 329 │ │ """ │
│ ❱ 330 │ │ result = self._run() │
│ 331 │ │ if raise_keyboard_interrupt is not None: │
│ 332 │ │ │ self._raise_kbi = not os.getenv( │
│ 333 │ │ │ │ "INQUIRERPY_NO_RAISE_KBI", not raise_keyboard_interrupt │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/InquirerPy/base/complex.py:208 in _run │
│ │
│ 205 │ │
│ 206 │ def _run(self) -> Any: │
│ 207 │ │ """Run the application.""" │
│ ❱ 208 │ │ return self.application.run() │
│ 209 │ │
│ 210 │ async def _run_async(self) -> None: │
│ 211 │ │ """Run the application asynchronously.""" │
│ │
│ /root/.local/pipx/venvs/plextraktsync/lib/python3.10/site-packages/prompt_toolkit/application/application.py:1002 in run │
│ │
│ 999 │ │ │
│ 1000 │ │ else: │
│ 1001 │ │ │ # No loop installed. Run like usual. │
│ ❱ 1002 │ │ │ return asyncio.run(coro) │
│ 1003 │ │
│ 1004 │ def _handle_exception( │
│ 1005 │ │ self, loop: AbstractEventLoop, context: dict[str, Any] │
│ │
│ /usr/lib/python3.10/asyncio/runners.py:33 in run │
│ │
│ 30 │ │ asyncio.run(main()) │
│ 31 │ """ │
│ 32 │ if events._get_running_loop() is not None: │
│ ❱ 33 │ │ raise RuntimeError( │
│ 34 │ │ │ "asyncio.run() cannot be called from a running event loop") │
│ 35 │ │
│ 36 │ if not coroutines.iscoroutine(main): │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
RuntimeError: asyncio.run() cannot be called from a running event loop
Error: Error running sync command: asyncio.run() cannot be called from a running event loop
sys:1: RuntimeWarning: coroutine 'Application.run_async' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Expected behavior
It should work without any issues and login to services without any errors.
Inspect of problematic items
No response
Workarounds
No response
Config file contents
No response
Install method
pipx
Version
0.30.4
Python Version
3.10.12
Plex Server Version
1.89.1.107-20abacd1
Operating System and Version
Ubuntu 22.04
Workarounds
No response
Do not ignore the bug report form. Downgrade! and report version that continued to work.
Avoid creating clickbait issues, fill the problem into title: "asyncio.run() cannot be called from a running event loop".
I think your problem only happens on first run. if your credentials are saved (it won't ask you), then this error will not occur.
also: don't run this program as root, it does not require any elevated privileges.
Setup a new instance this afternoon and saw this error as well after plex sign-in.
Did some testing and issue seems to begin on version 0.29.13
Version 0.29.12 was able to login and setup.
Also can confirm once sign in is complete don't see any errors on next sync. Will follow up once I've completed latest sync runs.
Thanks.
![Screenshot 2024-05-08 at 23 17 55](https://private-user-images.githubusercontent.com/132819104/329070474-0fcaa735-7e5f-4cdd-920a-21d9a84dfe23.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjA1NDUwNzAsIm5iZiI6MTcyMDU0NDc3MCwicGF0aCI6Ii8xMzI4MTkxMDQvMzI5MDcwNDc0LTBmY2FhNzM1LTdlNWYtNGNkZC05MjBhLTIxZDlhODRkZmUyMy5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQwNzA5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MDcwOVQxNzA2MTBaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT03ZWYxMjFmMDM3NDk0NTI3ZDU3YmE4NzA5NWI0NGU4M2ZkNDI0MDlmMGJiNzI4ZDU1MjMyYmRmYjYzZWQzZDkxJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCZhY3Rvcl9pZD0wJmtleV9pZD0wJnJlcG9faWQ9MCJ9.-Q7DMGInZIxZkWznKRvXcVcux5Me8JXyvxJ7SPbiZwo)
I am sorry that I created the issue with the clickbait title but that was not my intention. I have no proficiency with the language the program is written and couldn't find the appropriate line to put. Anyways I will keep this in mind for the next issues. I have attached the screenshot with this comment and even though I did the initial setup and as per your suggestion the credentials are saved, but still while doing sync or watch, it is throwing an error like above.
This should to fix #1941