Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 48 additions & 12 deletions sdk/python/packages/flet/src/flet/components/hooks/use_state.py
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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)
Expand All @@ -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
162 changes: 134 additions & 28 deletions sdk/python/packages/flet/src/flet/messaging/session.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import asyncio
import contextlib
import dataclasses
import inspect
import logging
import traceback
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
Expand All @@ -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

Expand Down Expand Up @@ -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[
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions sdk/python/packages/flet/src/flet/utils/event_loop.py
Original file line number Diff line number Diff line change
@@ -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`.
"""
Loading
Loading