From 3587023e86e00fe9c6bf3d104e01b19dea17c709 Mon Sep 17 00:00:00 2001 From: socrateslee Date: Thu, 25 Jul 2024 23:22:56 +0800 Subject: [PATCH 1/3] add timeout_worker_is_alive as an option for main.run. The option let user set the timeout for judging if a process is dead in multiprocessing mode(i.e., workers > 1). --- uvicorn/config.py | 2 ++ uvicorn/main.py | 10 ++++++++++ uvicorn/supervisors/multiprocess.py | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/uvicorn/config.py b/uvicorn/config.py index 9aff8c968..d733d3d1c 100644 --- a/uvicorn/config.py +++ b/uvicorn/config.py @@ -212,6 +212,7 @@ def __init__( timeout_keep_alive: int = 5, timeout_notify: int = 30, timeout_graceful_shutdown: int | None = None, + timeout_worker_is_alive: float = 5, callback_notify: Callable[..., Awaitable[None]] | None = None, ssl_keyfile: str | None = None, ssl_certfile: str | os.PathLike[str] | None = None, @@ -256,6 +257,7 @@ def __init__( self.timeout_keep_alive = timeout_keep_alive self.timeout_notify = timeout_notify self.timeout_graceful_shutdown = timeout_graceful_shutdown + self.timeout_worker_is_alive = timeout_worker_is_alive self.callback_notify = callback_notify self.ssl_keyfile = ssl_keyfile self.ssl_certfile = ssl_certfile diff --git a/uvicorn/main.py b/uvicorn/main.py index 4352efbca..15ef14fab 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -280,6 +280,12 @@ def print_version(ctx: click.Context, param: click.Parameter, value: bool) -> No default=None, help="Maximum number of seconds to wait for graceful shutdown.", ) +@click.option( + "--timeout-worker-is-alive", + type=float, + default=5, + help="Maximum number of seconds to wait for judging if a worker process is alive.", +) @click.option("--ssl-keyfile", type=str, default=None, help="SSL key file", show_default=True) @click.option( "--ssl-certfile", @@ -394,6 +400,7 @@ def main( limit_max_requests: int, timeout_keep_alive: int, timeout_graceful_shutdown: int | None, + timeout_worker_is_alive: float, ssl_keyfile: str, ssl_certfile: str, ssl_keyfile_password: str, @@ -443,6 +450,7 @@ def main( limit_max_requests=limit_max_requests, timeout_keep_alive=timeout_keep_alive, timeout_graceful_shutdown=timeout_graceful_shutdown, + timeout_worker_is_alive=timeout_worker_is_alive, ssl_keyfile=ssl_keyfile, ssl_certfile=ssl_certfile, ssl_keyfile_password=ssl_keyfile_password, @@ -495,6 +503,7 @@ def run( limit_max_requests: int | None = None, timeout_keep_alive: int = 5, timeout_graceful_shutdown: int | None = None, + timeout_worker_is_alive: float = 5, ssl_keyfile: str | None = None, ssl_certfile: str | os.PathLike[str] | None = None, ssl_keyfile_password: str | None = None, @@ -547,6 +556,7 @@ def run( limit_max_requests=limit_max_requests, timeout_keep_alive=timeout_keep_alive, timeout_graceful_shutdown=timeout_graceful_shutdown, + timeout_worker_is_alive=timeout_worker_is_alive, ssl_keyfile=ssl_keyfile, ssl_certfile=ssl_certfile, ssl_keyfile_password=ssl_keyfile_password, diff --git a/uvicorn/supervisors/multiprocess.py b/uvicorn/supervisors/multiprocess.py index 93c34d0d3..1a3f8feba 100644 --- a/uvicorn/supervisors/multiprocess.py +++ b/uvicorn/supervisors/multiprocess.py @@ -164,7 +164,7 @@ 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(timeout=self.config.timeout_worker_is_alive): continue process.kill() # process is hung, kill it From 9676be8debc4cf1b22c8e59f63866da21c117740 Mon Sep 17 00:00:00 2001 From: socrateslee Date: Thu, 25 Jul 2024 23:32:58 +0800 Subject: [PATCH 2/3] add the command line option for --timeout-worker-is-alive in docs --- docs/index.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/index.md b/docs/index.md index 5d805316b..d434839a9 100644 --- a/docs/index.md +++ b/docs/index.md @@ -182,6 +182,9 @@ Options: --timeout-graceful-shutdown INTEGER Maximum number of seconds to wait for graceful shutdown. + --timeout-worker-is-alive FLOAT + Maximum number of seconds to wait for + judging if a worker process is alive. --ssl-keyfile TEXT SSL key file --ssl-certfile TEXT SSL certificate file --ssl-keyfile-password TEXT SSL keyfile password From f1873bc719c53c603348f58a1ee84a1cec9f65f9 Mon Sep 17 00:00:00 2001 From: socrateslee Date: Thu, 1 Aug 2024 15:00:58 +0800 Subject: [PATCH 3/3] add --timeout-worker-is-alive in docs/deployment --- docs/deployment.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/deployment.md b/docs/deployment.md index 7a2c7972c..09b9c1122 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -112,6 +112,9 @@ Options: --timeout-graceful-shutdown INTEGER Maximum number of seconds to wait for graceful shutdown. + --timeout-worker-is-alive FLOAT + Maximum number of seconds to wait for + judging if a worker process is alive. --ssl-keyfile TEXT SSL key file --ssl-certfile TEXT SSL certificate file --ssl-keyfile-password TEXT SSL keyfile password