clipwright/tests/test_probe_noplaylist.py
juancarlos.cavero 60fa37a969 fix(gpu,proxy): release ASR models in the API, probe one video not the playlist
Three failures measured in prod on 17-sep-2026, one deploy.

1. 5 of 12 jobs failed with "ffmpeg could not cut clip ... exit 187, 0 bytes".
   Exit 187 is AVERROR_EXIT after "h264_nvenc: Could not open encoder":
   VRAM was full. The uvicorn process itself held 7.7 GB of the 20 GB card
   while idle: the thumbnail studio and /api/subtitle on a dubbed clip
   transcribe in-process, and the whisper (ctranslate2) and parakeet
   (onnxruntime CUDA) singletons then lived in the API for good. Both call
   transcribe_backends.release_models() now; the loaders fetch their model
   inside the ASR gate so a release can never pull it from under a running
   transcription.

2. 26 of the 37 probes that reached the per-GB proxy in 10 days were
   `watch?v=X&list=...` or mix links. Neither the probe nor the download set
   `noplaylist`, so yt-dlp walked the whole list, died on its first private /
   age-gated / bot-checked entry (a video nobody pasted), the probe read it
   as an IP problem and paid the proxy to walk the same list again, and the
   user got a 400 for a valid link. Reproduced in the prod container: the
   same URL with noplaylist answers in 9.6 s on a static. The probe also
   keeps every attempt's error per route now, so the anonymous retry's
   bot-check cannot overwrite a "confirm your age" from the cookie attempt
   into an escalation.

3. noplaylist does nothing for a search or a bare playlist URL: one
   `results?search_query=` held the probe thread for 2220 s in the container
   and still failed. Search, playlist, channel and feed URLs are refused by
   path before any request, with a message that says what to paste.
2026-09-17 17:19:57 +02:00

123 lines
4.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""The duration probe asks yt-dlp for ONE video, and a content verdict wins.
Two probe failures measured in prod between 7 and 17-sep-2026 (proxy_usage,
source='probe'): 26 of the 37 probes that reached the per-GB proxy were for a
`watch?v=X&list=...` or a `results?search_query=` URL. Without ``noplaylist``
yt-dlp walked the whole list, died on its first private / age-gated /
bot-checked entry (a video the user never pasted), the probe read that as an
IP problem, paid the proxy to walk the same list again, and the user got a
400 for a valid link.
The second: on a static route the attempt with the cookies said "Sign in to
confirm your age" (a content error, same on every IP) and the anonymous retry
said "not a bot"; only the last error was kept per route, so the bot-check
overrode the verdict and the paid proxy was spent on an age-gated video,
twice in 15 minutes.
"""
import sys
import types
import pytest
metering = pytest.importorskip("cloud.metering")
URL = "https://www.youtube.com/watch?v=3QFAEqE9-Kk&list=PLArw552Yzuwn&rco=1"
NETSCAPE = "# Netscape HTTP Cookie File\n.youtube.com\tTRUE\t/\tTRUE\t0\tSID\tx\n"
AGE = "ERROR: [youtube] 3QFAEqE9-Kk: Sign in to confirm your age. Use --cookies"
BOT = "ERROR: [youtube] 3QFAEqE9-Kk: Sign in to confirm you’re not a bot."
def _fake_ytdl(monkeypatch, attempts, outcome):
"""``outcome(opts)`` returns an info dict or raises."""
class _YDL:
def __init__(self, opts):
self.opts = opts
def __enter__(self):
return self
def __exit__(self, *a):
return False
def extract_info(self, url, download=False):
attempts.append(dict(self.opts))
return outcome(self.opts)
monkeypatch.setitem(sys.modules, "yt_dlp", types.SimpleNamespace(YoutubeDL=_YDL))
@pytest.fixture(autouse=True)
def _env(monkeypatch):
import security_utils
monkeypatch.setattr(security_utils, "assert_public_url", lambda u: u)
monkeypatch.setenv("STATIC_PROXY_URLS", "http://s1,http://s2")
monkeypatch.setenv("PROXY_URL", "http://paid")
monkeypatch.setenv("YOUTUBE_COOKIES", NETSCAPE)
monkeypatch.setenv("BGUTIL_BASE_URL", "http://127.0.0.1:4416")
monkeypatch.delenv("DIRECT_FIRST", raising=False)
# No network: the last-resort ffprobe must not run either.
monkeypatch.setattr(metering, "_ffprobe_url_seconds", lambda url, timeout=30: 0.0)
metering.pop_paid_probe_events()
yield
metering.pop_paid_probe_events()
class TestOneVideoNotThePlaylist:
def test_every_attempt_asks_for_the_single_video(self, monkeypatch):
attempts = []
_fake_ytdl(monkeypatch, attempts,
lambda o: {"extractor": "youtube", "duration": 600})
assert abs(metering.probe_url_minutes(URL) - 10) < 1e-6
assert attempts and all(a.get("noplaylist") is True for a in attempts)
class TestAContentVerdictOutranksTheAnonymousBotCheck:
def _outcome(self, opts):
# With the cookies: age-gated (same on every IP). Anonymous: bot-check.
raise RuntimeError(AGE if opts.get("cookiefile") else BOT)
def test_the_paid_proxy_is_not_spent(self, monkeypatch):
attempts = []
_fake_ytdl(monkeypatch, attempts, self._outcome)
with pytest.raises(ValueError):
metering.probe_url_minutes(URL)
assert not any(a.get("proxy") == "http://paid" for a in attempts)
assert metering.pop_paid_probe_events() == []
def test_both_errors_are_kept_for_the_alert(self, monkeypatch):
attempts = []
_fake_ytdl(monkeypatch, attempts, self._outcome)
seen = {}
real = metering._probe_with_proxies
def spy(url, proxies, strategies, static_errors, paid, ck_path):
try:
return real(url, proxies, strategies, static_errors, paid, ck_path)
finally:
seen.update(static_errors)
monkeypatch.setattr(metering, "_probe_with_proxies", spy)
with pytest.raises(ValueError):
metering.probe_url_minutes(URL)
assert "confirm your age" in seen["static1"]
assert "not a bot" in seen["static1"]
assert not metering.static_failure_warrants_paid(seen["static1"])
class TestAnIpProblemStillEscalates:
def test_bot_check_on_every_attempt_reaches_the_paid_proxy(self, monkeypatch):
attempts = []
def outcome(opts):
if opts.get("proxy") == "http://paid":
return {"extractor": "youtube", "duration": 60}
raise RuntimeError(BOT)
_fake_ytdl(monkeypatch, attempts, outcome)
assert abs(metering.probe_url_minutes(URL) - 1) < 1e-6
assert attempts[-1]["proxy"] == "http://paid"