diff --git a/jupyter_core/utils/__init__.py b/jupyter_core/utils/__init__.py index 4420c1a..665eac2 100644 --- a/jupyter_core/utils/__init__.py +++ b/jupyter_core/utils/__init__.py @@ -151,18 +151,17 @@ def wrapped(*args: Any, **kwargs: Any) -> Any: name = threading.current_thread().name inner = coro(*args, **kwargs) try: - # If a loop is currently running in this thread, - # use a task runner. asyncio.get_running_loop() - if name not in _runner_map: - _runner_map[name] = _TaskRunner() - return _runner_map[name].run(inner) except RuntimeError: - pass - - # Run the loop for this thread. - loop = ensure_event_loop() - return loop.run_until_complete(inner) + # No loop running, run the loop for this thread. + loop = ensure_event_loop() + return loop.run_until_complete(inner) + + # Loop is currently running in this thread, + # use a task runner. + if name not in _runner_map: + _runner_map[name] = _TaskRunner() + return _runner_map[name].run(inner) wrapped.__doc__ = coro.__doc__ return wrapped diff --git a/tests/test_utils.py b/tests/test_utils.py index b3d485b..5fffd91 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -56,6 +56,24 @@ async def foo(): asyncio.run(foo()) + error_msg = "__foo__" + + async def error(): + raise RuntimeError(error_msg) + + error_sync = run_sync(error) + + def test_error_sync(): + with pytest.raises(RuntimeError, match=error_msg): + error_sync() + + test_error_sync() + + async def with_running_loop(): + test_error_sync() + + asyncio.run(with_running_loop()) + def test_ensure_async(): async def main():