From eeecdf8deb59ab4c0f68166c219070122219ae8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Aracheski?= <21148235+joaoaracheski@users.noreply.github.com> Date: Fri, 20 Nov 2026 13:59:52 -0300 Subject: [PATCH 1/5] fix: make Session.schedule_update() and schedule_effect() thread-safe Both changed the pending queues and called asyncio.Event.set() on the calling thread. From a worker thread (e.g. state changed in a page.run_thread() handler) the set() doesn't wake an idle loop, so the update waited for something else to wake it, and in asyncio debug mode it raised and left the scheduler stuck for the rest of the session. Calls from other threads are now handed to the scheduler's loop with call_soon_threadsafe(), so the queues are only touched on the loop and no lock is needed. Fixes #7108 --- CHANGELOG.md | 1 + .../flet/src/flet/messaging/session.py | 51 ++++- .../tests/test_schedule_update_threads.py | 182 ++++++++++++++++++ 3 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 sdk/python/packages/flet/tests/test_schedule_update_threads.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 9629ab8bcb..1ed5c62aba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ * Fix `ShakeDetector` never calling `on_shake` with its default settings, and missing most shakes because it sampled the accelerometer only every 200 ms; on desktop it now raises `FletUnsupportedPlatformException`, like `Accelerometer` ([#6456](https://github.com/flet-dev/flet/issues/6456), [#7093](https://github.com/flet-dev/flet/pull/7093)) by @ndonkoHenri. * 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 component updates and effects scheduled from a worker thread, such as state changed in a `page.run_thread()` handler, not reaching the screen until something else woke the event loop, and stopping all later updates of the session when asyncio debug mode is on: `Session.schedule_update()` and `schedule_effect()` now hand calls from other threads to the event loop ([#7108](https://github.com/flet-dev/flet/issues/7108), [#PR](https://github.com/flet-dev/flet/pull/PR)) 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/messaging/session.py b/sdk/python/packages/flet/src/flet/messaging/session.py index 417941c8de..fe6b932ecd 100644 --- a/sdk/python/packages/flet/src/flet/messaging/session.py +++ b/sdk/python/packages/flet/src/flet/messaging/session.py @@ -36,6 +36,14 @@ __all__ = ["Session"] +def _running_loop() -> Optional[asyncio.AbstractEventLoop]: + """The event loop running in the calling thread, or `None` if there's none.""" + try: + return asyncio.get_running_loop() + except RuntimeError: + return None + + class Session: """ Represents a server-side Flet session. @@ -67,6 +75,9 @@ def __init__(self, conn: Connection): """Tasks awaiting `async` effect cleanups, held until they finish so they aren't garbage collected while pending.""" self.__updates_task: Optional[asyncio.Task] = None + self.__updates_loop: Optional[asyncio.AbstractEventLoop] = None + """Loop the updates scheduler runs on: the only thread allowed to touch the + pending queues and `__updates_ready`.""" self.__closed = False # Whether this session's app renders via the declarative components # API (page.render/render_views). Tracked per-session — not on the @@ -717,12 +728,18 @@ 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.__call_on_updates_loop(self.__queue_update, control) + + def __queue_update(self, control: BaseControl): self.__pending_updates.add(control) self.__updates_ready.set() @@ -730,6 +747,8 @@ 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. @@ -740,9 +759,38 @@ def schedule_effect(self, hook: "EffectHook", is_cleanup: bool): # 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.__call_on_updates_loop( + self.__queue_effect, hook, None if is_cleanup else hook.setup + ) + + def __queue_effect( + self, + hook: "EffectHook", + setup: Callable[[], Any | Awaitable[Any]] | None, + ): + self.__pending_effects.append((hook, setup)) self.__updates_ready.set() + def __call_on_updates_loop(self, callback: Callable[..., None], *args: Any): + """ + Runs `callback` on the updates scheduler's loop. + + The pending queues and `asyncio.Event` aren't thread-safe: `Event.set()` + from another thread may not wake the loop, and a queue changed there can + race with the scheduler draining it. So a call from another thread is + handed to the loop with `call_soon_threadsafe()`; on the loop thread, or + before the scheduler has started, `callback` runs at once. + """ + loop = self.__updates_loop + if loop is None or _running_loop() is loop: + callback(*args) + return + try: + loop.call_soon_threadsafe(callback, *args) + except RuntimeError: + # The loop is closed: the session is gone, so there's nothing to update. + logger.debug("Updates loop is closed, dropping %s%s", callback, args) + def start_updates_scheduler(self): """ Starts the deferred updates/effects scheduler task if not already running. @@ -750,6 +798,7 @@ def start_updates_scheduler(self): logger.debug("Starting updates scheduler: %s", self.id) if self.__updates_task and not self.__updates_task.done(): return + self.__updates_loop = asyncio.get_running_loop() self.__updates_task = asyncio.create_task(self.__updates_scheduler()) async def __updates_scheduler(self): 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..03f864ff09 --- /dev/null +++ b/sdk/python/packages/flet/tests/test_schedule_update_threads.py @@ -0,0 +1,182 @@ +"""`Session.schedule_update()` and `schedule_effect()` called from worker threads. + +The pending queues and the `asyncio.Event` that wakes the updates scheduler are only +safe on the event loop thread, so calls from other threads (e.g. state changed in a +`page.run_thread()` worker) must be handed to the loop. +""" + +import asyncio +import threading +import time + +import pytest +import pytest_asyncio + +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_session() -> Session: + conn = _RecordingConnection() + conn.pubsubhub = PubSubHub() + conn.loop = asyncio.get_running_loop() + return Session(conn) + + +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 _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 + session.close() + await asyncio.sleep(0) + 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() + session.close() + await asyncio.sleep(0) + + +@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 = _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.""" + + async def start(): + session = _create_session() + session.start_updates_scheduler() + await asyncio.sleep(0) + return session + + session = asyncio.run(start()) # the loop is closed when this returns + + errors = _run_in_threads(lambda i: session.schedule_update(_FakeControl()), 1) + + assert errors == [] From fd0903df8f7f61f0553d68085f1ebfee32330dc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Aracheski?= <21148235+joaoaracheski@users.noreply.github.com> Date: Fri, 20 Nov 2026 14:00:26 -0300 Subject: [PATCH 2/5] Changelog: PR number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ed5c62aba..6d4842e0ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ * Fix `ShakeDetector` never calling `on_shake` with its default settings, and missing most shakes because it sampled the accelerometer only every 200 ms; on desktop it now raises `FletUnsupportedPlatformException`, like `Accelerometer` ([#6456](https://github.com/flet-dev/flet/issues/6456), [#7093](https://github.com/flet-dev/flet/pull/7093)) by @ndonkoHenri. * 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 component updates and effects scheduled from a worker thread, such as state changed in a `page.run_thread()` handler, not reaching the screen until something else woke the event loop, and stopping all later updates of the session when asyncio debug mode is on: `Session.schedule_update()` and `schedule_effect()` now hand calls from other threads to the event loop ([#7108](https://github.com/flet-dev/flet/issues/7108), [#PR](https://github.com/flet-dev/flet/pull/PR)) by @joaoaracheski. +* Fix component updates and effects scheduled from a worker thread, such as state changed in a `page.run_thread()` handler, not reaching the screen until something else woke the event loop, and stopping all later updates of the session when asyncio debug mode is on: `Session.schedule_update()` and `schedule_effect()` now hand calls from other threads to the event loop ([#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. From b490b705a72e8e334eea3a67117d19d3c6ef97dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Aracheski?= <21148235+joaoaracheski@users.noreply.github.com> Date: Sat, 21 Nov 2026 12:23:12 -0300 Subject: [PATCH 3/5] Drop updates queued from other threads after the session closes or disconnects Co-authored-by: daneeller <283215001+daneeller@users.noreply.github.com> --- .../flet/src/flet/messaging/session.py | 21 +++++++- .../tests/test_schedule_update_threads.py | 51 +++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/sdk/python/packages/flet/src/flet/messaging/session.py b/sdk/python/packages/flet/src/flet/messaging/session.py index fe6b932ecd..1ee9fa335e 100644 --- a/sdk/python/packages/flet/src/flet/messaging/session.py +++ b/sdk/python/packages/flet/src/flet/messaging/session.py @@ -735,11 +735,25 @@ def schedule_update(self, control: BaseControl): control: Control to update. """ logger.debug("Schedule_update(%s)", control) - if self.__conn is None and self.__expires_at is not None: + if not self.__accepts_updates(): return self.__call_on_updates_loop(self.__queue_update, control) + def __accepts_updates(self) -> bool: + """ + Whether queued updates and effects will still be processed: `False` once the + session is closed, or while it is disconnected. + """ + return not self.__closed and not ( + self.__conn is None and self.__expires_at is not None + ) + def __queue_update(self, control: BaseControl): + # Checked again here, on the loop thread: the session may have been closed + # or disconnected after a worker thread passed the check in + # `schedule_update()`, and the queue must stay empty then. + if not self.__accepts_updates(): + return self.__pending_updates.add(control) self.__updates_ready.set() @@ -754,7 +768,7 @@ def schedule_effect(self, hook: "EffectHook", is_cleanup: bool): 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: + if not self.__accepts_updates(): return # Hold a strong reference to the hook until it runs. A weakref would # get cleared when the owning component unmounts and clears @@ -768,6 +782,9 @@ def __queue_effect( hook: "EffectHook", setup: Callable[[], Any | Awaitable[Any]] | None, ): + # See `__queue_update()`. + if not self.__accepts_updates(): + return self.__pending_effects.append((hook, setup)) self.__updates_ready.set() diff --git a/sdk/python/packages/flet/tests/test_schedule_update_threads.py b/sdk/python/packages/flet/tests/test_schedule_update_threads.py index 03f864ff09..d12d6423e9 100644 --- a/sdk/python/packages/flet/tests/test_schedule_update_threads.py +++ b/sdk/python/packages/flet/tests/test_schedule_update_threads.py @@ -180,3 +180,54 @@ async def start(): errors = _run_in_threads(lambda i: session.schedule_update(_FakeControl()), 1) assert errors == [] + + +@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) + session.close() + await asyncio.sleep(0) + + 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) + session.close() + await asyncio.sleep(0) + + 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_update_handed_to_the_loop_before_disconnect_is_dropped(session): + """A worker passes the check in `schedule_update()` and hands the update to the + loop, but `disconnect()` runs first and clears the queues: the update must not + put the control back and get applied on a disconnected session.""" + control = _FakeControl() + + # The thread is joined without yielding, so the update waits on the loop. + errors = _run_in_threads(lambda i: session.schedule_update(control), 1) + await session.disconnect(60) + await asyncio.sleep(0.05) + + assert errors == [] + assert control.updates == 0 + assert not session._Session__pending_updates From b659afed5d837f303c25880a41991842baf17fd0 Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Sat, 21 Nov 2026 18:36:11 -0700 Subject: [PATCH 4/5] Fix scheduler races across worker submissions and session transitions --- CHANGELOG.md | 2 +- .../flet/src/flet/messaging/session.py | 120 +++++++----------- .../tests/test_schedule_update_threads.py | 95 +++++++++++--- 3 files changed, 129 insertions(+), 88 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d4842e0ed..380e2361fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ * Fix `ShakeDetector` never calling `on_shake` with its default settings, and missing most shakes because it sampled the accelerometer only every 200 ms; on desktop it now raises `FletUnsupportedPlatformException`, like `Accelerometer` ([#6456](https://github.com/flet-dev/flet/issues/6456), [#7093](https://github.com/flet-dev/flet/pull/7093)) by @ndonkoHenri. * 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 component updates and effects scheduled from a worker thread, such as state changed in a `page.run_thread()` handler, not reaching the screen until something else woke the event loop, and stopping all later updates of the session when asyncio debug mode is on: `Session.schedule_update()` and `schedule_effect()` now hand calls from other threads to the event loop ([#7108](https://github.com/flet-dev/flet/issues/7108), [#7113](https://github.com/flet-dev/flet/pull/7113)) by @joaoaracheski. +* Fix component updates and effects scheduled from a worker thread, such as state changed in a `page.run_thread()` handler, not reaching the screen until something else woke the event loop, and stopping all later updates of the session when asyncio debug mode is on: `Session.schedule_update()` and `schedule_effect()` now wake the event loop when a worker queues work ([#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/messaging/session.py b/sdk/python/packages/flet/src/flet/messaging/session.py index 1ee9fa335e..a743f07113 100644 --- a/sdk/python/packages/flet/src/flet/messaging/session.py +++ b/sdk/python/packages/flet/src/flet/messaging/session.py @@ -6,6 +6,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 @@ -66,6 +67,7 @@ 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() self.__updates_ready: asyncio.Event = asyncio.Event() self.__pending_updates: set[BaseControl] = set() self.__pending_effects: list[ @@ -76,8 +78,6 @@ def __init__(self, conn: Connection): aren't garbage collected while pending.""" self.__updates_task: Optional[asyncio.Task] = None self.__updates_loop: Optional[asyncio.AbstractEventLoop] = None - """Loop the updates scheduler runs on: the only thread allowed to touch the - pending queues and `__updates_ready`.""" self.__closed = False # Whether this session's app renders via the declarative components # API (page.render/render_views). Tracked per-session — not on the @@ -185,8 +185,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() @@ -220,12 +221,13 @@ 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 + ) + self.__pending_updates.clear() + self.__pending_effects.clear() self.__send_buffer.clear() - self.__pending_updates.clear() - self.__pending_effects.clear() self.__updates_ready.clear() if self.__conn: self.__conn.dispose() @@ -241,7 +243,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 + self.__pending_updates.clear() + self.__pending_effects.clear() self.__updates_ready.set() if self.__updates_task and not self.__updates_task.done(): self.__updates_task.cancel() @@ -735,27 +740,9 @@ def schedule_update(self, control: BaseControl): control: Control to update. """ logger.debug("Schedule_update(%s)", control) - if not self.__accepts_updates(): - return - self.__call_on_updates_loop(self.__queue_update, control) - - def __accepts_updates(self) -> bool: - """ - Whether queued updates and effects will still be processed: `False` once the - session is closed, or while it is disconnected. - """ - return not self.__closed and not ( - self.__conn is None and self.__expires_at is not None - ) - - def __queue_update(self, control: BaseControl): - # Checked again here, on the loop thread: the session may have been closed - # or disconnected after a worker thread passed the check in - # `schedule_update()`, and the queue must stay empty then. - if not self.__accepts_updates(): - return - self.__pending_updates.add(control) - self.__updates_ready.set() + with self.__updates_lock: + if self.__request_scheduler_wakeup(): + self.__pending_updates.add(control) def schedule_effect(self, hook: "EffectHook", is_cleanup: bool): """ @@ -768,45 +755,29 @@ def schedule_effect(self, hook: "EffectHook", is_cleanup: bool): is_cleanup: `True` to run cleanup, `False` to run setup. """ logger.debug("Schedule_effect(%s, %s)", hook, is_cleanup) - if not self.__accepts_updates(): - 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.__call_on_updates_loop( - self.__queue_effect, hook, None if is_cleanup else hook.setup - ) + with self.__updates_lock: + if self.__request_scheduler_wakeup(): + # Keep the hook alive until cleanup and capture this render's setup. + setup = None if is_cleanup else hook.setup + self.__pending_effects.append((hook, setup)) - def __queue_effect( - self, - hook: "EffectHook", - setup: Callable[[], Any | Awaitable[Any]] | None, - ): - # See `__queue_update()`. - if not self.__accepts_updates(): - return - self.__pending_effects.append((hook, setup)) - self.__updates_ready.set() - - def __call_on_updates_loop(self, callback: Callable[..., None], *args: Any): - """ - Runs `callback` on the updates scheduler's loop. - - The pending queues and `asyncio.Event` aren't thread-safe: `Event.set()` - from another thread may not wake the loop, and a queue changed there can - race with the scheduler draining it. So a call from another thread is - handed to the loop with `call_soon_threadsafe()`; on the loop thread, or - before the scheduler has started, `callback` runs at once. + def __request_scheduler_wakeup(self) -> bool: + """Called under the queue lock. Only the wakeup crosses threads, so a + delayed callback cannot reorder effects or restore work after disconnect. """ + if self.__closed or self.__expires_at is not None: + return False loop = self.__updates_loop - if loop is None or _running_loop() is loop: - callback(*args) - return - try: - loop.call_soon_threadsafe(callback, *args) - except RuntimeError: - # The loop is closed: the session is gone, so there's nothing to update. - logger.debug("Updates loop is closed, dropping %s%s", callback, args) + if loop is None: + return True # Startup will wake the scheduler for pending work. + if _running_loop() is loop: + self.__updates_ready.set() + else: + try: + loop.call_soon_threadsafe(self.__updates_ready.set) + except RuntimeError: + return False # A worker outlived the event loop. + return True def start_updates_scheduler(self): """ @@ -815,7 +786,10 @@ def start_updates_scheduler(self): logger.debug("Starting updates scheduler: %s", self.id) if self.__updates_task and not self.__updates_task.done(): return - self.__updates_loop = asyncio.get_running_loop() + with self.__updates_lock: + self.__updates_loop = asyncio.get_running_loop() + if self.__pending_updates or self.__pending_effects: + self.__updates_ready.set() self.__updates_task = asyncio.create_task(self.__updates_scheduler()) async def __updates_scheduler(self): @@ -832,15 +806,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: diff --git a/sdk/python/packages/flet/tests/test_schedule_update_threads.py b/sdk/python/packages/flet/tests/test_schedule_update_threads.py index d12d6423e9..851bc8dff4 100644 --- a/sdk/python/packages/flet/tests/test_schedule_update_threads.py +++ b/sdk/python/packages/flet/tests/test_schedule_update_threads.py @@ -1,9 +1,4 @@ -"""`Session.schedule_update()` and `schedule_effect()` called from worker threads. - -The pending queues and the `asyncio.Event` that wakes the updates scheduler are only -safe on the event loop thread, so calls from other threads (e.g. state changed in a -`page.run_thread()` worker) must be handed to the loop. -""" +"""Worker-thread scheduling and session lifecycle for component updates and effects.""" import asyncio import threading @@ -12,6 +7,8 @@ import pytest import pytest_asyncio +from flet.components.component import Component +from flet.components.hooks.use_effect import EffectHook from flet.messaging.connection import Connection from flet.messaging.session import Session from flet.pubsub.pubsub_hub import PubSubHub @@ -36,11 +33,15 @@ def update(self): self.updates += 1 -def _create_session() -> Session: +def _create_connection() -> Connection: conn = _RecordingConnection() conn.pubsubhub = PubSubHub() conn.loop = asyncio.get_running_loop() - return Session(conn) + return conn + + +def _create_session() -> Session: + return Session(_create_connection()) def _run_in_threads(target, count): @@ -128,7 +129,7 @@ def worker(i): for control in controls[i * 100 : (i + 1) * 100]: session.schedule_update(control) - errors = _run_in_threads(worker, 8) + 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)) @@ -217,17 +218,81 @@ class _Hook: @pytest.mark.asyncio -async def test_update_handed_to_the_loop_before_disconnect_is_dropped(session): - """A worker passes the check in `schedule_update()` and hands the update to the - loop, but `disconnect()` runs first and clears the queues: the update must not - put the control back and get applied on a disconnected session.""" +async def test_pending_worker_update_is_discarded_on_disconnect(session): control = _FakeControl() - # The thread is joined without yielding, so the update waits on the loop. + # 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.05) + 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: + session.close() + await asyncio.sleep(0) From a6f7dff01035748df0442463156ab121ecffc20d Mon Sep 17 00:00:00 2001 From: Feodor Fitsner Date: Sat, 21 Nov 2026 19:26:40 -0700 Subject: [PATCH 5/5] Apply set_state from worker threads on the event loop and coalesce wakeups - set_state called off the event loop posts the change to the loop, in the render's context, so it can't race a render of the component. - Worker threads post at most one scheduler wakeup at a time, outside the queue lock. - Queuing work on the loop starts the scheduler, so apps that don't render components don't keep queued updates forever. - One __accepting_work() check for queuing and for effects waiting on async cleanups; the queue lock now also covers conn on disconnect. - Use the connection's loop instead of a separate __updates_loop. - close() and disconnect() drop the queued work after releasing the lock, so a finalizer that queues work can't deadlock. - Tests: wait for the close task in teardown; cover coalesced wakeups, scheduler start, finalizers and set_state from worker threads. - Changelog entry from the app developer's perspective. --- CHANGELOG.md | 2 +- .../src/flet/components/hooks/use_state.py | 60 +++++-- .../flet/src/flet/messaging/session.py | 160 ++++++++++++------ .../flet/src/flet/utils/event_loop.py | 9 + .../tests/test_schedule_update_threads.py | 149 ++++++++++++++-- 5 files changed, 308 insertions(+), 72 deletions(-) create mode 100644 sdk/python/packages/flet/src/flet/utils/event_loop.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 380e2361fa..9811789527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ * Fix `ShakeDetector` never calling `on_shake` with its default settings, and missing most shakes because it sampled the accelerometer only every 200 ms; on desktop it now raises `FletUnsupportedPlatformException`, like `Accelerometer` ([#6456](https://github.com/flet-dev/flet/issues/6456), [#7093](https://github.com/flet-dev/flet/pull/7093)) by @ndonkoHenri. * 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 component updates and effects scheduled from a worker thread, such as state changed in a `page.run_thread()` handler, not reaching the screen until something else woke the event loop, and stopping all later updates of the session when asyncio debug mode is on: `Session.schedule_update()` and `schedule_effect()` now wake the event loop when a worker queues work ([#7108](https://github.com/flet-dev/flet/issues/7108), [#7113](https://github.com/flet-dev/flet/pull/7113)) 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 a743f07113..c5cc6b7242 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 @@ -23,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 patch_dataclass from flet.utils.strings import random_string @@ -37,14 +39,6 @@ __all__ = ["Session"] -def _running_loop() -> Optional[asyncio.AbstractEventLoop]: - """The event loop running in the calling thread, or `None` if there's none.""" - try: - return asyncio.get_running_loop() - except RuntimeError: - return None - - class Session: """ Represents a server-side Flet session. @@ -68,6 +62,12 @@ def __init__(self, conn: Connection): 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[ @@ -77,7 +77,6 @@ def __init__(self, conn: Connection): """Tasks awaiting `async` effect cleanups, held until they finish so they aren't garbage collected while pending.""" self.__updates_task: Optional[asyncio.Task] = None - self.__updates_loop: Optional[asyncio.AbstractEventLoop] = None self.__closed = False # Whether this session's app renders via the declarative components # API (page.render/render_views). Tracked per-session — not on the @@ -225,13 +224,13 @@ async def disconnect(self, session_timeout_seconds: int) -> None: self.__expires_at = datetime.now(timezone.utc) + timedelta( seconds=session_timeout_seconds ) - self.__pending_updates.clear() - self.__pending_effects.clear() + conn, self.__conn = self.__conn, None + discarded = self.__take_pending() + del discarded self.__send_buffer.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): @@ -245,8 +244,8 @@ def close(self): logger.debug("Closing expired session: %s", self.id) with self.__updates_lock: self.__closed = True - self.__pending_updates.clear() - self.__pending_effects.clear() + discarded = self.__take_pending() + del discarded self.__updates_ready.set() if self.__updates_task and not self.__updates_task.done(): self.__updates_task.cancel() @@ -740,9 +739,7 @@ def schedule_update(self, control: BaseControl): control: Control to update. """ logger.debug("Schedule_update(%s)", control) - with self.__updates_lock: - if self.__request_scheduler_wakeup(): - self.__pending_updates.add(control) + self.__enqueue(update=control) def schedule_effect(self, hook: "EffectHook", is_cleanup: bool): """ @@ -755,43 +752,110 @@ def schedule_effect(self, hook: "EffectHook", is_cleanup: bool): is_cleanup: `True` to run cleanup, `False` to run setup. """ logger.debug("Schedule_effect(%s, %s)", hook, is_cleanup) - with self.__updates_lock: - if self.__request_scheduler_wakeup(): - # Keep the hook alive until cleanup and capture this render's setup. - setup = None if is_cleanup else hook.setup - self.__pending_effects.append((hook, setup)) - - def __request_scheduler_wakeup(self) -> bool: - """Called under the queue lock. Only the wakeup crosses threads, so a - delayed callback cannot reorder effects or restore work after disconnect. - """ - if self.__closed or self.__expires_at is not None: - return False - loop = self.__updates_loop - if loop is None: - return True # Startup will wake the scheduler for pending work. - if _running_loop() is loop: - self.__updates_ready.set() - else: - try: - loop.call_soon_threadsafe(self.__updates_ready.set) - except RuntimeError: - return False # A worker outlived the event loop. - return True + # 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 - with self.__updates_lock: - self.__updates_loop = asyncio.get_running_loop() - if self.__pending_updates or self.__pending_effects: - self.__updates_ready.set() + 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. @@ -897,7 +961,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 index 851bc8dff4..03f3855f0c 100644 --- a/sdk/python/packages/flet/tests/test_schedule_update_threads.py +++ b/sdk/python/packages/flet/tests/test_schedule_update_threads.py @@ -7,8 +7,10 @@ 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 @@ -61,6 +63,16 @@ def run(i): 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 @@ -79,8 +91,7 @@ async def session(): session.start_updates_scheduler() await asyncio.sleep(0) # the scheduler is now waiting for work yield session - session.close() - await asyncio.sleep(0) + await _close(session) loop.set_debug(debug) @@ -117,8 +128,7 @@ def worker(): await asyncio.wait_for(applied.wait(), 2) finally: thread.join() - session.close() - await asyncio.sleep(0) + await _close(session) @pytest.mark.asyncio @@ -168,7 +178,8 @@ async def test_update_on_the_loop_thread_is_queued_right_away(session): def test_update_from_a_thread_after_the_loop_closed_is_ignored(): - """A worker that outlives the session's event loop must not crash.""" + """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() @@ -176,11 +187,14 @@ async def start(): await asyncio.sleep(0) return session - session = asyncio.run(start()) # the loop is closed when this returns + # 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 @@ -189,8 +203,7 @@ async def test_update_from_a_thread_after_close_is_not_queued(): session = _create_session() session.start_updates_scheduler() await asyncio.sleep(0) - session.close() - await asyncio.sleep(0) + await _close(session) errors = _run_in_threads(lambda i: session.schedule_update(_FakeControl()), 1) await asyncio.sleep(0.05) @@ -204,8 +217,7 @@ async def test_effect_from_a_thread_after_close_is_not_queued(): session = _create_session() session.start_updates_scheduler() await asyncio.sleep(0) - session.close() - await asyncio.sleep(0) + await _close(session) class _Hook: setup = None @@ -294,5 +306,120 @@ def set_on_loop(): session.start_updates_scheduler() assert await _wait_for(lambda: control.updates == 1) finally: - session.close() + 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)