bug: `AssertionError: assert self.__uid is not None`
Duplicate Check
- [x] I have searched the opened issues and there are no duplicates
Describe the bug
An assertion error is raised when running the attached code.
Code sample
Code
import time
import flet as ft
class ChatMessage(ft.Row):
def __init__(self, message: str):
super().__init__()
self.controls = [ft.CircleAvatar(content=ft.Text("U")), ft.Text(message)]
def main(page: ft.Page):
def on_message(message: str):
chat.controls.append(ChatMessage(message))
page.update()
page.pubsub.subscribe(on_message)
chat = ft.ListView(
expand=True,
auto_scroll=True,
)
page.add(chat)
for i in range(300):
page.pubsub.send_all(f"message {i}")
time.sleep(0.2)
ft.app(target=main)
To reproduce
Run the code, then wait for some time while observing the terminal.
Expected behavior
No response
Screenshots / Videos
Captures
[Upload media here]
Operating System
macOS
Operating system details
15.0
Flet version
0.26.0
Regression
I'm not sure / I don't know
Suggestions
No response
Logs
Logs
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/playground/test-b.py", line 15, in on_message
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/page.py", line 732, in update
r = self.__update(self)
^^^^^^^^^^^^^^^^^^^
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/page.py", line 798, in __update
commands, added_controls, removed_controls = self.__prepare_update(*controls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/page.py", line 814, in __prepare_update
control.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 483, in build_update_commands
ctrl.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 483, in build_update_commands
ctrl.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 483, in build_update_commands
ctrl.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 502, in build_update_commands
assert self.__uid is not None
^^^^^^^^^^^^^^^^^^^^^^
AssertionError
Future exception was never retrieved
future: <Future finished exception=AssertionError()>
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/concurrent/futures/thread.py", line 58, in run
result = self.fn(*self.args, **self.kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/playground/test-b.py", line 15, in on_message
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/page.py", line 732, in update
r = self.__update(self)
^^^^^^^^^^^^^^^^^^^
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/page.py", line 798, in __update
commands, added_controls, removed_controls = self.__prepare_update(*controls)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/page.py", line 814, in __prepare_update
control.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 483, in build_update_commands
ctrl.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 483, in build_update_commands
ctrl.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 483, in build_update_commands
ctrl.build_update_commands(
File "/Users/ndonkohenri/PycharmProjects/flet-dev/flet/sdk/python/packages/flet/src/flet/core/control.py", line 502, in build_update_commands
assert self.__uid is not None
Additional details
Issue happens occasionally but very often.
I used to get this error and as am talking right now, I still run into it but the only temporary fix I discovered for it is to decrease on the consistent rates where your app's ui is updated or reduce on calling page.update consistently.
try to remove page.update() from on_message method!
try to remove page.update() from on_message method!
The UI won't get updated in that case. 😅
Try increasing the sleep value to something like 0.6. U'll not run into that error. The only reason why ure getting assertion is because you're trying to update the app's ui At the fastest rate in the way that the app is requesting a ui update even before completing the previous ui update request.
Any ideas when this issue will be resolved? If it helps anyone else, this appears to act as a temp workaround:
def page_update(self) -> None:
if self._page is None:
return
if self._page_update_in_progress:
self._page_updates_missed += 1
return
try:
self._page_update_in_progress = True
self._page.update()
time.sleep(0.5)
while self._page_updates_missed > 0:
self._page_updates_missed = 0
self._page.update()
time.sleep(0.5)
finally:
self._page_update_in_progress = False
The above acts as a replacement/wrapper to page.update(). It assumes that you have access to the original page passed in on component creation. Not ideal, but appears to do the job (sometimes!).
@kilesk Can you please show us how to implement that with a sample of a working code
I've since moved to using async variant of the client. No longer need the call to sleep and have not had the problem since by utilising an async lock to control access to the update.
NOTE: Have not tested the below, literally wrote it out here, but should give you a better idea of how go about things.
import asyncio
import flet as ft
import pydantic
class BaseModel(pydantic.BaseModel):
model_config = pydantic.ConfigDict(
arbitrary_types_allowed=True, populate_by_name=True
)
class ReadWriteLock(BaseModel):
_read_lock: asyncio.Lock = pydantic.PrivateAttr(default_factory=asyncio.Lock)
_write_lock: asyncio.Lock = pydantic.PrivateAttr(default_factory=asyncio.Lock)
_read_count: int = pydantic.PrivateAttr(default=0)
async def acquire_read(self):
async with self._read_lock:
self._read_count += 1
if self._read_count == 1:
await self._write_lock.acquire()
async def release_read(self):
async with self._read_lock:
self._read_count -= 1
if self._read_count == 0:
self._write_lock.release()
async def acquire_write(self):
await self._write_lock.acquire()
def release_write(self):
self._write_lock.release()
class MyComponent(BaseModel):
_page: ft.Page | None = pydantic.PrivateAttr(default=None)
_page_update_lock: ReadWriteLock = pydantic.PrivateAttr(
default_factory=ReadWriteLock
)
async def create(self, page: ft.Page) -> None:
self._page = page
self._setup_pubsubs()
# Create controls
...
def _setup_pubsubs(self):
self._page.pubsub.subscribe_topic(
topic=TopicType.CONTACT_DETAILS.name,
handler=self._on_set_contact_details,
)
...
async def _on_set_contact_details(
self, topic: str, contact_details: ContactDetails
) -> None:
# Set contact details control values
await self.page_update()
async def page_update(self) -> None:
"""Workaround for page.update() bug: https://github.com/flet-dev/flet/issues/4794"""
if self._page is None:
return
try:
await self._page_update_lock.acquire_write()
self._page.update()
finally:
self._page_update_lock.release_write()
One thing I did notice in the library code, with reference to flet.core.page.update:
...
class Page(AdaptiveControl):
...
def update(self, *controls) -> None:
with self.__lock:
if len(controls) == 0:
r = self.__update(self)
else:
r = self.__update(*controls)
self.__handle_mount_unmount(*r)
...
self.__handle_mount_unmount() is outside the lock, which could potentially be causing the issue (although there could be reasons for doing this).
@FeodorFitsner @ndonkoHenri
Tried the latest pre-release and it worked without errors.
import asyncio
import flet as ft
class ChatMessage(ft.Row):
def __init__(self, message: str):
super().__init__()
self.controls = [ft.CircleAvatar(content=ft.Text("U")), ft.Text(message)]
async def main(page: ft.Page):
def on_message(message: str):
chat.controls.append(ChatMessage(message))
page.update()
page.pubsub.subscribe(on_message)
page.add(
chat := ft.ListView(
expand=True,
auto_scroll=True,
)
)
for i in range(100):
page.pubsub.send_all(f"message {i}")
await asyncio.sleep(0.1)
ft.run(main)
Let know if you guys still face an issue.