Skip to content

fix: handle missing loop case outside except in run_sync #437

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

AThePeanut4
Copy link

#425 moved the "no running loop" case in run_sync to inside the except block, which causes any exceptions thrown by the inner function to have the RuntimeError added as "context". That then means that if there is no running event loop, all printed exception tracebacks from the wrapped function have an annoying "RuntimeError: no running event loop" traceback prepended to them.

To reproduce, run this with just python directly:

from jupyter_core.utils import run_sync

async def f():
    raise Exception("some exception")

run_sync(f)()

It gives

Traceback (most recent call last):
  File "/private/tmp/test/.venv/lib/python3.13/site-packages/jupyter_core/utils/__init__.py", line 159, in wrapped
    asyncio.get_running_loop()
    ~~~~~~~~~~~~~~~~~~~~~~~~^^
RuntimeError: no running event loop

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/private/tmp/test/main.py", line 6, in <module>
    run_sync(f)()
    ~~~~~~~~~~~^^
  File "/private/tmp/test/.venv/lib/python3.13/site-packages/jupyter_core/utils/__init__.py", line 163, in wrapped
    return loop.run_until_complete(inner)
           ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
  File "/opt/homebrew/Cellar/[email protected]/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/asyncio/base_events.py", line 725, in run_until_complete
    return future.result()
           ~~~~~~~~~~~~~^^
  File "/private/tmp/test/main.py", line 4, in f
    raise Exception("some exception")
Exception: some exception

The actual place I was getting this issue was with qtconsole and the in-process ipykernel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant