Repository navigation
fix(downloads): stop preparing a file once no download needs it - #4085
Conversation
Deleting, revoking or re-qualifying the last download of a prepared file now expires its queued or running job and stops the encode. The preparation run also retires stale jobs no in-flight download needs, and the claim query skips them. A create that linked a job just before it was retired queues it again when its link is confirmed. Refs Silo-Server#998
A retired running job keeps its id and output path. Keep an unowned lease for the stopped worker's heartbeat window, as a pause does, and keep it through a requeue, so a prepare-again cannot share the output path with an encode still shutting down. Document the retirement lifecycle.
|
Warning Review limit reachedThis organization requires a seat for paid usage-based reviews, including automatic and on-demand reviews. Ask an admin to assign a seat or enable paid usage-based reviews for users without a seat in Billing. Next included review available in 33 minutes. View limit details
|
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
…completion Review fixes for retiring preparations no download needs: - A heartbeat now fails for a job no in-flight download has needed for ten minutes. The preparation run that holds the sweep waits for its replica's running encodes, so on a single server an orphaned encode ran to the end. - A late completed report retires the job as a delete does. A running job used to keep encoding and was killed by the sweep ten minutes later on a cluster only. - Requeue moves last_used_at to now, so a file asked for again is not skipped by the claim query or retired before its download row lands. - ConfirmArtifactLink locks the job FOR NO KEY UPDATE. With FOR SHARE, two creates confirming the same retired job deadlocked. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A worker on another replica that finishes a retired job before its next heartbeat is refused the ready transition. Its cleanup removed the file only when the job row was gone, and retirement keeps the row, so the whole file stayed on disk with nothing tracking it. An expired row has no attempt writing its path, so the cleanup now removes it too. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Quick104
left a comment
There was a problem hiding this comment.
🤖 Claude Code (claude-opus-5-5), reviewing on behalf of a maintainer, with a cross-provider pass by Codex (gpt-6.1-sol).
The approach holds. On main, deleting the last download of a running job leaves the encode running; on this head it stops. I checked that with a test written against main, because the PR's own new tests don't compile there (the ConfirmArtifactLink signature changed).
Five findings are below, each on its line. I'm fixing all five on this branch, and each fix comes with a test that fails on this head. One more point needs a maintainer decision rather than a code change. It is on preparation_controls.go line 178.
| FOR UPDATE | ||
| ) | ||
| UPDATE download_artifacts a SET `+expireLocatorAssignment+`, paused_at = NULL, | ||
| lease_expires_at = CASE WHEN t.running THEN now() + make_interval(secs => $2) |
There was a problem hiding this comment.
Maintainer decision: retirement during a rolling upgrade. On main, Requeue sets lease_expires_at = NULL. Suppose a new replica retires a running job, and within the stopped worker's heartbeat window an older replica requeues it for a new download. The unowned lease is then cleared, and a worker can claim the job while the stopped encode still writes the same output path (it includes the job id). This happens only while replicas run mixed versions, and only inside a window of up to the 40-second heartbeat interval. Single-server installs are unaffected. The options are to accept it, or to release lease preservation before retirement turns on. That is a maintainer call, so I'm leaving the code as is.
Silo Kody — review completeReview finished. Check the inline comments for findings and verify each suggestion against the code and tests. Reviewing changes in Silo
Review settingsReview OptionsThe following review options are enabled or disabled:
|
|
🤖 Claude Code (claude-opus-5-5): review summary for head 1aa1726, with a cross-provider pass by Codex (gpt-6.1-sol). The fix works. On the base, deleting the only download of a running job leaves the encode running. On this branch the delete stops it. I checked that with a test written against the base, because the PR's own new tests don't compile there ( Addressed on this branchFive findings, fixed in 4ed929f and 1aa1726. Each test below failed on 672e117 before its fix.
Left for the maintainers
Validated featuresThe validation board couldn't be read here; the token lacks Manual and clientsThe user manual needs an update. No client shows anything new. Apple, Android, Roku and web need no change, and jellycompat has no server-prepared downloads. The evidence page still matches this head, because the fixes don't change the delete and queue behavior it shows. Fit: 9/10 · Readiness: 8/10 — conditional (assuming CI is green) Fit: this stops transcodes that nobody will receive, which matters most on low-power servers. It adds no setting or API surface. Readiness: the main behavior failed on the base and passes here, and each review fix has a test that failed first. The rolling-upgrade decision and the board check are still open. |
Problem
Closes #998
Related issue: N/A
Validation tasks: none (the server downloads task #1168 has no recorded results)
Other clients: Apple N/A (no client-visible change); Android N/A (no client-visible change); Roku N/A (no client-visible change)
When a user deletes a download while the server is still preparing its file, the transcode keeps running to the end, and a job still waiting in the queue starts later anyway. Nobody receives the result, but it costs CPU or GPU time, source reads and preparation slots. This change stops a queued or running preparation as soon as its last waiting download is gone. A job that another download still waits on keeps going.
#1468 already stopped missing-output recovery from rebuilding unused ready files. This PR covers the rest of #998: work that is queued or running when its last download is deleted, revoked or moved to another quality.
Approach
A job no in-flight download needs is retired: the row becomes
expired, as #1468 does for unused ready files, and keeps its recipe. Deleting a download, an administrator revoke, a managed quality change, and a latecompletedreport retire the old job right away. The check that no other in-flight download needs the job runs in the same statement. An encode running on the same replica stops at once. One running on another replica or a transcode node stops at its next heartbeat, because the heartbeat only renews a running job.Jobs that no in-flight download needs and that nothing has used for ten minutes count as abandoned. The claim query skips them, their worker's next heartbeat fails, and the preparation run, every 30 seconds, retires them. The heartbeat matters on a busy server, whose preparation run waits for its own running encodes. Asking for an expired file again counts as a use. This catches downloads removed some other way, such as a deleted profile or monitor, and orphaned jobs left by earlier versions. The ten minutes leave room for a create that has queued its job but not yet inserted its download row.
The job keeps its id and output path, so a retired running job keeps an unowned lease for the stopped worker's heartbeat window, as a pause does. If someone asks for the same file again within that window, the job is queued, but no worker claims it until the window ends. A new encode therefore never writes to the output path while the old one is still shutting down.
A create can read a job just before it is retired and then link its download to the expired row. Confirming the link, which every create path already does, queues the job again and starts preparation. Revoke used to delete such a job, which failed that racing download. It now uses the same retirement.
Validation
go build ./...,gofmt -l,go mod tidy -diff,go vet ./...,make lint-changed,make verify-local-paths,make verify-route-inventory,make verify-size-ratchet: clean.make test-go: pass.go test ./internal/downloads/... ./internal/apiv2/...against a freshly migrated Postgres: pass. Two new DB tests, listed inscripts/ci/db-contracts.txt, cover these cases: deleting the only download of a running job stops its local attempt and fences its heartbeat; a queued job is retired; a job shared with another download keeps preparing; a download linked after retirement queues the job again; a re-request does not claim a retired running job inside the stop window; stale orphaned jobs, queued or with a dead worker, are skipped by the claim query and retired by the preparation run, while a needed job and a just-queued job are not.completedreport stops the job (TestLateCompletionStopsItsPreparationDB, inscripts/ci/db-contracts.txt); a requeued file is not skipped as abandoned; a retired job's finished local file is removed.ConfirmArtifactLinklocksFOR NO KEY UPDATE, which avoids a deadlock reproduced in Postgres withFOR SHARE.Evidence
Checked on a local build with a 15-minute synthetic 1080p movie and
download.max_concurrent_prepares = 1. Two devices requested managed transcoded downloads, 2 Mbps and 1 Mbps, throughPOST /api/v2/downloads. The first job was encoding and the second was queued. Both downloads were then deleted withDELETE /api/v2/downloads/{id}(204).Before, on
origin/mainat 85d1c16: the running ffmpeg kept going, the queued job started after it, and both finished with no download left.After, on this branch, same steps and data: both jobs were retired within the delete requests, ffmpeg exited, and the
.partfile was removed.Neighbouring behaviour on the same build: with two devices on one job, deleting one left the encode running, and the other download became
ready. Asking for the deleted file again queued the expired job, waited out the stop window (about 100 s left), and then prepared it toready.Risks
Checklist
AI Disclosure
Note
Retire artifact preparations once no in-flight download needs them
RetireAbandonedPreparationsandRetireStaleAbandonedPreparationsin preparation_controls.go. These expire queued or running jobs with no in-flight requester instead of deleting them, keeping the recipe and a lease window so a late link can safely requeue the job.ClaimNextandHeartbeatin artifact_repo.go now skip or stop extending leases for stale abandoned jobs (10-minute grace), so workers stop work nobody needs.Service.Delete,Service.RevokeDeviceDownloads, managed replacement, and completion reporting in service.go and status_events.go call the new retirement path when the last requester goes away.Repository.ConfirmArtifactLinkin repo.go requeues an expired artifact when a still-preparing linked download needs it, and returns a requeued flag that triggers an artifact drain.CancelAbandonedPreparationsdeletion flow; removed download-triggered retirement, heartbeat fencing, and claim exclusion are covered by new database tests in preparation_controls_test.go.CancelPreparationsno longer applies the abandoned-job filter;ConfirmArtifactLinkreturns a thirdrequeuedresult, so callers must use the updated signature.Macroscope summarized 1aa1726.