Commit 2fcd89f
### Motivation
This PR fixes zauberzeug#3253, how `app.shutdown` does not work when we do
`ui.run_with` as `Server.instance` was never set.
### Analysis
Note that upstream is extremely unlikely to be able to supply it to us:
- FastAPI CLI simply calls `uvicorn.run`
https://github.com/fastapi/fastapi-cli/blob/e62b61d6c56b77d39310a1c49e8eb7abdad2785a/src/fastapi_cli/cli.py#L218-L233
- `uvicorn.run` doesn't even return `server`
https://github.com/Kludex/uvicorn/blob/918dae6ef91917a5824ab48c46026e07b7c80beb/uvicorn/main.py#L579-L602
### Implementation (Copilot)
**Improvements to shutdown handling:**
* Updated the `shutdown` method in `nicegui/app/app.py` to use garbage
collection to find and set the `should_exit` flag on the correct
`uvicorn.Server` instance if the standard reference is unavailable,
increasing reliability of server shutdown.
* Added the `gc` and `uvicorn` imports to support the improved shutdown
logic in `nicegui/app/app.py`.
[[1]](diffhunk://#diff-adae3337464d5c5adf411c1b2e4b52034747030db2de3f87cfc83fea39763f96R2)
[[2]](diffhunk://#diff-adae3337464d5c5adf411c1b2e4b52034747030db2de3f87cfc83fea39763f96R13)
* **Human note: Server instance is always unavailable when using
`ui.run_with`, and always available when using `ui.run`**
**Application instance management:**
* Introduced a new global variable `fastapi_app` in `nicegui/core.py` to
hold a reference to the FastAPI application, and ensured it is set
during application startup in `nicegui/ui_run_with.py`.
[[1]](diffhunk://#diff-5ed898650c8cdc47c39575a2e752cfe07edba4c755c1c2390460373640983081R6)
[[2]](diffhunk://#diff-5ed898650c8cdc47c39575a2e752cfe07edba4c755c1c2390460373640983081R15)
[[3]](diffhunk://#diff-8af9e19f881b73ad9d426d4e341acfbc54b5f68e57e68d8eb2e7866fdde76f24R95-R96)
* **Human note: Prevents killing the wrong server, in case there are
multiple!**
### Progress
- [x] I chose a meaningful title that completes the sentence: "If
applied, this PR will..."
- [x] The implementation is complete.
- [x] If this PR addresses a security issue, it has been coordinated via
the [security
advisory](https://github.com/zauberzeug/nicegui/security/advisories/new)
process.
- [x] Pytests is out-of-scope.
- [x] Documentation is not necessary for a capability expansion of an
already-there function.
---------
Co-authored-by: Falko Schindler <falko@zauberzeug.com>
1 parent 6fa0b51 commit 2fcd89f
3 files changed
+15
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
189 | 189 | | |
190 | 190 | | |
191 | 191 | | |
192 | | - | |
| 192 | + | |
193 | 193 | | |
194 | 194 | | |
195 | 195 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| 16 | + | |
14 | 17 | | |
15 | 18 | | |
16 | 19 | | |
| |||
86 | 89 | | |
87 | 90 | | |
88 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
89 | 102 | | |
90 | 103 | | |
91 | 104 | | |
| |||
0 commit comments