From 482ad6fbee4385eb06ea584be71e4190d06f0061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Mon, 16 Dec 2024 19:12:15 +0100 Subject: [PATCH] Increase backup upload timeout (#132990) --- homeassistant/components/cloud/backup.py | 5 +++-- tests/components/cloud/test_backup.py | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/cloud/backup.py b/homeassistant/components/cloud/backup.py index 2c7cc9d7bd5..d394daa7dc5 100644 --- a/homeassistant/components/cloud/backup.py +++ b/homeassistant/components/cloud/backup.py @@ -7,7 +7,7 @@ from collections.abc import AsyncIterator, Callable, Coroutine import hashlib from typing import Any, Self -from aiohttp import ClientError, StreamReader +from aiohttp import ClientError, ClientTimeout, StreamReader from hass_nabucasa import Cloud, CloudError from hass_nabucasa.cloud_api import ( async_files_delete_file, @@ -151,9 +151,10 @@ class CloudBackupAgent(BackupAgent): details["url"], data=await open_stream(), headers=details["headers"] | {"content-length": str(backup.size)}, + timeout=ClientTimeout(connect=10.0, total=43200.0), # 43200s == 12h ) upload_status.raise_for_status() - except ClientError as err: + except (TimeoutError, ClientError) as err: raise BackupAgentError("Failed to upload backup") from err async def async_delete_backup( diff --git a/tests/components/cloud/test_backup.py b/tests/components/cloud/test_backup.py index d5dc8751d82..ac0ef1826de 100644 --- a/tests/components/cloud/test_backup.py +++ b/tests/components/cloud/test_backup.py @@ -372,6 +372,7 @@ async def test_agents_upload( assert f"Uploading backup {backup_id}" in caplog.text +@pytest.mark.parametrize("put_mock_kwargs", [{"status": 500}, {"exc": TimeoutError}]) @pytest.mark.usefixtures("cloud_logged_in", "mock_list_files") async def test_agents_upload_fail_put( hass: HomeAssistant, @@ -379,6 +380,7 @@ async def test_agents_upload_fail_put( caplog: pytest.LogCaptureFixture, aioclient_mock: AiohttpClientMocker, mock_get_upload_details: Mock, + put_mock_kwargs: dict[str, Any], ) -> None: """Test agent upload backup fails.""" client = await hass_client() @@ -395,7 +397,7 @@ async def test_agents_upload_fail_put( protected=True, size=0.0, ) - aioclient_mock.put(mock_get_upload_details.return_value["url"], status=500) + aioclient_mock.put(mock_get_upload_details.return_value["url"], **put_mock_kwargs) with ( patch(