Skip to content

Conversation

@AThePeanut4
Copy link
Contributor

#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.

@minrk minrk closed this Oct 8, 2025
@minrk minrk reopened this Oct 8, 2025
@minrk minrk merged commit db8d93d into jupyter:main Oct 8, 2025
31 checks passed
@AThePeanut4 AThePeanut4 deleted the run-sync-runtimeerror branch October 8, 2025 15:36
@minrk minrk changed the title fix: handle missing loop case outside except in run_sync fix: nicer traceback in missing loop case outside except in run_sync Oct 15, 2025
@minrk minrk changed the title fix: nicer traceback in missing loop case outside except in run_sync nicer traceback in missing loop case outside except in run_sync Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants