From 0b3102e6f3811b88fdc2ab9415b687b30c070c19 Mon Sep 17 00:00:00 2001 From: Romulo Quidute Filho Date: Thu, 3 Dec 2026 10:02:16 -0300 Subject: [PATCH 1/3] CLI: give project logs a longer timeout and report a timeout accurately th-cli project logs failed after 5.8s with ReadTimeout on a project with 12 executions (36 MB of logs): the command used httpx's default 5s read timeout, but the backend builds the whole archive before it sends the first byte, so the wait grows with the project. Other long-running commands already set their own timeout. - PROJECT_LOGS_TIMEOUT (300s read, 10s connect) for project logs only, passed through get_sync_apis(timeout=). - A timeout is no longer reported as 'Is the backend running?': the connection worked, the backend is just still building the archive. Other transport errors keep the backend hint. Refs project-chip/certification-tool#1232 --- tests/test_project_commands.py | 69 +++++++++++++++++++++++++++++++++- th_cli/commands/project.py | 25 ++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/tests/test_project_commands.py b/tests/test_project_commands.py index 247428e..d6d44bc 100644 --- a/tests/test_project_commands.py +++ b/tests/test_project_commands.py @@ -19,12 +19,13 @@ from pathlib import Path from unittest.mock import Mock, patch +import httpx import pytest from click.testing import CliRunner from th_cli.api_lib_autogen import models as api_models -from th_cli.api_lib_autogen.exceptions import UnexpectedResponse -from th_cli.commands.project import project +from th_cli.api_lib_autogen.exceptions import ResponseHandlingException, UnexpectedResponse +from th_cli.commands.project import PROJECT_LOGS_TIMEOUT, project @pytest.mark.unit @@ -1070,6 +1071,70 @@ def test_import_project_help_message(self, cli_runner: CliRunner) -> None: assert "--file" in result.output +@pytest.mark.unit +@pytest.mark.cli +class TestProjectLogsTimeout: + """`project logs` must not use httpx's 5s default: the backend sends the first byte only after it has built the + whole archive, so the wait grows with the project.""" + + def invoke_logs(self, cli_runner, mock_sync_apis, mock_api_client, temp_dir, **download): + mock_sync_apis.projects_api.download_project_logs_api_v1_projects__id__logs_get.configure_mock(**download) + with patch("th_cli.commands.project.get_client", return_value=mock_api_client) as get_client: + with patch("th_cli.commands.project.SyncApis", return_value=mock_sync_apis): + with cli_runner.isolated_filesystem(temp_dir=temp_dir): + result = cli_runner.invoke(project, ["logs", "--id", "1"]) + written = list(Path(".").iterdir()) + return result, get_client, written + + def test_logs_asks_for_an_extended_timeout( + self, cli_runner, mock_sync_apis, mock_api_client, sample_project, temp_dir + ): + mock_sync_apis.projects_api.read_project_api_v1_projects__id__get.return_value = sample_project + result, get_client, _ = self.invoke_logs( + cli_runner, mock_sync_apis, mock_api_client, temp_dir, return_value=b"zip" + ) + assert result.exit_code == 0 + assert get_client.call_args.kwargs["timeout"] is PROJECT_LOGS_TIMEOUT + assert PROJECT_LOGS_TIMEOUT.read >= 120.0 # at least what a single execution's log download gets + assert PROJECT_LOGS_TIMEOUT.connect == 10.0 + + def test_other_project_commands_keep_the_default_timeout(self, cli_runner, mock_sync_apis, mock_api_client): + mock_sync_apis.projects_api.read_projects_api_v1_projects__get.return_value = [] + with patch("th_cli.commands.project.get_client", return_value=mock_api_client) as get_client: + with patch("th_cli.commands.project.SyncApis", return_value=mock_sync_apis): + cli_runner.invoke(project, ["list"]) + assert get_client.call_args.kwargs["timeout"] is None + + def test_a_read_timeout_is_reported_as_slow_not_as_backend_down( + self, cli_runner, mock_sync_apis, mock_api_client, temp_dir + ): + error = ResponseHandlingException(httpx.ReadTimeout("")) + result, _, written = self.invoke_logs(cli_runner, mock_sync_apis, mock_api_client, temp_dir, side_effect=error) + assert result.exit_code == 1 + assert "did not start sending the logs of project 1 within 300s" in result.output + assert "reachable" in result.output + assert "Is the backend running?" not in result.output # the connection worked + assert written == [] # no partial file + + def test_a_refused_connection_still_gets_the_backend_hint( + self, cli_runner, mock_sync_apis, mock_api_client, temp_dir + ): + error = ResponseHandlingException(httpx.ConnectError("")) + result, _, _ = self.invoke_logs(cli_runner, mock_sync_apis, mock_api_client, temp_dir, side_effect=error) + assert result.exit_code == 1 + assert "ConnectError" in result.output + assert "Is the backend running?" in result.output + assert "did not start sending" not in result.output + + def test_every_kind_of_httpx_timeout_is_treated_as_a_timeout( + self, cli_runner, mock_sync_apis, mock_api_client, temp_dir + ): + for exc in (httpx.ReadTimeout(""), httpx.WriteTimeout(""), httpx.PoolTimeout("")): + error = ResponseHandlingException(exc) + result, _, _ = self.invoke_logs(cli_runner, mock_sync_apis, mock_api_client, temp_dir, side_effect=error) + assert "did not start sending" in result.output, type(exc).__name__ + + @pytest.mark.unit @pytest.mark.cli class TestProjectLogsCommand: diff --git a/th_cli/commands/project.py b/th_cli/commands/project.py index a9c9907..d1f2645 100644 --- a/th_cli/commands/project.py +++ b/th_cli/commands/project.py @@ -19,10 +19,11 @@ from typing import Any import click +from httpx import Timeout, TimeoutException from pydantic import ValidationError from th_cli.api_lib_autogen.api_client import SyncApis -from th_cli.api_lib_autogen.exceptions import UnexpectedResponse +from th_cli.api_lib_autogen.exceptions import ResponseHandlingException, UnexpectedResponse from th_cli.api_lib_autogen.models import ( PICS, BodyImportprojectConfigApiV1ProjectsImportPost, @@ -58,11 +59,18 @@ def project(): pass +# `project logs` asks the backend for an archive of every log row of every execution in the project. The backend +# builds the whole archive before it sends the first byte, so the wait grows with the project and httpx's default 5s +# read timeout is far too short (a project with 12 executions, 36 MB of logs, failed at 5.8s every time). A single +# execution's log gets 120s (TEST_RUN_EXECUTION_IO_TIMEOUT); a project is the sum of its executions. +PROJECT_LOGS_TIMEOUT = Timeout(300.0, connect=10.0) # 300s read/write, 10s connect + + @contextmanager -def get_sync_apis(operation: str): +def get_sync_apis(operation: str, timeout: Timeout | None = None): client = None try: - client = get_client() + client = get_client(timeout=timeout) yield SyncApis(client) except CLIError: raise @@ -333,7 +341,7 @@ def applicable_tests(id: int, json_output: bool) -> None: ) def logs(id: int, grouped: bool, output_file: str | None) -> None: """Download all logs for a project as a single zip archive""" - with get_sync_apis("logs") as sync_apis: + with get_sync_apis("logs", timeout=PROJECT_LOGS_TIMEOUT) as sync_apis: _download_project_logs(sync_apis, id, grouped, output_file) @@ -737,6 +745,15 @@ def _download_project_logs(sync_apis: SyncApis, id: int, grouped: bool, output_f log_bytes: bytes = sync_apis.projects_api.download_project_logs_api_v1_projects__id__logs_get( id=id, grouped=grouped ) + except ResponseHandlingException as e: + if isinstance(e.error, TimeoutException): + # The connection worked, so "is the backend running?" would send the user the wrong way: it is running + # and still building the archive. + raise CLIError( + f"The backend did not start sending the logs of project {id} within {PROJECT_LOGS_TIMEOUT.read:.0f}s. " + "It is reachable, but building the archive of a project with many or large executions takes longer." + ) from e + raise except UnexpectedResponse as e: if e.status_code == 404: if isinstance(e.content, dict): From 87a25dea14349aa93b0beba66331334820892ea0 Mon Sep 17 00:00:00 2001 From: Romulo Quidute Filho Date: Thu, 3 Dec 2026 11:04:49 -0300 Subject: [PATCH 2/3] CLI: report only a read timeout of project logs, in phase-neutral wording --- tests/test_project_commands.py | 15 +++++++++------ th_cli/commands/project.py | 15 +++++++++------ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/tests/test_project_commands.py b/tests/test_project_commands.py index d6d44bc..3188509 100644 --- a/tests/test_project_commands.py +++ b/tests/test_project_commands.py @@ -1111,8 +1111,8 @@ def test_a_read_timeout_is_reported_as_slow_not_as_backend_down( error = ResponseHandlingException(httpx.ReadTimeout("")) result, _, written = self.invoke_logs(cli_runner, mock_sync_apis, mock_api_client, temp_dir, side_effect=error) assert result.exit_code == 1 - assert "did not start sending the logs of project 1 within 300s" in result.output - assert "reachable" in result.output + assert "no data received for 300s" in result.output + assert "accepted the connection" in result.output assert "Is the backend running?" not in result.output # the connection worked assert written == [] # no partial file @@ -1124,15 +1124,18 @@ def test_a_refused_connection_still_gets_the_backend_hint( assert result.exit_code == 1 assert "ConnectError" in result.output assert "Is the backend running?" in result.output - assert "did not start sending" not in result.output + assert "no data received" not in result.output - def test_every_kind_of_httpx_timeout_is_treated_as_a_timeout( + def test_only_a_read_timeout_is_reported_as_slow_archive( self, cli_runner, mock_sync_apis, mock_api_client, temp_dir ): - for exc in (httpx.ReadTimeout(""), httpx.WriteTimeout(""), httpx.PoolTimeout("")): + """Connect, write and pool timeouts do not say the backend accepted the connection and is busy.""" + for exc in (httpx.ConnectTimeout(""), httpx.WriteTimeout(""), httpx.PoolTimeout("")): error = ResponseHandlingException(exc) result, _, _ = self.invoke_logs(cli_runner, mock_sync_apis, mock_api_client, temp_dir, side_effect=error) - assert "did not start sending" in result.output, type(exc).__name__ + assert result.exit_code == 1, type(exc).__name__ + assert "no data received" not in result.output, type(exc).__name__ + assert "accepted the connection" not in result.output, type(exc).__name__ @pytest.mark.unit diff --git a/th_cli/commands/project.py b/th_cli/commands/project.py index d1f2645..a4764bd 100644 --- a/th_cli/commands/project.py +++ b/th_cli/commands/project.py @@ -19,7 +19,7 @@ from typing import Any import click -from httpx import Timeout, TimeoutException +from httpx import ReadTimeout, Timeout from pydantic import ValidationError from th_cli.api_lib_autogen.api_client import SyncApis @@ -746,12 +746,15 @@ def _download_project_logs(sync_apis: SyncApis, id: int, grouped: bool, output_f id=id, grouped=grouped ) except ResponseHandlingException as e: - if isinstance(e.error, TimeoutException): - # The connection worked, so "is the backend running?" would send the user the wrong way: it is running - # and still building the archive. + if isinstance(e.error, ReadTimeout): + # The connection was made, so "is the backend running?" would send the user the wrong way. httpx's read + # timeout runs between chunks of data, so this does not say whether the first byte ever arrived; building + # the archive of a large project is the likely cause, not an established fact. Connect, write and pool + # timeouts do not mean that, and keep the generic transport error below. raise CLIError( - f"The backend did not start sending the logs of project {id} within {PROJECT_LOGS_TIMEOUT.read:.0f}s. " - "It is reachable, but building the archive of a project with many or large executions takes longer." + f"Timed out waiting for the logs of project {id}: no data received for {PROJECT_LOGS_TIMEOUT.read:.0f}s. " + "The backend accepted the connection; building the archive of a project with many or large " + "executions can take longer than that." ) from e raise except UnexpectedResponse as e: From 72baf824dd1dd65abe7bc46dae91a6b5ca8f6ed8 Mon Sep 17 00:00:00 2001 From: Romulo Quidute Filho Date: Thu, 3 Dec 2026 11:05:13 -0300 Subject: [PATCH 3/3] CLI: wrap the project logs timeout message to the line limit --- th_cli/commands/project.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/th_cli/commands/project.py b/th_cli/commands/project.py index a4764bd..ce51215 100644 --- a/th_cli/commands/project.py +++ b/th_cli/commands/project.py @@ -752,7 +752,8 @@ def _download_project_logs(sync_apis: SyncApis, id: int, grouped: bool, output_f # the archive of a large project is the likely cause, not an established fact. Connect, write and pool # timeouts do not mean that, and keep the generic transport error below. raise CLIError( - f"Timed out waiting for the logs of project {id}: no data received for {PROJECT_LOGS_TIMEOUT.read:.0f}s. " + f"Timed out waiting for the logs of project {id}: no data received for " + f"{PROJECT_LOGS_TIMEOUT.read:.0f}s. " "The backend accepted the connection; building the archive of a project with many or large " "executions can take longer than that." ) from e