From e341f5a237004d26f71bc5f74da35eb76ca79128 Mon Sep 17 00:00:00 2001 From: jgoutin Date: Wed, 26 Oct 2022 14:22:06 +0200 Subject: [PATCH 1/2] Only remove UDS socket if exists. --- uvicorn/main.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/uvicorn/main.py b/uvicorn/main.py index 16f5aa117..c96386193 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -568,7 +568,10 @@ def run( else: server.run() if config.uds: - os.remove(config.uds) # pragma: py-win32 + try: + os.remove(config.uds) # pragma: py-win32 + except FileNotFoundError: + pass if not server.started and not config.should_reload and config.workers == 1: sys.exit(STARTUP_FAILURE) From a6ed689f079a23a99fe2601c8582d3c9dfaaae92 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Thu, 27 Oct 2022 09:56:04 +0200 Subject: [PATCH 2/2] Check if file exists instead of relying on exception --- tests/test_cli.py | 1 - uvicorn/main.py | 7 ++----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index eb950ff88..0d34d1d36 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -95,7 +95,6 @@ def test_cli_call_multiprocess_run() -> None: def test_cli_uds(tmp_path: Path) -> None: # pragma: py-win32 runner = CliRunner() uds_file = tmp_path / "uvicorn.sock" - uds_file.touch(exist_ok=True) with mock.patch.object(Config, "bind_socket") as mock_bind_socket: with mock.patch.object(Multiprocess, "run") as mock_run: diff --git a/uvicorn/main.py b/uvicorn/main.py index c96386193..3c59fa131 100644 --- a/uvicorn/main.py +++ b/uvicorn/main.py @@ -567,11 +567,8 @@ def run( Multiprocess(config, target=server.run, sockets=[sock]).run() else: server.run() - if config.uds: - try: - os.remove(config.uds) # pragma: py-win32 - except FileNotFoundError: - pass + if config.uds and os.path.exists(config.uds): + os.remove(config.uds) # pragma: py-win32 if not server.started and not config.should_reload and config.workers == 1: sys.exit(STARTUP_FAILURE)