diff --git a/jupyter_core/utils/__init__.py b/jupyter_core/utils/__init__.py index 665eac2..ff71bf8 100644 --- a/jupyter_core/utils/__init__.py +++ b/jupyter_core/utils/__init__.py @@ -150,9 +150,15 @@ def run_sync(coro: Callable[..., Awaitable[T]]) -> Callable[..., T]: def wrapped(*args: Any, **kwargs: Any) -> Any: name = threading.current_thread().name inner = coro(*args, **kwargs) + + loop_running = False try: asyncio.get_running_loop() + loop_running = True except RuntimeError: + pass + + if not loop_running: # No loop running, run the loop for this thread. loop = ensure_event_loop() return loop.run_until_complete(inner)