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

Support --before-graceful-exit #2242

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ Options:
buffer of an incomplete event.
--factory Treat APP as an application factory, i.e. a
() -> <ASGI app> callable.
--before-graceful-exit TEXT A callable to be executed before the server
starts the graceful shutdown.
--help Show this message and exit.
```

Expand Down
2 changes: 2 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ Options:
buffer of an incomplete event.
--factory Treat APP as an application factory, i.e. a
() -> <ASGI app> callable.
--before-graceful-exit TEXT A callable to be executed before the server
starts the graceful shutdown.
--help Show this message and exit.
```

Expand Down
32 changes: 32 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,35 @@ To understand more about the SSL context options, please refer to the [Python do

* `--timeout-keep-alive <int>` - Close Keep-Alive connections if no new data is received within this timeout. **Default:** *5*.
* `--timeout-graceful-shutdown <int>` - Maximum number of seconds to wait for graceful shutdown. After this timeout, the server will start terminating requests.

## Hooks

### Before graceful exit

* `--before-graceful-exit <str>` - A callable object to be executed before the server graceful exit.

```python
from starlette.applications import Starlette

app = Starlette()
app.state.should_exit = False


@app.websocket_route('/ws')
async def ws(websocket):
await websocket.accept()

while not app.state.should_exit:
# How to interrupt this while loop on the shutdown event?
await asyncio.sleep(0.1)

await websocket.close()


def before_graceful_exit():
app.state.should_exit = True


if __name__ == "__main__":
uvicorn.run("example:app", before_graceful_exit_hook="example:before_graceful_exit")
```
12 changes: 12 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,3 +589,15 @@ def test_warn_when_using_reload_and_workers(caplog: pytest.LogCaptureFixture) ->
'"workers" flag is ignored when reloading is enabled.'
in caplog.records[0].message
)


def test_import_before_graceful_exit_hook() -> None:
config = Config(app=asgi_app, before_graceful_exit_hook=lambda: None)
config.load()
assert callable(config.before_graceful_exit_hook)

config = Config(
app=asgi_app, before_graceful_exit_hook="tests.test_config:asgi_app"
)
config.load()
assert callable(config.before_graceful_exit_hook)
9 changes: 9 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,12 @@ async def test_exit_on_create_server_with_invalid_host() -> None:
server = Server(config=config)
await server.serve()
assert exc_info.value.code == 1


@pytest.mark.anyio
async def test_before_graceful_exit_hook() -> None:
config = Config(
app=app, before_graceful_exit_hook=lambda: print("Before graceful exit")
)
async with run_server(config):
pass
7 changes: 7 additions & 0 deletions uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ def __init__(
headers: list[tuple[str, str]] | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
before_graceful_exit_hook: Callable[[], Any] | str | None = None,
abersheeran marked this conversation as resolved.
Show resolved Hide resolved
):
self.app = app
self.host = host
Expand Down Expand Up @@ -271,6 +272,7 @@ def __init__(
self.encoded_headers: list[tuple[bytes, bytes]] = []
self.factory = factory
self.h11_max_incomplete_event_size = h11_max_incomplete_event_size
self.before_graceful_exit_hook = before_graceful_exit_hook

self.loaded = False
self.configure_logging()
Expand Down Expand Up @@ -473,6 +475,11 @@ def load(self) -> None:
"but please consider setting the --factory flag explicitly."
)

if isinstance(self.before_graceful_exit_hook, str):
self.before_graceful_exit_hook = import_from_string(
self.before_graceful_exit_hook
)

if self.interface == "auto":
if inspect.isclass(self.loaded_app):
use_asgi_3 = hasattr(self.loaded_app, "__await__")
Expand Down
10 changes: 10 additions & 0 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No
help="Treat APP as an application factory, i.e. a () -> <ASGI app> callable.",
show_default=True,
)
@click.option(
"--before-graceful-exit",
type=str,
default=None,
help="A callable to be executed before the server starts the graceful shutdown.",
)
def main(
app: str,
host: str,
Expand Down Expand Up @@ -414,6 +420,7 @@ def main(
app_dir: str,
h11_max_incomplete_event_size: int | None,
factory: bool,
before_graceful_exit: str,
) -> None:
run(
app,
Expand Down Expand Up @@ -463,6 +470,7 @@ def main(
factory=factory,
app_dir=app_dir,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
before_graceful_exit_hook=before_graceful_exit,
)


Expand Down Expand Up @@ -515,6 +523,7 @@ def run(
app_dir: str | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
before_graceful_exit_hook: Callable[[], Any] | str | None = None,
) -> None:
if app_dir is not None:
sys.path.insert(0, app_dir)
Expand Down Expand Up @@ -566,6 +575,7 @@ def run(
use_colors=use_colors,
factory=factory,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
before_graceful_exit_hook=before_graceful_exit_hook,
)
server = Server(config=config)

Expand Down
6 changes: 6 additions & 0 deletions uvicorn/server.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import asyncio
import inspect
import logging
import os
import platform
Expand Down Expand Up @@ -270,6 +271,11 @@ async def shutdown(self, sockets: list[socket.socket] | None = None) -> None:
for sock in sockets or []:
sock.close()

if callable(self.config.before_graceful_exit_hook):
f = self.config.before_graceful_exit_hook()
if inspect.isawaitable(f):
await f
abersheeran marked this conversation as resolved.
Show resolved Hide resolved
Comment on lines +274 to +277

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to place this code prior to the point where new connections are no longer accepted (#Stop accepting new connections)?

This could help in resolving the matter we have mentioned in #2257. Additionally, since the topic at hand is related to "before graceful exit," it would be good to execute the code beforehand, before the acceptance of any further connections is stopped.

WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no objection to this, for me these two positions are the same.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adriangb @Kludex , what are you thoughts on this?


# Request shutdown on all existing connections.
for connection in list(self.server_state.connections):
connection.shutdown()
Expand Down