Skip to content
Closed
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
15 changes: 12 additions & 3 deletions src/prompt_toolkit/eventloop/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def call_soon_threadsafe(
func: Callable[[], None],
max_postpone_time: float | None = None,
loop: asyncio.AbstractEventLoop | None = None,
) -> None:
) -> asyncio.Handle:
"""
Wrapper around asyncio's `call_soon_threadsafe`.

Expand All @@ -55,17 +55,25 @@ def call_soon_threadsafe(
process output.)
However, we want to set a deadline value, for when the rendering should
happen. (The UI should stay responsive).

Return a handle whose ``cancel()`` method prevents the callback from running,
including after it has been postponed. Cancellation cannot stop a callback
that has already started.
"""
loop2 = loop or get_running_loop()

# If no `max_postpone_time` has been given, schedule right now.
if max_postpone_time is None:
loop2.call_soon_threadsafe(func)
return
return loop2.call_soon_threadsafe(func)

max_postpone_until = time.time() + max_postpone_time
handle = asyncio.Handle(func, (), loop2)

def schedule() -> None:
# The original handle remains the cancellation token across retries.
if handle.cancelled():
return

# When there are no other tasks scheduled in the event loop. Run it
# now.
# Notice: uvloop doesn't have this _ready attribute. In that case,
Expand All @@ -83,6 +91,7 @@ def schedule() -> None:
loop2.call_soon_threadsafe(schedule)

loop2.call_soon_threadsafe(schedule)
return handle


def get_traceback_from_context(context: dict[str, Any]) -> TracebackType | None:
Expand Down
158 changes: 158 additions & 0 deletions tests/test_eventloop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
from __future__ import annotations

import asyncio
from collections import deque
from unittest.mock import Mock, patch

import pytest

from prompt_toolkit.eventloop.utils import call_soon_threadsafe


@pytest.mark.parametrize("postpone", [None, 1.0])
def test_call_soon_threadsafe_returns_cancellable_handle(postpone):
loop = asyncio.new_event_loop()
callback = Mock()
try:
handle = call_soon_threadsafe(callback, max_postpone_time=postpone, loop=loop)
assert isinstance(handle, asyncio.Handle)
handle.cancel()
loop.run_until_complete(asyncio.sleep(0))
callback.assert_not_called()
finally:
loop.close()


def test_cancel_after_postponement():
loop = Mock(spec=asyncio.AbstractEventLoop)
loop.get_debug.return_value = False
pending = deque()
loop.call_soon_threadsafe.side_effect = pending.append
loop._ready = [object()]
callback = Mock()
with patch("prompt_toolkit.eventloop.utils.time.time", return_value=0):
handle = call_soon_threadsafe(callback, max_postpone_time=1, loop=loop)
pending.popleft()()
callback.assert_not_called()
assert len(pending) == 1
assert isinstance(handle, asyncio.Handle)
handle.cancel()
pending.popleft()()
callback.assert_not_called()
assert not pending


@pytest.mark.parametrize("ready,now", [([], 0), ([object()], 2)])
def test_postponed_callback_runs_once(ready, now):
loop = Mock(spec=asyncio.AbstractEventLoop)
loop.get_debug.return_value = False
pending = deque()
loop.call_soon_threadsafe.side_effect = pending.append
loop._ready = ready
callback = Mock()
with patch("prompt_toolkit.eventloop.utils.time.time", return_value=0) as clock:
call_soon_threadsafe(callback, max_postpone_time=1, loop=loop)
clock.return_value = now
pending.popleft()()
callback.assert_called_once_with()
assert not pending


def test_loop_without_ready_attribute():
loop = Mock(spec=asyncio.AbstractEventLoop)
loop.get_debug.return_value = False
pending = deque()
loop.call_soon_threadsafe.side_effect = pending.append
callback = Mock()
call_soon_threadsafe(callback, max_postpone_time=1, loop=loop)
pending.popleft()()
callback.assert_called_once_with()
assert not pending


@pytest.mark.parametrize("postpone", [None, 1.0])
def test_callback_runs_in_scheduling_context(postpone):
from contextvars import ContextVar

value = ContextVar("test_value", default="outside")
loop = asyncio.new_event_loop()
seen = []
done = loop.create_future()

def callback():
seen.append(value.get())
done.set_result(None)

token = value.set("scheduled")
try:
call_soon_threadsafe(callback, max_postpone_time=postpone, loop=loop)
value.reset(token)
loop.run_until_complete(asyncio.sleep(0))
loop.run_until_complete(done)
assert seen == ["scheduled"]
finally:
loop.close()


@pytest.mark.parametrize("postpone", [None, 1.0])
def test_callback_exception_uses_loop_handler(postpone):
loop = asyncio.new_event_loop()
error = ValueError("callback error")
contexts = []
done = loop.create_future()

def handle_exception(loop, context):
contexts.append(context)
done.set_result(None)

loop.set_exception_handler(handle_exception)

def callback():
raise error

try:
call_soon_threadsafe(callback, max_postpone_time=postpone, loop=loop)
loop.run_until_complete(asyncio.sleep(0))
loop.run_until_complete(done)
assert len(contexts) == 1
assert contexts[0]["exception"] is error
finally:
loop.close()


@pytest.mark.parametrize("postpone", [None, 1.0])
def test_cancel_from_scheduling_thread(postpone):
from threading import Thread

loop = asyncio.new_event_loop()
callback = Mock()
errors = []

def schedule_and_cancel():
try:
handle = call_soon_threadsafe(
callback, max_postpone_time=postpone, loop=loop
)
handle.cancel()
except BaseException as exc:
errors.append(exc)

try:
thread = Thread(target=schedule_and_cancel)
thread.start()
thread.join()
assert errors == []
loop.run_until_complete(asyncio.sleep(0))
callback.assert_not_called()
finally:
loop.close()


def test_uses_running_loop_when_not_specified():
async def run():
done = asyncio.get_running_loop().create_future()
handle = call_soon_threadsafe(lambda: done.set_result(None))
assert isinstance(handle, asyncio.Handle)
await done

asyncio.run(run())
Loading