mirror of
https://github.com/home-assistant/core.git
synced 2026-12-03 04:08:19 -04:00
Ignore B2 upload progress reported after the event loop closes (#187860)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import asyncio
|
||||
from collections.abc import AsyncIterator, Callable, Coroutine
|
||||
from contextlib import suppress
|
||||
import functools
|
||||
import json
|
||||
import logging
|
||||
@@ -167,7 +168,9 @@ class _ProgressListener(AbstractProgressListener): # type: ignore[misc]
|
||||
|
||||
def bytes_completed(self, byte_count: int) -> None:
|
||||
"""Report the number of bytes uploaded so far."""
|
||||
self._hass.loop.call_soon_threadsafe(self._report, byte_count)
|
||||
# The loop is closed if Home Assistant stops during the upload
|
||||
with suppress(RuntimeError):
|
||||
self._hass.loop.call_soon_threadsafe(self._report, byte_count)
|
||||
|
||||
@callback
|
||||
def _report(self, byte_count: int) -> None:
|
||||
|
||||
@@ -22,6 +22,7 @@ from pytest_homeassistant.typing import (
|
||||
|
||||
from homeassistant.components.backblaze_b2.backup import (
|
||||
_parse_metadata,
|
||||
_ProgressListener,
|
||||
async_get_backup_agents,
|
||||
async_register_backup_agents_listener,
|
||||
)
|
||||
@@ -557,6 +558,21 @@ async def test_upload_progress_after_failed_upload(
|
||||
on_progress.assert_not_called()
|
||||
|
||||
|
||||
async def test_upload_progress_after_loop_closed(hass: HomeAssistant) -> None:
|
||||
"""Test that a B2 worker reporting progress after shutdown does not raise."""
|
||||
on_progress = Mock()
|
||||
listener = _ProgressListener(hass, on_progress)
|
||||
|
||||
with patch.object(
|
||||
hass.loop,
|
||||
"call_soon_threadsafe",
|
||||
side_effect=RuntimeError("Event loop is closed"),
|
||||
):
|
||||
listener.bytes_completed(8192)
|
||||
|
||||
on_progress.assert_not_called()
|
||||
|
||||
|
||||
async def test_upload_with_cleanup_and_logging(
|
||||
hass_client: ClientSessionGenerator,
|
||||
mock_config_entry: MockConfigEntry,
|
||||
|
||||
Reference in New Issue
Block a user