Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions jupyter_core/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
Loading