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

[Windows] Python 3.12 and not properly reloading changed files #2138

Open
frankhuurman opened this issue Dec 8, 2023 · 12 comments
Open

[Windows] Python 3.12 and not properly reloading changed files #2138

frankhuurman opened this issue Dec 8, 2023 · 12 comments
Labels
bug Something isn't working waiting Can not be fixed right now

Comments

@frankhuurman
Copy link
Contributor

Description

This might not be the cleanest bug report because I'll update this ticket as I go since I'm currently at work and don't want to forget to report on it whilst I'm experiencing it. Apologies in advance.

There seems to be a bug in the combo of Python 3.12 and NiceGUI (1.4.5 and lower) that in specific cases doesn't pick up on changed files and only continues execution(or show output in Git Bash on Win10) as soon as I close the current open browser tab of the NiceGUI application. Using Ctrl+C in Git Bash during this "hanging" won't stop the program either, only after I close the browser tab it seems to be able to quit the hanging process.

Because it's related to the browser tab this makes me think it's perhaps websocket or button callback related, but perhaps this could be Git Bash related as well, I'm unsure at the moment.

I've switched between Python 3.11.7 and 3.12.0 a couple of times because we want to have a stable experience for a NiceGUI based work application so we had to decide on versions. That's how I found out that 3.11.7 didn't have the bug and 3.12.0 does.

Code snippet 1 to reproduce:

from nicegui import ui

class SomePage:

    def show(self) -> None:
        ui.button("test", on_click=self.test_thing)

    def test_thing(self) -> None:
        print("a thing", flush=True)

SomePage().show()
ui.run()

Code snippet 2 to reproduce:

from nicegui import ui

class SomePage:

    def show(self) -> None:
        ui.button("test", on_click=self.test_thing)

    def test_thing(self) -> None:
        print("no thing", flush=True)


SomePage().show()
ui.run()

Steps in order to reproduce weird behaviour:

  • run code snippet 1
  • while running code snippet 1, press the nicegui ui.button in the app and keep the app running for the next couple of steps
  • notice that a thing is printed in the terminal
  • copy and paste code snippet 2 in the place of code snippet 1(or just manually edit the string to be printed in the test_thing method) and save the file
  • notice that WARNING: WatchFiles detected changes in 'test.py'. Reloading... is printed in the terminal
  • press the ui.button in the app again
  • notice that a thing is printed in the terminal, while we expected no thing
  • press Ctrl+C in the terminal where the app is running
  • Notice how it does not seem to shutdown the app
  • Close the browser tab where the app is showing
  • Notice how the terminal now immediately shows some output, either a asyncio.exceptions.CancelledError or a KeyboardInterrupt with sometimes the line "NiceGUI ready to go on http://localhost:8080, http://10.107.239.140:8080, http://10.13.192.176:8080, http://172.24.176.1:8080, http://172.24.208.1:8080, and http://192.168.56.1:8080" again before the app actually closes (sometimes it also keeps running but most of the time it closes)
@frankhuurman
Copy link
Contributor Author

Update: It doesn't even seem to be callback related, just changing the text on the ui.button in the code snippet while the app is running is enough to get the app hanging.

Perhaps the Filewatcher is not Python 3.12 ready?

I need to confirm if it's not a Windows/Git Bash/Python 3.12 specific issue instead of NiceGUI. Will do that as soon as I get out of work.

@retsyo
Copy link
Contributor

retsyo commented Dec 8, 2023

my python 3.10.9 amd 64 for windows does not reload changed file too. even it says

WARNING:  WatchFiles detected changes in 'can't_fire.py'. Reloading...

@frankhuurman
Copy link
Contributor Author

Interesting, are you also using Git Bash to run your app or something else?

Then again, I don't have the problem with Python 3.11.7 so I don't think it's related.
You could try to install 3.11.7 and see if the problem goes away?

@retsyo
Copy link
Contributor

retsyo commented Dec 9, 2023

it runs out that no matter I use python 3.10 or 3.11

  • if I run nicegui project in command line as e:\py3\python.exe demo.py, and modify the code in some editor, the new code will be loaded and refresh the webpage automatically
  • if I edit and run (via menu tools - > go, or F5 shortcut key) nicegui project in SciTe, file change can be detected
WARNING:  WatchFiles detected changes in 'demo.py'. Reloading...

but the webpage will never been updated

so I think this problem lives in SciTe. However I don't know the reason

@frankhuurman
Copy link
Contributor Author

it runs out that no matter I use python 3.10 or 3.11

  • if I run nicegui project in command line as e:\py3\python.exe demo.py, and modify the code in some editor, the new code will be loaded and refresh the webpage automatically
  • if I edit and run (via menu tools - > go, or F5 shortcut key) nicegui project in SciTe, file change can be detected
WARNING:  WatchFiles detected changes in 'demo.py'. Reloading...

