fix(editor): serialize rerenders per job and re-validate after snapping
- Per-job asyncio lock around /api/clip/rerender (same pattern as _restore_locks): concurrent saves raced on the metadata.json read-modify-write, with the last writer silently reverting the other clip's recipe and video_url. - Re-run normalize_segments after snap_to_words + clamp: a segment fully outside the canonical range used to clamp to an inverted (end < start) window that passed within_range and died in ffmpeg as a 500; it is now a clean 400.
This commit is contained in:
parent
18654eabf6
commit
e381d21b54
2 changed files with 35 additions and 0 deletions
20
app.py
20
app.py
|
|
@ -2272,6 +2272,11 @@ class RerenderRequest(BaseModel):
|
|||
reapply_captions: bool = True
|
||||
|
||||
|
||||
# One lock per job (same pattern as _restore_locks): rerenders on the same job
|
||||
# share metadata.json and the canonical files, so they must not interleave.
|
||||
_rerender_locks: Dict[str, asyncio.Lock] = {}
|
||||
|
||||
|
||||
@app.post("/api/clip/rerender")
|
||||
async def rerender_clip(req: RerenderRequest, request: Request):
|
||||
"""Re-render a clip from an edited EDL (the clip editor's save button).
|
||||
|
|
@ -2291,6 +2296,16 @@ async def rerender_clip(req: RerenderRequest, request: Request):
|
|||
job = jobs[req.job_id]
|
||||
await _assert_job_owner(request, job)
|
||||
|
||||
# Serialize rerenders per job: concurrent saves (easy for an MCP agent to
|
||||
# produce) would otherwise race on the shared metadata.json
|
||||
# read-modify-write below, with the last writer silently reverting the
|
||||
# other clip's recipe/video_url.
|
||||
lock = _rerender_locks.setdefault(req.job_id, asyncio.Lock())
|
||||
async with lock:
|
||||
return await _rerender_locked(req, request, job)
|
||||
|
||||
|
||||
async def _rerender_locked(req: RerenderRequest, request: Request, job):
|
||||
output_dir = os.path.join(OUTPUT_DIR, req.job_id)
|
||||
json_files = glob.glob(os.path.join(output_dir, "*_metadata.json"))
|
||||
if not json_files:
|
||||
|
|
@ -2327,6 +2342,11 @@ async def rerender_clip(req: RerenderRequest, request: Request):
|
|||
{"start": round(max(s['start'], canonical_range['start']), 3),
|
||||
"end": round(min(s['end'], canonical_range['end']), 3)}
|
||||
for s in segments]
|
||||
# Re-validate after snapping/clamping: a segment fully outside the
|
||||
# canonical range clamps to an inverted (end < start) window, and
|
||||
# snapping can stretch the total past the cap. Without this it
|
||||
# reaches ffmpeg and dies as a 500 instead of a clean 400.
|
||||
segments = recut.normalize_segments(segments, source_duration)
|
||||
except recut.RecutError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
|
|
|
|||
|
|
@ -160,6 +160,21 @@ class TestRerenderValidation:
|
|||
assert resp.status_code == 409
|
||||
assert fake_recut == []
|
||||
|
||||
def test_snap_clamp_inverted_segment_400_not_500(self, job, fake_recut,
|
||||
monkeypatch):
|
||||
# No source + snap_to_words: a segment fully outside the canonical
|
||||
# range clamps to an inverted (end < start) window. That must be
|
||||
# rejected as a 400 by the post-snap re-validation, never reach
|
||||
# ffmpeg as `-ss 150 -to 30` and surface as a 500.
|
||||
os.remove(job["dir"] / "src.mp4")
|
||||
monkeypatch.setattr(recut, "snap_segments",
|
||||
lambda segments, transcript, bound: segments)
|
||||
resp = _request("POST", "/api/clip/rerender", {
|
||||
"job_id": JOB_ID, "clip_index": 0, "snap_to_words": True,
|
||||
"segments": [{"start": 200, "end": 210}]})
|
||||
assert resp.status_code == 400
|
||||
assert fake_recut == []
|
||||
|
||||
|
||||
class TestRerenderPaths:
|
||||
def test_fast_path_cuts_from_canonical_clip(self, job, fake_recut):
|
||||
|
|
|
|||
Loading…
Reference in a new issue