fix(downloads): a swarm note change is not activity for the stall timer (#1572)

The torrent stall timer can be deferred for as long as the swarm keeps
changing. Since #1494 the torrent handler appends a note to its progress
message (`0% · 2 seeds`, and `· 0 seeds, giving up in 7m` while the
no-seeders clock runs). `update_download_status` treats any changed
message as activity, so a seed count that goes 2, 3, 1 between polls, or
the give-up countdown ticking down every minute, refreshes
`_last_activity` and the torrent never stalls. A torrent whose seed
count stays constant still stalls, which is why this is easy to miss.

`shelfmark/download/activity.py` already says what counts: a changed
status event or a changed progress value, and a message that changes on
a timer proves nothing. So `update_download_status` now compares
messages through a small `activity_key()` that drops that trailing seed
note. The card still gets every message and shows the live seed count,
only the stall clock ignores a change in the note. A new progress
percentage, a different client message or a status change still counts
as activity.

The handler builds the note in `_seed_note` and `activity_key` has to
match it, so one test builds the note for a range of seed counts and
countdowns and checks the key strips all of them. That also covers the
gap #1494 left, where no test connected a handler's message to the
orchestrator: the other two drive `update_download_status` with real
notes and a fake clock. Without the change the note test fails and the
key tests cannot even import `activity_key`. The other orchestrator test
is a control that a progress or status change still counts. Full suite
passes, and each new test also passes when run alone.
This commit is contained in:
splitsec2 authored and GitHub committed 2026-10-26 00:45:28 -06:00
1 parent d50440f6c2
commit ac14e4dd24
4 files changed
+109 -3

No files matched your search

+15
View File
@@ -35,6 +35,7 @@ uploads, and the Anna's Archive countdown in `release_sources.direct_download` (
refreshes the stall clock on every tick of a loop that proves nothing about the remote).
"""
import re
from collections.abc import Callable
# Seconds without a changed status or progress value before the orchestrator cancels a
@@ -48,6 +49,20 @@ ACTIVITY_GRACE_STATUS = "__activity_grace__"
StatusCallback = Callable[[str, str | None], None]
# The note a torrent handler appends to its progress message: " · 2 seeds", or
# " · 0 seeds, giving up in 7m". It follows the swarm and counts down on a timer, and neither
# is progress, so a change in it alone must not refresh the stall clock. The handler builds
# it in `ExternalClientHandler._seed_note`; a test keeps the two in step.
_SEED_NOTE_SUFFIX = re.compile(r" · \d+ seeds?(?:, giving up in \d+m)?$")
def activity_key(message: str | None) -> str | None:
"""The part of a status message whose change counts as activity."""
if message is None:
return None
return _SEED_NOTE_SUFFIX.sub("", message)
# A status_callback is caller-supplied and may raise; a failed liveness hint must never
# break the operation it was protecting. Mirrors http._STATUS_CALLBACK_ERRORS.
_CALLBACK_ERRORS = (AttributeError, KeyError, OSError, RuntimeError, TypeError, ValueError)
+10 -3
View File
@@ -25,7 +25,7 @@ from shelfmark.core.request_helpers import (
)
from shelfmark.core.utils import is_audiobook as check_audiobook
from shelfmark.core.utils import transform_cover_url
from shelfmark.download.activity import STALL_TIMEOUT_SECONDS, parse_activity_grace
from shelfmark.download.activity import STALL_TIMEOUT_SECONDS, activity_key, parse_activity_grace
from shelfmark.download.fs import run_blocking_io
from shelfmark.download.postprocess.pipeline import is_torrent_source, safe_cleanup_path
from shelfmark.download.postprocess.router import post_process_download
@@ -977,9 +977,16 @@ def update_download_status(book_id: str, status: str, message: str | None = None
with _progress_lock:
status_event = (status_key, message)
if _last_status_event.get(book_id) == status_event:
previous_event = _last_status_event.get(book_id)
if previous_event == status_event:
return
_last_activity[book_id] = time.time()
# A change only in the swarm note beside the progress is shown but is not activity.
if (
previous_event is None
or previous_event[0] != status_key
or activity_key(previous_event[1]) != activity_key(message)
):
_last_activity[book_id] = time.time()
_last_status_event[book_id] = status_event
# Update status message first so terminal snapshots capture the final message
+33
View File
@@ -0,0 +1,33 @@
"""activity_key drops the swarm note the torrent handler adds beside a progress message."""
import pytest
from shelfmark.download.activity import activity_key
def _seed_note(seeds, give_up_in):
# Importing the orchestrator first keeps the handler module out of its import cycle.
import shelfmark.download.orchestrator # noqa: F401
from shelfmark.download.clients.base_handler import ExternalClientHandler
return ExternalClientHandler._seed_note(seeds, give_up_in)
@pytest.mark.parametrize("seeds", [0, 1, 2, 15, 1200])
@pytest.mark.parametrize("give_up_in", [None, 0.0, 30.0, 61.0, 3599.0])
def test_activity_key_strips_whatever_the_handler_appends(seeds, give_up_in):
message = f"42%{_seed_note(seeds, give_up_in)}"
assert activity_key(message) == "42%"
@pytest.mark.parametrize(
"message",
["42%", "Fetching metadata", "Seeding (stalled)", "Paused", "No seeders: 0 found, 1 needed"],
)
def test_activity_key_leaves_other_messages_alone(message):
assert activity_key(message) == message
def test_activity_key_keeps_none():
assert activity_key(None) is None
@@ -78,3 +78,54 @@ def test_update_download_progress_refreshes_activity_when_progress_changes(monke
assert orchestrator._last_activity[book_id] == 40.0
assert orchestrator._last_progress_value[book_id] == 0.5
def _status_pipeline(monkeypatch):
import shelfmark.download.orchestrator as orchestrator
orchestrator._last_activity.clear()
orchestrator._last_progress_value.clear()
orchestrator._last_status_event.clear()
mock_queue = MagicMock()
monkeypatch.setattr(orchestrator, "book_queue", mock_queue)
monkeypatch.setattr(orchestrator, "queue_status", lambda: {})
monkeypatch.setattr(orchestrator, "ws_manager", MagicMock())
clock = {"now": 100.0}
monkeypatch.setattr(orchestrator.time, "time", lambda: clock["now"])
return orchestrator, mock_queue, clock
def test_a_changing_seed_note_is_shown_but_does_not_refresh_the_stall_clock(monkeypatch):
"""The swarm count flaps and the give-up countdown ticks; neither is progress (F1)."""
orchestrator, mock_queue, clock = _status_pipeline(monkeypatch)
from shelfmark.download.clients.base_handler import ExternalClientHandler
note = ExternalClientHandler._seed_note
orchestrator.update_download_status("book", "downloading", f"0%{note(2, None)}")
assert orchestrator._last_activity["book"] == 100.0
for step, (seeds, give_up_in) in enumerate([(3, None), (1, None), (0, 600.0), (0, 540.0)], 1):
clock["now"] = 100.0 + step * 60
orchestrator.update_download_status("book", "downloading", f"0%{note(seeds, give_up_in)}")
# The card followed every change, the stall clock did not move.
assert mock_queue.update_status_message.call_count == 5
assert orchestrator._last_activity["book"] == 100.0
def test_progress_and_status_changes_still_refresh_the_stall_clock(monkeypatch):
orchestrator, _queue, clock = _status_pipeline(monkeypatch)
from shelfmark.download.clients.base_handler import ExternalClientHandler
note = ExternalClientHandler._seed_note
orchestrator.update_download_status("book", "downloading", f"0%{note(2, None)}")
clock["now"] = 200.0
orchestrator.update_download_status("book", "downloading", f"5%{note(2, None)}")
assert orchestrator._last_activity["book"] == 200.0
clock["now"] = 300.0
orchestrator.update_download_status("book", "resolving", f"5%{note(2, None)}")
assert orchestrator._last_activity["book"] == 300.0