mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-24 09:55:12 +00:00
fix(dl): end a cancelled track by raising instead of returning short
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""Cancel/abort hardening for the downloader.
|
||||
|
||||
Pins two teardown-safety invariants:
|
||||
Pins three teardown-safety invariants:
|
||||
|
||||
- ``dispatch_parts`` must NOT finalize when the process-global ``DOWNLOAD_CANCELLED``
|
||||
fires mid-download: part workers return silently keeping partials, so every future
|
||||
@@ -9,6 +9,10 @@ Pins two teardown-safety invariants:
|
||||
- A worker parked in the retry backoff wait (which can reach MAX_BACKOFF) must wake and
|
||||
exit as soon as the batch abort is set, instead of sleeping out the full delay and
|
||||
stalling shutdown or the merge.
|
||||
- A segment batch that starts with a cross-track cancel set, or meets one mid-flight, must
|
||||
end by raising ``DownloadCancelled`` in adaptive mode as well as fixed. Adaptive's tail
|
||||
window holds back boost candidates, and the cancel keeps ``seg_done`` short, so without
|
||||
the raise it busy-loops forever on a batch it will never finish.
|
||||
"""
|
||||
|
||||
import importlib
|
||||
@@ -19,6 +23,8 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
import pytest
|
||||
from requests import Session
|
||||
|
||||
from unshackle.core.constants import DownloadCancelled
|
||||
|
||||
dl = importlib.import_module("unshackle.core.downloaders.requests")
|
||||
|
||||
pytestmark = pytest.mark.unit
|
||||
@@ -143,3 +149,27 @@ def test_backoff_wait_exits_promptly_on_batch_abort(server, tmp_path, monkeypatc
|
||||
assert not worker.is_alive()
|
||||
# generous bound: far below the 30s nap, so the wait was interrupted, not slept out
|
||||
assert time.monotonic() - start < 10
|
||||
|
||||
|
||||
@pytest.mark.parametrize("adaptive", [False, True])
|
||||
def test_cancelled_batch_raises_instead_of_finishing_or_hanging(server, tmp_path, adaptive):
|
||||
dl.DOWNLOAD_CANCELLED.set()
|
||||
urls = [f"{url(server)}?i={i}" for i in range(40)]
|
||||
result: list = []
|
||||
|
||||
def run() -> None:
|
||||
try:
|
||||
for _ in dl.requests(urls, tmp_path, "{i}.mp4", max_workers=16, adaptive=adaptive):
|
||||
pass
|
||||
result.append(None)
|
||||
except BaseException as exc: # noqa: BLE001 (the type is the assertion)
|
||||
result.append(exc)
|
||||
|
||||
worker = threading.Thread(target=run, daemon=True)
|
||||
worker.start()
|
||||
worker.join(timeout=15)
|
||||
|
||||
assert not worker.is_alive(), "requests() never returned on a cancelled batch"
|
||||
assert isinstance(result[0], DownloadCancelled)
|
||||
# returning short here would hand the caller an empty segment dir to merge
|
||||
assert not list(tmp_path.iterdir())
|
||||
|
||||
@@ -26,6 +26,8 @@ from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
import pytest
|
||||
|
||||
from unshackle.core.constants import DownloadCancelled
|
||||
|
||||
dl = importlib.import_module("unshackle.core.downloaders.requests")
|
||||
|
||||
SEG_REPEAT = 32 # 8-digit index * 32 = 256-byte body, distinct per index
|
||||
@@ -139,11 +141,12 @@ def test_multiprocess_cancel_terminates_children(server, tmp_path):
|
||||
baseline = len(multiprocessing.active_children())
|
||||
start = time.time()
|
||||
progressed = 0
|
||||
for event in dl.requests(urls, output_dir=tmp_path, filename="seg_{i:04}.bin", max_workers=4, processes=2):
|
||||
if event.get("advance") or event.get("written"):
|
||||
progressed += 1
|
||||
if progressed >= 2:
|
||||
dl.DOWNLOAD_CANCELLED.set() # sibling-track cancel: parent must tear the children down
|
||||
with pytest.raises(DownloadCancelled):
|
||||
for event in dl.requests(urls, output_dir=tmp_path, filename="seg_{i:04}.bin", max_workers=4, processes=2):
|
||||
if event.get("advance") or event.get("written"):
|
||||
progressed += 1
|
||||
if progressed >= 2:
|
||||
dl.DOWNLOAD_CANCELLED.set() # sibling-track cancel: parent must tear the children down
|
||||
elapsed = time.time() - start
|
||||
|
||||
# cancel stopped the work: nowhere near all segments finished, and no hang
|
||||
|
||||
Reference in New Issue
Block a user