but the webpage will never been updated

so I think this problem lives in SciTe. However I don't know the reason

It sounds to me like it runs as intended and it's a SciTe issue.
I'm trying to figure out if this is a Windows issue, a Python issue, a Git Bash issue, a NiceGUI issue or one of its dependencies, or something else entirely or a combination of it.
If anyone recognizes the issue, please share :)

@frankhuurman
Copy link
Contributor Author

Python 3.12.1 (couldn't seem to install 3.12.0 through the deadsnakes PPA) and the latest NiceGUI running on Ubuntu 22.04 seems to work just fine.

I see that 3.12.1 just dropped for Windows on python.org as well. I'll check if the point release fixes it or if I need some other tests.

@frankhuurman
Copy link
Contributor Author

image

Python 3.12.1 doesn't seem to fix the issue for me either, tried it in both Git Bash and cmd.exe but still the hanging occurs while filewatcher tries to pick up on file changes. I've also noticed Python 3.12+ forces admin rights during installation on Windows and Python 3.11+ does not as evidenced at the checkboxes here:

image
image

Checkboxes are grayed out anyway so I unfortunately can't turn them on/off to test(on a work laptop so probably don't have full admin access to change it).

Anyway I've tried to run my script both with an admin rights git bash window and one without admin rights and both didn't seem to work so I feel that the admin rights is a dead end as well.

Next I tried to do it on a different machine instead of my Windows work laptop so I could exclude more things, so I spinned up a Windows 10 VM in Gnome Boxes on my Ubuntu 22.04 laptop. Already had Python 3.11.4 installed there, tried the same code snippet and it worked fine in both Git Bash/CMD.

However when I installed Python 3.12+ again(this time I tried it both with/without admin rights during install) I again get the hanging filewatcher bug.

@rodja @falkoschindler can one of you confirm on a Windows 10 machine if my steps-to-reproduce won't hang the filewatcher when running my code example and change the text of the button while it's running & save the file in either Git bash or cmd?

Scratching my head here since it all seems to be related to the Python 3.12, NiceGUI & Windows combo.

@rodja
Copy link
Member

rodja commented Dec 10, 2023

We are only running Mac and Linux systems here. It would be interesting if you could reproduce the behaviour with plain FastAPI and Uvicorn.

@frankhuurman
Copy link
Contributor Author

We are only running Mac and Linux systems here. It would be interesting if you could reproduce the behaviour with plain FastAPI and Uvicorn.

Everything is Linux at home but unfortunately Gov IT in the Netherlands, excluding servers, is mostly Windows.
Unsure if WSL would show the same issue, could try that still.

I'll try to reproduce the behaviour with FastAPI/Uvicorn, good idea.

Would assume it would be something along the lines of:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def somepage():
    return {"message": "Aloha"}

and run it using uvicorn main:app --reload --port 5000 if I recall correctly?

@frankhuurman
Copy link
Contributor Author

Can confirm the bug is present in FastAPI/Uvicorn on Win10 with Python 3.12+
I used the code example from my last reply and as soon as I change the text of the return message and save, the "INFO: Shutting down" lines show up and it seems to shut down some part of the server or filewatcher process.

$ uvicorn test2:app --reloadINFO:     Will watch for changes in these directories: ['C:\\Users\\Frinux\\Documents\\niceguitest']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [7228] using WatchFiles
INFO:     Started server process [6576]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     127.0.0.1:51503 - "GET / HTTP/1.1" 200 OK
WARNING:  WatchFiles detected changes in 'test2.py'. Reloading...
INFO:     Shutting down
INFO:     127.0.0.1:51504 - "GET / HTTP/1.1" 200 OK
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [6576]
INFO:     Started server process [7100]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
2023-12-10.23-58-11.mp4

In the video below you can see me trying to Ctrl+C to quit the running server after I do a file change, it first throws an asyncio.exceptions.CancelledError and still tries to keep the server running. Then I hit Ctrl+C again and you can clearly see the "Shutting down" process being stuck forever until I close the browser tab which immediately continues the rest of the terminal output.

2023-12-11.00-00-33.mp4

So I guess we can close this bug for NiceGUI itself(even though this would mean Python 3.12 isn't supported for Windows users until uvicorn or fastapi gets a fix out).

@falkoschindler falkoschindler added bug Something isn't working waiting Can not be fixed right now labels Dec 11, 2023
@falkoschindler
Copy link
Contributor

Thanks for the extensive research, @frankhuurman!
Looks like we need to wait for an upstream fix.

@frankhuurman frankhuurman changed the title Python 3.12 and not properly reloading changed files [Windows] Python 3.12 and not properly reloading changed files Dec 11, 2023
@rohitsathish
Copy link

@frankhuurman did rolling back to a specific version of NiceGUI or/and python work for you? I'm still struggling with this

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

No branches or pull requests

5 participants