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

When native is True and reload is True, port is ignored and app_shutdown never fires #2107

Open
PawelRoman opened this issue Dec 1, 2023 · 6 comments
Labels
help wanted Extra attention is needed

Comments

@PawelRoman
Copy link

PawelRoman commented Dec 1, 2023

Description

Platform:
Linux Mint 20.03 (ubuntu)
NiceGUI 1.4.3

Steps to replicate:
Save the code below as main.py and run python main.py

from nicegui import app, ui

def app_startup():
    print('app startup')

def app_shutdown():
    print('app shutdown')

app.on_startup(app_startup)
app.on_shutdown(app_shutdown)

ui.run(
    title="Some app",
    port=8502,
    native=True,
    reload=True,
    window_size=(1920, 1200),
    storage_secret='my secret'
)

Expected: Window app opens up, server starts on port 8502, app startup printed out.
Actual: Window app opens up, app startup printed out, but server starts on port 8000 (note: I double checked that 8502 is NOT taken by some other process)

Then close the window by clicking "X"

Expected: app shutdown printed out
Actual: nothing is printed out, the app_shutdown function is never called

@PawelRoman PawelRoman changed the title When native is True, port is ignored and app_shutdown never fires When native is True and reload is True, port is ignored and app_shutdown never fires Dec 1, 2023
@retsyo
Copy link
Contributor

retsyo commented Dec 2, 2023

Currently, port is not used in native mode as #1457 stated. I have made a pull request to change this.

However, I don't know why app_shutdown never fires.

@rodja
Copy link
Member

rodja commented Dec 2, 2023

Thanks @retsyo. On Mac M1 the shutdown works as expected. Are you running on Windows @PawelRoman? Does the shutdown work if you disable reload? What does happen when you use the buttons

ui.button('destroy window', on_click=lambda: app.native.main_window.destroy())
ui.button('nicegui shutdown', on_click=lambda: app.shutdown())

@retsyo
Copy link
Contributor

retsyo commented Dec 2, 2023

On my windows 10 64 bits, both buttons close the window and python

ui.button('destroy window', on_click=lambda: app.native.main_window.destroy())
ui.button('nicegui shutdown', on_click=lambda: app.shutdown())

ui.run(reload=False, native=True)

and PawelRoman's code runs app_shutdown too if reload=False

def app_shutdown():
    print('app shutdown')

app.on_shutdown(app_shutdown)
ui.run(reload=False, native=True)

so does reload=True conflict with app.on_shutdown?

@rodja
Copy link
Member

rodja commented Dec 4, 2023

I fear this must be debugged on a Windows system. On Mac the demo you provided prints the shutdown message. Any help would be appreciated.

@rodja rodja added the help wanted Extra attention is needed label Dec 4, 2023
@PawelRoman
Copy link
Author

As I wrote in the original post, I work on Ubuntu 20 (Linux Mint).

The on_shutdown issue gets fixed when I set reload to False, with reload=True, the on_shutdown does not fire at all. Would also be great if the port issue fix be merged asap, as it's very annoying in certain scenarios not being able to select specific port.

@frankhuurman
Copy link
Contributor

@PawelRoman thanks for the port fix :) saw that it got merged

Tried your examples on a Win 10 machine & python 3.11, came to the same conclusion that the app startup does get printed and the app shutdown doesn't fire with reload=True unless I Ctrl+C the app, when I set reload=False it fires without having to use Ctrl+C.

So that means on both Linux Mint & Win 10 the shutdown event doesn't seem to trigger with reload set to True.

Perhaps the reload/directory&file watcher functionality makes uvicorn spawn a watcher thread and won't let go of it unless Ctrl+C'd?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants