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

on_shutdown callback not called on Windows 10 #1050

Closed
chhinze opened this issue Jun 19, 2023 · 11 comments
Closed

on_shutdown callback not called on Windows 10 #1050

chhinze opened this issue Jun 19, 2023 · 11 comments
Labels
bug Something isn't working
Milestone

Comments

@chhinze
Copy link

chhinze commented Jun 19, 2023

Description

Hi all,

thanks for this awesome project.

Registering a callback with app.on_shutdown should lead to the callback to be called during hot reloading (described in https://nicegui.io/documentation#events). This worked on a machine with Ubuntu 23.04 as expected, but on Windows 10 it is not called.

Minimal example to reproduce:

from nicegui import app, ui

app.on_shutdown(lambda: print("Shutdown called"))
app.on_startup(lambda : print("Startup called"))

ui.markdown("# Title")
ui.run(title="Does not call shutdown on Win10 Demo")

On Windows 10 the console Output is

Startup called
NiceGUI ready to go in http://...
?[33mWARNING?[0m: Watch files detected changes in ...

[here is, where the reload happens, triggered by Ctrl+S in the file.]

Startup called
NiceGUI ready to go in http://...

In Ubuntu I see a line Shutdown called before the second startup called, this is also what I would expect reading the documentation.

Am I using it wrong?
Is this related to #604 i.e., the Ctrl handler is not set properly?

Best regards
Chris

@falkoschindler
Copy link
Contributor

Thanks for reporting this issue, @chhinze! This is definitely unexpected.

NiceGUI's lifecycle hooks like on_startup and on_shutdown rely on FastAPI. Can you, please, check if the following pure FastAPI example has the same problem on Windows 10?

main.py:

import uvicorn
from fastapi import FastAPI

app = FastAPI()

@app.on_event("startup")
async def startup_event():
    print("Application startup")

@app.on_event("shutdown")
async def shutdown_event():
    print("Application shutdown")

@app.get("/")
def read_root():
    return {"Hello": "World"}

if __name__ == "__main__":
    uvicorn.run('main:app', host="0.0.0.0", port=8000, reload=True)

@chhinze
Copy link
Author

chhinze commented Jun 20, 2023

Thank you a lot for the quick response, @falkoschindler.

I just tested your code. The problem persists also with the pure FastAPI example you provided. The output is:

INFO:     Will atch for changes in these directories: ['...']
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [3644] using WatchFiles
INFO:     Started server process [7404]
INFO:     Waiting for application startup.
Application startup
INFO:     Application startup complete.
WARNING:  WatchFiles detected changes in 'main.py'. Reloading...
INFO:     Started server process [5596]
INFO:     Waiting for application startup.
Application startup
INFO:     Application startup complete.

On Linux, after the line WARNING: WatchFiles detected changes in 'main.py'. Reloading... I get the additional lines

INFO:   Shutting down
INFO:   Waiting for application shutdown.
Application shutdown 
INFO:   Application shutdown complete
INFO:   Finished server process [ProcessNo]

Thanks to your information that it is related to FastAPI I now found the open issues fastapi#6026 and fastapi#6391. A fix seems to be merged already back in April and is scheduled for uvicorn release 0.22.0.

I guess the best option is to just wait for this release of FastAPI.
Or is there a workaround to register a callback before reloading?

Edit: Clarified that the fix is in uvocorn (not fastapi).

@rodja
Copy link
Member

rodja commented Jun 20, 2023

Or is there a workaround to register a callback before reloading?

Unfortunately not. Or none that we are aware of. We would really much like to execute some code before uvicorn reloads, but we have not found a way yet.

@falkoschindler
Copy link
Contributor

Ok, we'll wait for encode/uvicorn#1584 to be released.

@falkoschindler falkoschindler added the waiting Can not be fixed right now label Jun 22, 2023
@firai
Copy link

firai commented Jul 3, 2023

Ok, we'll wait for encode/uvicorn#1584 to be released.

It seems like this was already released in uvicorn 0.22.0 in April (https://github.com/encode/uvicorn/releases/tag/0.22.0)? However, uvicorn seems to be pinned to 0.20.0 in NiceGui.

@chhinze
Copy link
Author

chhinze commented Jul 4, 2023

Yes, you are right. I was misguided by the milestone progress bar in the issue (encode/uvicorn#1584). Sorry for the confusion.
I also tested manually upgrading uvicorn: on_shutdown callbacks work as expected now.

@firai
Copy link

firai commented Jul 4, 2023

@falkoschindler Can uvicorn be upgraded to 0.22.0? Or would there be blocking conflicts from changes in uvicorn 0.21.0 or 0.22.0?

@rodja
Copy link
Member

rodja commented Jul 4, 2023

@firai thanks for asking. We will use uvicorn 0.22.0 in our upcoming 1.3 release. It would be great if you could try it out and tell us if it works for you.

@firai
Copy link

firai commented Jul 4, 2023

Ah, I assumed that development was on main without looking. Didn't realize that it's already changed on the 1.3 branch. Thanks!

@rodja
Copy link
Member

rodja commented Jul 4, 2023

Yea, we normally do everything on main. But breaking changes have accumulated on the v1.3 branch and is almost ready to be released.

@falkoschindler falkoschindler removed the waiting Can not be fixed right now label Jul 6, 2023
@falkoschindler
Copy link
Contributor

Ok, uvicorn has been updated in commit 578de6a. It will be released with NiceGUI 1.3.0. Therefore I'll close this issue.

@falkoschindler falkoschindler added this to the 1.3.0 milestone Jul 6, 2023
@falkoschindler falkoschindler added the bug Something isn't working label Jul 6, 2023
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

4 participants