Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling update() inside Control.did_mount() causes deadlock #489

Closed
FeodorFitsner opened this issue Oct 17, 2022 · 5 comments
Closed

Calling update() inside Control.did_mount() causes deadlock #489

FeodorFitsner opened this issue Oct 17, 2022 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@FeodorFitsner
Copy link
Contributor

Sample code causing deadlock:

import flet
from flet import Container, Page, Text, UserControl

class MyControl(UserControl):
    def build(self):
        print("i am built")
        self.c = Container()
        self.c.bgcolor = "blue"
        self.c.content = Text("hello there")
        return self.c

    def did_mount(self):
        print("i am mounted")
        self.resize_me()

    def resize_me(self):
        self.c.bgcolor = "red"
        self.update()

def main(page: Page):
    t = MyControl()
    page.add(t)
    print("i am added")

flet.app(target=main)

Solution:

Go through a list of added controls after update() is finished and lock released and call did_mount() for each added control.

@FeodorFitsner FeodorFitsner added the bug Something isn't working label Oct 17, 2022
@FeodorFitsner FeodorFitsner added this to the Controls-S3 milestone Oct 17, 2022
@FeodorFitsner FeodorFitsner added the status: working on it Working on a fix for the issue label Oct 27, 2022
@FeodorFitsner FeodorFitsner removed the status: working on it Working on a fix for the issue label Nov 10, 2022
@ndonkoHenri
Copy link
Collaborator

Hey, will it now be possible to easily access self.page in the UserControl?

@FeodorFitsner
Copy link
Contributor Author

Sure, it's possible now.

@ItsCubeTime
Copy link

ItsCubeTime commented Dec 15, 2022

Hi, coming from #488 which was merged in favor of this. The issue described there still seems present in both 0.3.0.dev941 & 0.2.4 on Pypi.

import flet

class userCtrl(flet.UserControl):
    def build(self):
        return [flet.Container(bgcolor=flet.colors.RED, width=self.page.width, height=100000)]

def main(page: flet.Page):
    page.add(
        userCtrl()
    )

flet.app(
    target=main, web_renderer="html", assets_dir=".", port=3768
    # , view=flet.WEB_BROWSER
)
PS C:\Users\olliv\Desktop\Art And Development\Flutter Learning\Flet\Calculator example> python -u "f:\Olliver\Art and development\Flutter Learning\Flet\Calculator example\selfPageBug.py"
Unhandled error processing page session 07cc183d-3ca2-4055-bcd2-0a70433d1b61: Traceback (most recent call last):
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\flet.py", line 220, in on_session_created
    session_handler(page)
  File "f:\Olliver\Art and development\Flutter Learning\Flet\Calculator example\selfPageBug.py", line 8, in main
    page.add(
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\page.py", line 256, in add
    added_controls = self.__update(self)
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\page.py", line 231, in __update
    control.build_update_commands(self._index, added_controls, commands)
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\control.py", line 353, in build_update_commands
    innerCmds = ctrl._build_add_commands(
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\control.py", line 401, in _build_add_commands
    childCmd = control._build_add_commands(
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\control.py", line 381, in _build_add_commands
    self._build()
  File "C:\Users\olliv\AppData\Local\Programs\Python\Python310\lib\site-packages\flet\user_control.py", line 12, in _build
    content = self.build()
  File "f:\Olliver\Art and development\Flutter Learning\Flet\Calculator example\selfPageBug.py", line 5, in build
    return [flet.Container(bgcolor=flet.colors.RED, width=self.page.width, height=100000)]
AttributeError: 'NoneType' object has no attribute 'width'

@FeodorFitsner
Copy link
Contributor Author

self.page is available in did_mount() method only: #488 (comment)

Control tree is updated with a page instance only after its changes sent to a UI.

@ItsCubeTime
Copy link

ItsCubeTime commented Dec 16, 2022

Ah I see

Thank you for clarifying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants