fix(files): review round 3, the server — a fetched locked re-read, a finish that follows its file, an eviction that counts again

- set_retention and delete_own re-read the folder under its lock and use that row (an unfetched ORM
  result never refreshes the object); a folder gone meanwhile is refused (409), and the route audits
  the retention actually replaced
- a finishing download is stamped only while it is still in the folder (and keep) it was read with:
  a move or a folder delete landing meanwhile sends the read round again, so no file is done with no
  expiry
- the eviction pass counts the disk before each candidate and skips one asked for since it began
- docs: source builds get their own volume-copy snippet
- race tests assert the lock wait; one built on a committing delete never raced
This commit is contained in:
2026-10-13 03:15:59 +02:00
parent 5bb101e493
commit c9baf68b16
6 changed files with 261 additions and 53 deletions
+12 -13
View File
@@ -276,8 +276,7 @@ def run_download_gc(db: Session) -> dict:
# fetch again) and never a file anyone kept; an evicted file leaves every holder's library.
cap = sysconfig.get_int(db, "download_total_max_bytes")
if cap > 0:
total = _on_disk(db)
if total > cap:
if _on_disk(db) > cap:
kept_assets = select(DownloadJob.asset_id).where(
DownloadJob.kept.is_(True), DownloadJob.asset_id.is_not(None)
)
@@ -296,21 +295,21 @@ def run_download_gc(db: Session) -> dict:
)
db.rollback()
for asset_id in candidate_ids:
if total <= cap:
# Counted afresh before each: a file deleted since (this one, an upload, a candidate
# further down) has freed its space, and no other user's file goes for that space.
if _on_disk(db) <= cap:
break
# Locked and re-read like an expiry: a holder may have kept it, or every holder
# deleted it since the SELECT.
# Locked and re-read like an expiry: since the SELECT a holder may have kept it,
# every holder deleted it, or someone asked for it again (a fresh cache hit).
asset, holders = _relock_evictable(db, asset_id)
if asset is None or asset.status != "ready":
# Its file went (or is being fetched afresh) since the sum: the disk is counted
# again, or the pass would evict another user's file for space already free.
db.commit()
total = _on_disk(db)
continue
if any(j.kept for j in holders):
if (
asset is None
or asset.status != "ready"
or any(j.kept for j in holders)
or (asset.last_access_at is not None and asset.last_access_at > now)
):
db.commit()
continue
total -= asset.size_bytes or 0
for job in holders:
_notify(db, job, asset, "download_evicted")
db.delete(job)