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

Add option to set worker healthcheck timeout #2500

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ Options:
buffer of an incomplete event.
--factory Treat APP as an application factory, i.e. a
() -> <ASGI app> callable.
--worker-healthcheck-timeout FLOAT
Timeout for healthcheck between supervisor
and worker in seconds (used only if workers
> 1).
--help Show this message and exit.
```

Expand Down
4 changes: 4 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ Options:
buffer of an incomplete event.
--factory Treat APP as an application factory, i.e. a
() -> <ASGI app> callable.
--worker-healthcheck-timeout FLOAT
Timeout for healthcheck between supervisor
and worker in seconds (used only if workers
> 1).
--help Show this message and exit.
```

Expand Down
1 change: 1 addition & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Using Uvicorn with watchfiles will enable the following options (which are other
* `--ws-ping-timeout <float>` - Set the WebSockets ping timeout, in seconds. Please note that this can be used only with the default `websockets` protocol. **Default:** *20.0*
* `--lifespan <str>` - Set the Lifespan protocol implementation. **Options:** *'auto', 'on', 'off'.* **Default:** *'auto'*.
* `--h11-max-incomplete-event-size <int>` - Set the maximum number of bytes to buffer of an incomplete event. Only available for `h11` HTTP protocol implementation. **Default:** *'16384'* (16 KB).
* `--worker_healthcheck_timeout <float>` - Timeout for healthcheck between supervisor and worker in seconds (used only if workers > 1). **Default:** *'5.0'* (5 s).

## Application Interface

Expand Down
2 changes: 2 additions & 0 deletions uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ def __init__(
headers: list[tuple[str, str]] | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
worker_healthcheck_timeout: float = 5.0,
):
self.app = app
self.host = host
Expand Down Expand Up @@ -268,6 +269,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.worker_healthcheck_timeout = worker_healthcheck_timeout

self.loaded = False
self.configure_logging()
Expand Down
11 changes: 11 additions & 0 deletions uvicorn/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,13 @@ 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(
"--worker-healthcheck-timeout",
"worker_healthcheck_timeout",
type=float,
default=5.0,
help="Timeout for healthcheck between supervisor and worker in seconds (used only if workers > 1).",
)
def main(
app: str,
host: str,
Expand Down Expand Up @@ -408,6 +415,7 @@ def main(
app_dir: str,
h11_max_incomplete_event_size: int | None,
factory: bool,
worker_healthcheck_timeout: float,
) -> None:
run(
app,
Expand Down Expand Up @@ -457,6 +465,7 @@ def main(
factory=factory,
app_dir=app_dir,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
worker_healthcheck_timeout=worker_healthcheck_timeout,
)


Expand Down Expand Up @@ -509,6 +518,7 @@ def run(
app_dir: str | None = None,
factory: bool = False,
h11_max_incomplete_event_size: int | None = None,
worker_healthcheck_timeout: float = 5.0,
) -> None:
if app_dir is not None:
sys.path.insert(0, app_dir)
Expand Down Expand Up @@ -560,6 +570,7 @@ def run(
use_colors=use_colors,
factory=factory,
h11_max_incomplete_event_size=h11_max_incomplete_event_size,
worker_healthcheck_timeout=worker_healthcheck_timeout,
)
server = Server(config=config)

Expand Down
5 changes: 3 additions & 2 deletions uvicorn/supervisors/multiprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,16 +164,17 @@ def keep_subprocess_alive(self) -> None:
return # parent process is exiting, no need to keep subprocess alive

for idx, process in enumerate(self.processes):
if process.is_alive():
if process.is_alive(self.config.worker_healthcheck_timeout):
continue

logger.info(f"Child process [{process.pid}] is unresponsive")

process.kill() # process is hung, kill it
process.join()

if self.should_exit.is_set():
return # pragma: full coverage

logger.info(f"Child process [{process.pid}] died")
process = Process(self.config, self.target, self.sockets)
process.start()
self.processes[idx] = process
Expand Down
Loading