diff --git a/CHANGELOG.md b/CHANGELOG.md index 90efd73680..c8d508200c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -38,6 +38,7 @@ * Fix `@ft.observable` not refreshing the UI after in-place changes such as `+=` on a list field ([#6293](https://github.com/flet-dev/flet/issues/6293), [#7086](https://github.com/flet-dev/flet/pull/7086)) by @joaoaracheski. * Fix web apps freezing every session while sending a large page update, such as a theme switch on a page with a big `DataTable`: the transport no longer turns each message into debug text when debug logging is off ([#7097](https://github.com/flet-dev/flet/pull/7097)) by @joaoaracheski. * Fix web apps serving FastAPI's `/docs`, `/redoc` and `/openapi.json`, which took those routes from the app and loaded Swagger UI and ReDoc from a CDN even with `no_cdn=True`: apps made by `flet_web.fastapi.app()` and `flet run --web` no longer register them ([#7111](https://github.com/flet-dev/flet/issues/7111), [#7112](https://github.com/flet-dev/flet/pull/7112)) by @joaoaracheski. +* Fix component state changed from a worker thread, such as by `set_state` in a `page.run_thread()` handler, not reaching the screen until something else woke the app, stopping all later updates of the session when asyncio debug mode is on, and possibly racing a re-render of the component ([#7108](https://github.com/flet-dev/flet/issues/7108), [#7113](https://github.com/flet-dev/flet/pull/7113)) by @joaoaracheski. * Fix `flet --version --json` printing invalid JSON when `flet-cli` is not installed yet: the on-the-fly install's progress messages now go to stderr ([flet-build-action#2](https://github.com/flet-dev/flet-build-action/issues/2), [#7099](https://github.com/flet-dev/flet/pull/7099)) by @ndonkoHenri. * Fix changes to a control's `theme`, `dark_theme` or `theme_mode`, such as toggling `Container.theme_mode`, not being applied until its parent was updated ([#7090](https://github.com/flet-dev/flet/pull/7090)) by @ndonkoHenri. * Fix `Screenshot.capture()` failing with a type error when `pixel_ratio` is a whole number, such as `pixel_ratio=1` ([#7090](https://github.com/flet-dev/flet/pull/7090)) by @ndonkoHenri. diff --git a/sdk/python/packages/flet/src/flet/components/hooks/use_state.py b/sdk/python/packages/flet/src/flet/components/hooks/use_state.py index d6b7b2a851..d61b4c060d 100644 --- a/sdk/python/packages/flet/src/flet/components/hooks/use_state.py +++ b/sdk/python/packages/flet/src/flet/components/hooks/use_state.py @@ -1,12 +1,16 @@ from __future__ import annotations +import asyncio +import contextlib +import contextvars from collections.abc import Callable -from dataclasses import dataclass +from dataclasses import dataclass, field from typing import Any, TypeVar, overload from flet.components.hooks.hook import Hook from flet.components.observable import Observable, ObservableSubscription from flet.components.utils import current_component +from flet.utils.event_loop import running_loop StateT = TypeVar("StateT") Updater = Callable[[StateT], StateT] @@ -44,6 +48,15 @@ class StateHook(Hook): hook-level updates even when consumers do not inspect `value` directly. """ + _loop: asyncio.AbstractEventLoop | None = field(default=None, repr=False) + """The event loop the component renders on, where `set_state` called from + another thread is applied. `None` when rendered without a running loop, as in + synchronous tests.""" + + _context: contextvars.Context | None = field(default=None, repr=False) + """A copy of the context of the render that created the hook, with the page + set, in which `set_state` called from another thread is applied.""" + @overload def use_state( @@ -74,12 +87,17 @@ def use_state( (value, set_value) tuple. """ component = current_component() - hook = component.use_hook( - lambda: StateHook( + + def create_hook() -> StateHook: + loop = running_loop() + return StateHook( component, initial() if callable(initial) else initial, + _loop=loop, + _context=contextvars.copy_context() if loop is not None else None, ) - ) + + hook = component.use_hook(create_hook) def update_subscription(h: StateHook): """ @@ -104,14 +122,8 @@ def update_subscription(h: StateHook): update_subscription(hook) - def set_state(new_value_or_fn: StateT | Updater): - """ - Update the state value. - - Can be called with either: - - a direct new value, or - - a function that takes the current value and returns the next one. - """ + def apply_state(new_value_or_fn: StateT | Updater): + """Applies a `set_state` call, on the event loop.""" # Compute next value new_value = ( new_value_or_fn(hook.value) @@ -127,4 +139,28 @@ def set_state(new_value_or_fn: StateT | Updater): if hook.component: hook.component._schedule_update() + def set_state(new_value_or_fn: StateT | Updater): + """ + Update the state value. + + Can be called with either: + - a direct new value, or + - a function that takes the current value and returns the next one. + + Safe to call from any thread: from a worker thread, such as a + `page.run_thread()` handler, the update is applied on the event loop, + in call order, so it can't race a render of the component. + """ + # `set_state` must not reference itself: the cycle would leave every + # render's setters to the garbage collector. + loop = hook._loop + if loop is not None and running_loop() is not loop: + # RuntimeError: the loop closed, so the app is gone. + with contextlib.suppress(RuntimeError): + loop.call_soon_threadsafe( + apply_state, new_value_or_fn, context=hook._context + ) + return + apply_state(new_value_or_fn) + return hook.value, set_state diff --git a/sdk/python/packages/flet/src/flet/messaging/session.py b/sdk/python/packages/flet/src/flet/messaging/session.py index 0c46646181..39aff4abea 100644 --- a/sdk/python/packages/flet/src/flet/messaging/session.py +++ b/sdk/python/packages/flet/src/flet/messaging/session.py @@ -1,4 +1,5 @@ import asyncio +import contextlib import dataclasses import inspect import logging @@ -6,6 +7,7 @@ import weakref from collections.abc import Awaitable, Callable from datetime import datetime, timedelta, timezone +from threading import Lock from typing import TYPE_CHECKING, Any, Optional from flet.controls.base_control import BaseControl @@ -22,6 +24,7 @@ ) from flet.messaging.session_store import SessionStore from flet.pubsub.pubsub_client import PubSubClient +from flet.utils.event_loop import running_loop from flet.utils.object_model import is_coroutine_result, patch_dataclass from flet.utils.strings import random_string @@ -58,6 +61,13 @@ def __init__(self, conn: Connection): self.__store: SessionStore = SessionStore() self.__pubsub_client = PubSubClient(conn.pubsubhub, self.__id) self.__method_calls: dict[str, asyncio.Future] = {} + self.__updates_lock = Lock() + """Guards the update and effect queues and the state that decides whether + they accept work (`conn`, `expires_at`, `closed`), which worker threads + read when they queue work.""" + self.__wakeup_pending = False + """Whether a worker thread posted a scheduler wakeup to the event loop that + hasn't run yet, so later workers don't post another one.""" self.__updates_ready: asyncio.Event = asyncio.Event() self.__pending_updates: set[BaseControl] = set() self.__pending_effects: list[ @@ -174,8 +184,9 @@ def attach_connection(self, conn: Connection) -> None: """ logger.debug("Connect session: %s", self.id) _context_page.set(self.__page) - self.__conn = conn - self.__expires_at = None + with self.__updates_lock: + self.__conn = conn + self.__expires_at = None for message in self.__send_buffer: self.__send_message(message) self.__send_buffer.clear() @@ -209,16 +220,17 @@ async def disconnect(self, session_timeout_seconds: int) -> None: considered expired. """ logger.debug("Disconnect session: %s", self.id) - self.__expires_at = datetime.now(timezone.utc) + timedelta( - seconds=session_timeout_seconds - ) + with self.__updates_lock: + self.__expires_at = datetime.now(timezone.utc) + timedelta( + seconds=session_timeout_seconds + ) + conn, self.__conn = self.__conn, None + discarded = self.__take_pending() + del discarded self.__send_buffer.clear() - self.__pending_updates.clear() - self.__pending_effects.clear() self.__updates_ready.clear() - if self.__conn: - self.__conn.dispose() - self.__conn = None + if conn: + conn.dispose() await self.dispatch_event(self.__page._i, "disconnect", None) def close(self): @@ -230,7 +242,10 @@ def close(self): a closure error, and dispatches the page-level `close` event. """ logger.debug("Closing expired session: %s", self.id) - self.__closed = True + with self.__updates_lock: + self.__closed = True + discarded = self.__take_pending() + del discarded self.__updates_ready.set() if self.__updates_task and not self.__updates_task.done(): self.__updates_task.cancel() @@ -717,41 +732,130 @@ def schedule_update(self, control: BaseControl): """ Queues a control for update by the background scheduler. + Safe to call from any thread, e.g. when state changes in a + :meth:`flet.Page.run_thread` worker. + Args: control: Control to update. """ logger.debug("Schedule_update(%s)", control) - if self.__conn is None and self.__expires_at is not None: - return - self.__pending_updates.add(control) - self.__updates_ready.set() + self.__enqueue(update=control) def schedule_effect(self, hook: "EffectHook", is_cleanup: bool): """ Queues an effect hook setup/cleanup operation for scheduler execution. + Safe to call from any thread. + Args: hook: Effect hook to process. is_cleanup: `True` to run cleanup, `False` to run setup. """ logger.debug("Schedule_effect(%s, %s)", hook, is_cleanup) - if self.__conn is None and self.__expires_at is not None: - return - # Hold a strong reference to the hook until it runs. A weakref would - # get cleared when the owning component unmounts and clears - # `_state.hooks` — dropping queued cleanup effects on the floor. - self.__pending_effects.append((hook, None if is_cleanup else hook.setup)) - self.__updates_ready.set() + # Keep the hook alive until cleanup and capture this render's setup. + self.__enqueue(effect=(hook, None if is_cleanup else hook.setup)) def start_updates_scheduler(self): """ Starts the deferred updates/effects scheduler task if not already running. + + Work queued on the event loop also starts it, so calling this is only + needed to start it before anything is queued. """ - logger.debug("Starting updates scheduler: %s", self.id) if self.__updates_task and not self.__updates_task.done(): return + logger.debug("Starting updates scheduler: %s", self.id) self.__updates_task = asyncio.create_task(self.__updates_scheduler()) + def __accepting_work(self) -> bool: + """ + Whether the session takes new updates and effects: it's neither closed nor + disconnected. Worker threads call it under the queue lock. + """ + return not self.__closed and self.__expires_at is None + + def __event_loop(self) -> Optional[asyncio.AbstractEventLoop]: + """ + The connection's event loop, called under the queue lock. `None` if the + session is disconnected or its connection has no loop. + """ + try: + return self.__conn.loop if self.__conn else None + except RuntimeError: + return None + + def __enqueue( + self, + update: Optional[BaseControl] = None, + effect: Optional[ + tuple["EffectHook", Callable[[], Any | Awaitable[Any]] | None] + ] = None, + ): + """ + Queues a control to update or an effect operation if the session accepts + work, then wakes the scheduler. + + On the event loop it wakes the scheduler right away, unless it's already + woken. A worker thread posts the wakeup to the loop instead, unless another + worker's wakeup is still on its way: that one drains this work too. + """ + running = running_loop() + with self.__updates_lock: + if not self.__accepting_work(): + return + loop = self.__event_loop() + from_worker = loop is not None and running is not loop + if from_worker and loop.is_closed(): + return # A worker outlived the app's event loop. + if effect is None: + self.__pending_updates.add(update) + else: + self.__pending_effects.append(effect) + if from_worker: + if self.__wakeup_pending: + return + self.__wakeup_pending = True + if not from_worker: + if not self.__updates_ready.is_set(): + self.__wake_scheduler() + return + # RuntimeError: the loop closed after the check above. + with contextlib.suppress(RuntimeError): + loop.call_soon_threadsafe(self.__on_posted_wakeup) + + def __on_posted_wakeup(self): + """Runs on the event loop the wakeup a worker thread posted.""" + with self.__updates_lock: + self.__wakeup_pending = False + self.__wake_scheduler() + + def __wake_scheduler(self): + """ + Wakes the scheduler, starting it if needed, unless the session no longer + accepts work. Runs on the event loop. + + Without a running loop, as in synchronous tests, only the wakeup is + recorded and the scheduler drains the queues once it starts. + """ + if not self.__accepting_work(): + return + if running_loop() is not None: + self.start_updates_scheduler() + self.__updates_ready.set() + + def __take_pending(self) -> tuple[set, list]: + """ + Empties the queues, called under the queue lock, and returns what they held. + + The caller drops the result after releasing the lock: releasing the last + reference to a control or hook can run a finalizer that queues work, which + takes the lock again. + """ + pending = (self.__pending_updates, self.__pending_effects) + self.__pending_updates = set() + self.__pending_effects = [] + return pending + async def __updates_scheduler(self): """ Background loop that drains queued updates and effect operations. @@ -766,15 +870,17 @@ async def __updates_scheduler(self): self.__updates_ready.clear() # Process pending updates - pending_updates = list(self.__pending_updates) - self.__pending_updates.clear() + with self.__updates_lock: + pending_updates = self.__pending_updates + self.__pending_updates = set() for control in pending_updates: control.update() # Process pending effects - pending_effects = list(self.__pending_effects) - self.__pending_effects.clear() + with self.__updates_lock: + pending_effects = self.__pending_effects + self.__pending_effects = [] for hook, setup in pending_effects: if hook: @@ -859,7 +965,7 @@ def done(task: asyncio.Task): self.__effect_cleanup_tasks.discard(task) hook._cleanup_task = None queued = hook.take_queued() - if self.__closed or (self.__conn is None and self.__expires_at is not None): + if not self.__accepting_work(): return for setup in queued: self.__run_effect(hook, setup) diff --git a/sdk/python/packages/flet/src/flet/utils/event_loop.py b/sdk/python/packages/flet/src/flet/utils/event_loop.py new file mode 100644 index 0000000000..3424078fba --- /dev/null +++ b/sdk/python/packages/flet/src/flet/utils/event_loop.py @@ -0,0 +1,9 @@ +import asyncio + +running_loop = asyncio._get_running_loop +""" +Returns the event loop running in the calling thread, or `None` if there's none. + +Unlike `asyncio.get_running_loop()`, it doesn't raise when there's none, which makes +it cheap enough for hot paths such as `set_state`. +""" diff --git a/sdk/python/packages/flet/tests/test_schedule_update_threads.py b/sdk/python/packages/flet/tests/test_schedule_update_threads.py new file mode 100644 index 0000000000..03f3855f0c --- /dev/null +++ b/sdk/python/packages/flet/tests/test_schedule_update_threads.py @@ -0,0 +1,425 @@ +"""Worker-thread scheduling and session lifecycle for component updates and effects.""" + +import asyncio +import threading +import time + +import pytest +import pytest_asyncio + +import flet as ft +from flet.components.component import Component +from flet.components.hooks.use_effect import EffectHook +from flet.controls.context import _context_page +from flet.messaging.connection import Connection +from flet.messaging.session import Session +from flet.pubsub.pubsub_hub import PubSubHub + + +class _RecordingConnection(Connection): + def __init__(self): + super().__init__() + self.messages = [] + + def send_message(self, message): + self.messages.append(message) + + +class _FakeControl: + """Counts the updates the scheduler makes.""" + + def __init__(self): + self.updates = 0 + + def update(self): + self.updates += 1 + + +def _create_connection() -> Connection: + conn = _RecordingConnection() + conn.pubsubhub = PubSubHub() + conn.loop = asyncio.get_running_loop() + return conn + + +def _create_session() -> Session: + return Session(_create_connection()) + + +def _run_in_threads(target, count): + errors = [] + + def run(i): + try: + target(i) + except Exception as e: # noqa: BLE001 - the test asserts there are none + errors.append(e) + + threads = [threading.Thread(target=run, args=(i,)) for i in range(count)] + for t in threads: + t.start() + for t in threads: + t.join() + return errors + + +async def _close(session: Session): + """Closes `session` and waits for the tasks closing it started, such as the + `close` event dispatch, so none is pending when the test's loop ends.""" + session.close() + current = asyncio.current_task() + await asyncio.gather( + *(t for t in asyncio.all_tasks() if t is not current), return_exceptions=True + ) + + +async def _wait_for(condition, timeout=2.0): + loop = asyncio.get_running_loop() + deadline = loop.time() + timeout + while not condition() and loop.time() < deadline: + await asyncio.sleep(0.01) + return condition() + + +@pytest_asyncio.fixture +async def session(): + loop = asyncio.get_running_loop() + # Debug mode makes asyncio raise on non-thread-safe calls from other threads. + debug = loop.get_debug() + loop.set_debug(True) + session = _create_session() + session.start_updates_scheduler() + await asyncio.sleep(0) # the scheduler is now waiting for work + yield session + await _close(session) + loop.set_debug(debug) + + +@pytest.mark.asyncio +async def test_update_scheduled_from_a_worker_thread_is_applied(session): + control = _FakeControl() + + errors = _run_in_threads(lambda i: session.schedule_update(control), 1) + + assert errors == [] + assert await _wait_for(lambda: control.updates == 1) + + +@pytest.mark.asyncio +async def test_update_from_a_worker_thread_wakes_an_idle_loop(): + """Without debug mode and with nothing else waking the loop (no polling, as in + an idle app), the update must still be applied right away.""" + session = _create_session() + session.start_updates_scheduler() + await asyncio.sleep(0) + applied = asyncio.Event() + + class _Control: + def update(self): + applied.set() + + def worker(): + time.sleep(0.1) # the loop is idle, waiting for `applied` + session.schedule_update(_Control()) + + thread = threading.Thread(target=worker) + thread.start() + try: + await asyncio.wait_for(applied.wait(), 2) + finally: + thread.join() + await _close(session) + + +@pytest.mark.asyncio +async def test_no_update_is_lost_when_many_threads_schedule_at_once(session): + controls = [_FakeControl() for _ in range(8 * 100)] + + def worker(i): + for control in controls[i * 100 : (i + 1) * 100]: + session.schedule_update(control) + + errors = await asyncio.to_thread(_run_in_threads, worker, 8) + + assert errors == [] + assert await _wait_for(lambda: all(c.updates >= 1 for c in controls)) + + +@pytest.mark.asyncio +async def test_effect_scheduled_from_a_worker_thread_runs(session, monkeypatch): + ran = [] + monkeypatch.setattr( + session, + "_Session__run_effect", + lambda hook, setup: ran.append((hook, setup)), + ) + setup = object() + + class _Hook: + pass + + hook = _Hook() + hook.setup = setup + + errors = _run_in_threads(lambda i: session.schedule_effect(hook, False), 1) + + assert errors == [] + assert await _wait_for(lambda: ran == [(hook, setup)]) + + +@pytest.mark.asyncio +async def test_update_on_the_loop_thread_is_queued_right_away(session): + """On the loop thread nothing is deferred: the control is queued at once.""" + control = _FakeControl() + + session.schedule_update(control) + + assert control in session._Session__pending_updates + + +def test_update_from_a_thread_after_the_loop_closed_is_ignored(): + """A worker that outlives the session's event loop must not crash, and its + update isn't queued, as nothing would ever drain it.""" + + async def start(): + session = _create_session() + session.start_updates_scheduler() + await asyncio.sleep(0) + return session + + # The session is left open: closing it needs the loop. Closing the loop + # cancels the scheduler, and nothing else is pending. + session = asyncio.run(start()) + + errors = _run_in_threads(lambda i: session.schedule_update(_FakeControl()), 1) + + assert errors == [] + assert not session._Session__pending_updates + + +@pytest.mark.asyncio +async def test_update_from_a_thread_after_close_is_not_queued(): + """After `close()` nobody drains the queues, so nothing must pile up in them.""" + session = _create_session() + session.start_updates_scheduler() + await asyncio.sleep(0) + await _close(session) + + errors = _run_in_threads(lambda i: session.schedule_update(_FakeControl()), 1) + await asyncio.sleep(0.05) + + assert errors == [] + assert not session._Session__pending_updates + + +@pytest.mark.asyncio +async def test_effect_from_a_thread_after_close_is_not_queued(): + session = _create_session() + session.start_updates_scheduler() + await asyncio.sleep(0) + await _close(session) + + class _Hook: + setup = None + + errors = _run_in_threads(lambda i: session.schedule_effect(_Hook(), False), 1) + await asyncio.sleep(0.05) + + assert errors == [] + assert not session._Session__pending_effects + + +@pytest.mark.asyncio +async def test_pending_worker_update_is_discarded_on_disconnect(session): + control = _FakeControl() + + # Join before yielding so the scheduler cannot process the update yet. + errors = _run_in_threads(lambda i: session.schedule_update(control), 1) + await session.disconnect(60) + await asyncio.sleep(0) + + assert errors == [] + assert control.updates == 0 + assert not session._Session__pending_updates + + +@pytest.mark.asyncio +async def test_worker_effect_precedes_later_loop_effect(session): + events = [] + component = Component(fn=lambda: None) + component._state.mounted = True + + def first_setup(): + events.append("first setup") + return lambda: events.append("first cleanup") + + hook = EffectHook(component, setup=first_setup) + component._state.hooks.append(hook) + assert _run_in_threads(lambda i: session.schedule_effect(hook, False), 1) == [] + hook.setup = lambda: events.append("second setup") + session.schedule_effect(hook, False) + + assert await _wait_for(lambda: "first setup" in events and "second setup" in events) + assert events == ["first setup", "first cleanup", "second setup"] + + +@pytest.mark.asyncio +async def test_reconnect_discards_pending_worker_updates_and_effects(session): + old_control, new_control = _FakeControl(), _FakeControl() + events = [] + component = Component(fn=lambda: None) + component._state.mounted = True + hook = EffectHook(component, setup=lambda: events.append("old")) + component._state.hooks.append(hook) + + def submit_old_work(i): + session.schedule_update(old_control) + session.schedule_effect(hook, False) + + assert _run_in_threads(submit_old_work, 1) == [] + await session.disconnect(60) + session.attach_connection(_create_connection()) + hook.setup = lambda: events.append("new") + session.schedule_effect(hook, False) + session.schedule_update(new_control) + + assert await _wait_for(lambda: new_control.updates == 1) + assert old_control.updates == 0 + assert events == ["new"] + + +@pytest.mark.asyncio +async def test_worker_submission_before_startup_leaves_wakeup_to_loop(monkeypatch): + session = _create_session() + control = _FakeControl() + owner = threading.current_thread() + ready = session._Session__updates_ready + set_ready = ready.set + + def set_on_loop(): + assert threading.current_thread() is owner + set_ready() + + monkeypatch.setattr(ready, "set", set_on_loop) + try: + assert _run_in_threads(lambda i: session.schedule_update(control), 1) == [] + session.start_updates_scheduler() + assert await _wait_for(lambda: control.updates == 1) + finally: + await _close(session) + + +@pytest.mark.asyncio +async def test_many_worker_updates_post_one_wakeup(session, monkeypatch): + """Updates queued while a wakeup is still on its way to the loop don't post + another one.""" + loop = asyncio.get_running_loop() + call_soon_threadsafe = loop.call_soon_threadsafe + wakeups = [] + + def counting(callback, *args, **kwargs): + if callback == session._Session__on_posted_wakeup: + wakeups.append(callback) + return call_soon_threadsafe(callback, *args, **kwargs) + + monkeypatch.setattr(loop, "call_soon_threadsafe", counting) + controls = [_FakeControl() for _ in range(1000)] + + # The loop doesn't run while the workers do, so the first wakeup stays pending. + def worker(i): + for control in controls[i * 250 : (i + 1) * 250]: + session.schedule_update(control) + + assert _run_in_threads(worker, 4) == [] + + assert len(wakeups) == 1 + assert await _wait_for(lambda: all(c.updates == 1 for c in controls)) + + # Once that wakeup ran, the next worker update posts a new one. + assert _run_in_threads(lambda i: session.schedule_update(controls[0]), 1) == [] + assert len(wakeups) == 2 + assert await _wait_for(lambda: controls[0].updates == 2) + + +@pytest.mark.asyncio +async def test_update_starts_the_scheduler_when_nothing_started_it(): + """An app that doesn't render components never starts the scheduler, so + queuing work starts it, from the loop or from a worker thread.""" + session = _create_session() + on_loop, from_worker = _FakeControl(), _FakeControl() + try: + session.schedule_update(on_loop) + assert await _wait_for(lambda: on_loop.updates == 1) + + session._Session__updates_task.cancel() + await asyncio.sleep(0) + assert _run_in_threads(lambda i: session.schedule_update(from_worker), 1) == [] + assert await _wait_for(lambda: from_worker.updates == 1) + finally: + await _close(session) + + +@pytest.mark.asyncio +@pytest.mark.parametrize("close", [True, False]) +async def test_finalizer_queuing_work_while_the_queues_are_dropped(session, close): + """Dropping the queues on close or disconnect can release the last reference + to a control whose finalizer queues work. That must not deadlock on the queue + lock, and the work is dropped.""" + finalized = [] + + class _Finalizing(_FakeControl): + def __del__(self): + session.schedule_update(_FakeControl()) + finalized.append(True) + + session._Session__pending_updates.add(_Finalizing()) + if close: + session.close() + else: + await session.disconnect(60) + + assert finalized == [True] + assert not session._Session__pending_updates + + +@pytest.mark.asyncio +async def test_set_state_from_worker_threads_is_applied_on_the_loop(): + """`set_state` called from worker threads applies every change on the event + loop, so it can't race a render of the component.""" + session = _create_session() + token = _context_page.set(session.page) + loop_thread = threading.current_thread() + updater_threads = set() + setters = [] + + @ft.component + def App(): + count, set_count = ft.use_state(0) + setters.append(set_count) + return ft.Text(f"count {count}") + + def increment(count): + updater_threads.add(threading.current_thread()) + return count + 1 + + def worker(i): + for _ in range(100): + setters[-1](increment) + + try: + session.page.render(App) + session.get_page_patch() + + errors = await asyncio.to_thread(_run_in_threads, worker, 4) + + assert errors == [] + assert await _wait_for( + lambda: any( + isinstance(c, ft.Text) and c.value == "count 400" + for c in session.index.values() + ) + ) + assert updater_threads == {loop_thread} + finally: + _context_page.reset(token) + await _close(session)