fix(tracks): keep segment resume across an hls merge failure and a locked partial on windows

This commit is contained in:
imSp4rky
2026-11-28 20:26:40 -07:00
parent 37357673fe
commit 74fa29b6f1
5 changed files with 212 additions and 38 deletions
+49 -2
View File
@@ -140,7 +140,7 @@ def test_existing_stash_target_is_left_alone(temp_root, monkeypatch):
target.mkdir(parents=True)
(target / "old.mp4").write_bytes(b"old")
save_dir = task_dir / SEG_NAME
save_dir.mkdir()
open_store(save_dir)
(save_dir / "new.mp4").write_bytes(b"new")
store(save_dir).abandon()
@@ -168,7 +168,7 @@ def test_restore_is_a_noop_without_stash_or_with_present_dir(temp_root, monkeypa
assert not (save_dir / "a.mp4").exists()
def test_rename_failure_never_replaces_download_error(temp_root, monkeypatch):
def test_move_failure_never_replaces_download_error(temp_root, monkeypatch):
monkeypatch.setattr(config, "continue_downloads", True)
def deny(self, target):
@@ -176,6 +176,7 @@ def test_rename_failure_never_replaces_download_error(temp_root, monkeypatch):
with task_temp_dir():
monkeypatch.setattr(Path, "rename", deny)
monkeypatch.setattr(Path, "replace", deny)
with pytest.raises(RuntimeError, match="simulated crash"):
run_download(monkeypatch, failing_parser)
@@ -278,3 +279,49 @@ def test_stash_refreshes_mtime(temp_root, monkeypatch):
target = temp_root / RESUME_STASH / SEG_NAME
assert time.time() - target.stat().st_mtime < 60
def test_a_partial_still_open_does_not_block_the_stash(temp_root, monkeypatch):
"""On Windows a worker that still writes a partial file after the failure blocks the rename of its directory.
The directory patches emulate that, so a stash that renames the directory finds nothing stashed.
"""
monkeypatch.setattr(config, "continue_downloads", True)
rename, replace, unlink = Path.rename, Path.replace, Path.unlink
def refuse_dir(move):
def windows(self, target):
if self.is_dir():
raise PermissionError(5, "Access is denied", str(self))
return move(self, target)
return windows
def locked(self, missing_ok=False):
if self.name.endswith(".!dev"):
raise PermissionError(32, "being used by another process", str(self))
unlink(self, missing_ok=missing_ok)
with task_temp_dir(), pytest.MonkeyPatch.context() as windows_fs:
windows_fs.setattr(Path, "rename", refuse_dir(rename))
windows_fs.setattr(Path, "replace", refuse_dir(replace))
windows_fs.setattr(Path, "unlink", locked)
with pytest.raises(RuntimeError):
run_download(monkeypatch, failing_parser)
stashed = temp_root / RESUME_STASH / SEG_NAME
assert store(stashed).sidecar.exists()
assert not list(stashed.rglob("*.!dev"))
seen = {}
def second_parser(track, ctx):
seen["bytes"] = (ctx.save_dir / "0.mp4").read_bytes()
seen["reused"] = open_store(ctx.save_dir)
ctx.save_path.write_bytes(b"output")
track.path = ctx.save_path
with task_temp_dir():
run_download(monkeypatch, second_parser)
assert seen == {"bytes": b"complete segment", "reused": True}