mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-24 18:05:07 +00:00
fix(serve): lock shared session state and throttle progress writes
Handlers share one service instance, so a per-session lock keeps two requests out of it at once. Track threads merge and write job state under one lock, and the progress sink coalesces ticks that say the same thing.
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
"""A bounded sub-range that returns no bytes must fail, not spin.
|
||||
|
||||
After a cut, the sequential path asks for bounded sub-ranges and treats a whole
|
||||
sub-range as progress rather than a retry. A response with an empty body of
|
||||
unknown length (a chunked 206, a 204, a 304) makes no progress, so it must
|
||||
raise and spend an attempt, or the same request repeats forever.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import threading
|
||||
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
||||
|
||||
import pytest
|
||||
import requests as rq
|
||||
|
||||
from unshackle.core.constants import DOWNLOAD_CANCELLED
|
||||
from unshackle.core.downloaders.requests import download
|
||||
|
||||
# the package re-exports the `requests` function, shadowing the module of the same name
|
||||
downloader = sys.modules[download.__module__]
|
||||
|
||||
BODY = bytes(range(256)) * 256 # 64 KiB
|
||||
CUT_AFTER = 10 * 1024
|
||||
CHUNK = 16 * 1024
|
||||
DEADLINE = 30
|
||||
|
||||
|
||||
class _EmptyRangeServer(ThreadingHTTPServer):
|
||||
"""Cuts the first body, then answers every bounded range with an empty chunked 206."""
|
||||
|
||||
allow_reuse_address = True
|
||||
daemon_threads = True
|
||||
|
||||
def __init__(self):
|
||||
self.ranges: list[str] = []
|
||||
super().__init__(("127.0.0.1", 0), _EmptyRangeHandler)
|
||||
self.url = f"http://127.0.0.1:{self.server_address[1]}/seg.mp4"
|
||||
|
||||
def handle_error(self, request, client_address): # the client hanging up is expected here
|
||||
pass
|
||||
|
||||
|
||||
class _EmptyRangeHandler(BaseHTTPRequestHandler):
|
||||
protocol_version = "HTTP/1.1"
|
||||
|
||||
def log_message(self, *_):
|
||||
pass
|
||||
|
||||
def do_GET(self):
|
||||
header = self.headers.get("Range")
|
||||
self.server.ranges.append(header)
|
||||
start, end, bounded = 0, len(BODY) - 1, False
|
||||
if header:
|
||||
start_s, _, end_s = header.removeprefix("bytes=").partition("-")
|
||||
start, end = int(start_s), (int(end_s) if end_s else len(BODY) - 1)
|
||||
bounded = bool(end_s)
|
||||
if bounded:
|
||||
# a valid response: the chunked terminator closes a zero-length body, so requests raises nothing
|
||||
self.send_response(206)
|
||||
self.send_header("Content-Range", f"bytes {start}-{end}/{len(BODY)}")
|
||||
self.send_header("Transfer-Encoding", "chunked")
|
||||
self.end_headers()
|
||||
self.wfile.write(b"0\r\n\r\n")
|
||||
return
|
||||
body = BODY[start : end + 1]
|
||||
self.send_response(206 if header else 200)
|
||||
if header:
|
||||
self.send_header("Content-Range", f"bytes {start}-{end}/{len(BODY)}")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.end_headers()
|
||||
# the cut teaches the downloader to bound the requests that follow
|
||||
self.wfile.write(body[:CUT_AFTER])
|
||||
self.close_connection = True
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def server(monkeypatch):
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
monkeypatch.setattr(downloader, "RETRY_WAIT", 0)
|
||||
monkeypatch.setattr(downloader, "REQUEST_CHUNK_SIZE", CHUNK)
|
||||
monkeypatch.setattr(downloader, "MIN_REQUEST_CHUNK", 4 * 1024)
|
||||
monkeypatch.setattr(downloader, "MIN_RESUME_PROGRESS", 1024)
|
||||
server = _EmptyRangeServer()
|
||||
threading.Thread(target=server.serve_forever, daemon=True).start()
|
||||
yield server
|
||||
server.shutdown()
|
||||
server.server_close()
|
||||
DOWNLOAD_CANCELLED.clear()
|
||||
|
||||
|
||||
def test_empty_sub_range_fails_instead_of_looping(tmp_path, server):
|
||||
save_path = tmp_path / "0.mp4"
|
||||
result: list[BaseException] = []
|
||||
|
||||
def run() -> None:
|
||||
try:
|
||||
list(download(url=server.url, save_path=save_path, session=rq.Session()))
|
||||
except BaseException as exc:
|
||||
result.append(exc)
|
||||
|
||||
# a hard deadline: a regression hangs this thread, and the suite must fail, not stall
|
||||
worker = threading.Thread(target=run, daemon=True)
|
||||
worker.start()
|
||||
worker.join(timeout=DEADLINE)
|
||||
|
||||
assert not worker.is_alive(), f"download still running after {DEADLINE}s ({len(server.ranges)} requests)"
|
||||
assert result and isinstance(result[0], IOError), f"expected an IOError, got {result!r}"
|
||||
assert "no bytes" in str(result[0])
|
||||
assert len(server.ranges) <= downloader.MAX_ATTEMPTS + 1
|
||||
assert not save_path.exists()
|
||||
@@ -0,0 +1,55 @@
|
||||
"""Connection pooling survives the no-retry adapter view.
|
||||
|
||||
``download()`` builds this view once per segment. ``copy()`` runs
|
||||
``HTTPAdapter.__setstate__``, which blanks ``proxy_manager`` and re-runs
|
||||
``init_poolmanager()``, so a view that keeps those fresh objects hands every segment an
|
||||
empty pool: one TCP connection and one TLS handshake each. That is invisible on Linux and
|
||||
costs a native Windows host roughly 7x the wall time and 4x the CPU on a manifest of a few
|
||||
thousand segments.
|
||||
|
||||
The second test pins the reason sharing one pool between the two adapters is safe:
|
||||
``HTTPAdapter.send`` passes ``retries=self.max_retries`` per call, so the view's ``Retry(0)``
|
||||
travels with the view and never reaches the caller's own session.
|
||||
"""
|
||||
|
||||
from requests import Session
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util.retry import Retry
|
||||
|
||||
from unshackle.core.downloaders.requests import no_retry_session
|
||||
|
||||
|
||||
def pooled_session(total: int = 5) -> Session:
|
||||
session = Session()
|
||||
adapter = HTTPAdapter(pool_connections=16, pool_maxsize=16, pool_block=True, max_retries=Retry(total=total))
|
||||
session.mount("https://", adapter)
|
||||
session.mount("http://", adapter)
|
||||
return session
|
||||
|
||||
|
||||
def test_view_shares_the_pools_it_was_copied_from() -> None:
|
||||
session = pooled_session()
|
||||
view = no_retry_session(session)
|
||||
|
||||
for prefix, adapter in session.adapters.items():
|
||||
twin = view.adapters[prefix]
|
||||
assert twin is not adapter, "the view must be its own adapter, or Retry(0) lands on the caller"
|
||||
assert twin.poolmanager is adapter.poolmanager
|
||||
assert twin.proxy_manager is adapter.proxy_manager
|
||||
|
||||
|
||||
def test_view_never_retries_and_leaves_the_caller_retrying() -> None:
|
||||
session = pooled_session(total=5)
|
||||
view = no_retry_session(session)
|
||||
|
||||
assert view.adapters["https://"].max_retries.total == 0
|
||||
assert session.adapters["https://"].max_retries.total == 5, "the caller's retry policy must survive"
|
||||
|
||||
|
||||
def test_repeated_views_keep_reusing_one_pool() -> None:
|
||||
"""``download()`` rebuilds the view per segment; every rebuild must land on the same pool."""
|
||||
session = pooled_session()
|
||||
origin = session.adapters["https://"].poolmanager
|
||||
|
||||
for _ in range(3):
|
||||
assert no_retry_session(session).adapters["https://"].poolmanager is origin
|
||||
Reference in New Issue
Block a user