The 72-Hour Rollback Window That Expired on Arrival: 86 of 118 Files
The Window Was Zero Hours Long
Replacing a transcode in place is the most dangerous write in a media pipeline, because the file being deleted is often the only copy of the original. Mine does it in two phases: the replacement is encoded to a temporary name and verified, then the existing file is retired by a same-volume rename into a .transcode_trash/<date>/ directory beside it, and only after the new file is published does the original become collectable. If the publish step fails, the rename is reversed and the original is back where it was.
The collectable part is the promise: an original sits in quarantine for 72 hours. Inside that window, a bad encode, a wrong crop, a subtitle that landed in the wrong track — all of it is reversible with a move. Outside it, the original is gone and the only recovery is another encode, which is not the same file.
On 2026-11-10 I asked the garbage collector a question it had never been asked: for each entry currently sitting in quarantine, how much of that 72 hours is actually left? The answer, measured across the library:
| Quarantined originals | State at the moment of arrival |
|---|---|
| 118 | — |
| 86 | already past the cutoff: 0 hours of the promised 72 |
| 32 | a genuine window, measured from their own mtime |
Seventy-three percent of the files in a directory whose entire purpose is recoverability were recoverable only until the next sweep. Nothing had been lost yet — the number came from comparing two timestamps, not from a post-mortem — but 86 originals were one hourly sweep away from becoming unrecoverable, and the sweep had no idea anything was wrong.
Two Clocks, and Only One of Them Is Custody
The installer retires an original with os.replace(src, dest). That is the right primitive: on the same volume it is atomic, it costs no copy, and it leaves no half-moved file behind. It also preserves the source file’s modification time, which is exactly what a rename is supposed to do — the bytes did not change, so neither should their metadata.
The collector then judged expiry with a single line:
if st.st_mtime > cutoff:
continue # still inside the window, keep it
mtime is a true statement about the content. A film released in 2014 that I re-encoded this morning still has a 2014 mtime on the original, because nothing modified it — it was only moved. So the entry landed in quarantine, the collector compared 2014 against “72 hours ago”, and concluded the file had expired years earlier. The window had never existed.
The same mistake flatters the site in the other direction, which is why the average looked plausible and nobody noticed:
| Entry | mtime carried in | Window under the mtime rule | Window after the fix |
|---|---|---|---|
| Film from 2014, replaced this morning | 2014 | 0 h | 72 h |
| File downloaded two hours ago, replaced now | ~2 h ago | ~70 h | 72 h |
| File stamped two hours in the future (copied from a machine whose clock runs ahead) | ~2 h ahead | ~74 h | 72 h |
All three rows are the same bug, and the middle one is the interesting one: the rule is not too strict or too generous, it is measuring the wrong event, so every window it grants is off by however long the content existed before the move. For a film, that error is measured in years; for a fresh download, in the two hours it sat on disk. Only the future-dated row can run the other way, and only because a timestamp from a machine with a skewed clock is not a duration at all. The 0-hour case deletes data, the 70-hour case made the average look plausible, and neither is what the comment above the line says the code does.
Ask the Entry When It Arrived
A retention clock has to be recorded at the event that starts the retention — at the moment of the move, not inferred later from the thing being retained. So the quarantine entry now carries its own arrival timestamp, written into a sidecar next to it during the move:
rec = {"src": os.path.abspath(path), "live": os.path.abspath(live), "note": note,
"ts": time.strftime("%Y-%m-%dT%H:%M:%S"), "pid": os.getpid()}
The collector reads that, with two fallbacks ordered so that a missing timestamp can only ever make a file look younger than it is — never older:
🔍 `trash_entry_ts()` — arrival time with a safe fallback order (Python, 27 lines)
def trash_entry_ts(path, st=None):
"""When this original entered quarantine — the 72 h hold is measured from here, not mtime.
Why (measured 2026-11-10): retiring an original is `os.replace`, which keeps the source file's
own mtime. So "a 2014 film replaced today" arrives carrying a 2014 timestamp, `st_mtime >
cutoff` judges it expired, and the promised 72-hour rollback window is 0 hours for old files.
Measured on the library: 86 of 118 entries were expired on arrival.
Order of precedence (earlier is more authoritative):
1. the sidecar `.json`'s `ts` (written by move_to_trash, second precision);
2. the `<trash>/<YYYY-MM-DD>/` directory name — day granularity, so the END of that day
(a day late rather than a second early);
3. neither exists (dropped in by hand) — only then fall back to mtime.
"""
try:
with open(path + ".json", encoding="utf-8") as fh:
ts = (json.load(fh) or {}).get("ts")
if ts:
return time.mktime(time.strptime(str(ts)[:19], "%Y-%m-%dT%H:%M:%S"))
except (OSError, ValueError, TypeError):
pass
day = os.path.basename(os.path.dirname(path))
try:
return time.mktime(time.strptime(day, "%Y-%m-%d")) + 86400.0
except ValueError:
pass
return (st.st_mtime if st is not None else os.path.getmtime(path))
Three details in that ordering are the whole fix:
The sidecar is written during the move, so it cannot be older than the move. Second precision, and it is the only field in the record whose meaning is custody rather than content.
The directory name is read as the end of its day. A day-granularity fallback that resolved to midnight would shave up to 24 hours off the hold; resolving to the following midnight gives that time back. When a fallback is unavoidable, it has to err in the direction that wastes disk rather than the direction that loses data.
mtime survives only for entries the pipeline never moved. Anything hand-dropped into quarantine has no sidecar and no dated directory, and for those the old behaviour is still the best available answer.
Both timestamps are written and read in the same local clock on the same host — time.strftime writes, time.mktime reads — so the comparison never crosses a timezone boundary, and a container that logs in UTC cannot shift a hold by hours.
The collector’s gate becomes one call, and the rest of purge_trash() is untouched:
if trash_entry_ts(p, st) > cutoff:
continue
The Three Invariants That Were Not Allowed to Move
Changing an expiry rule inside a delete path is a bad place to be clever, so the fix was scoped to the clock and nothing else. Three properties had to survive, and each exists because of a specific way this could destroy data:
The last copy is never collected. The sidecar records the live path the original was retired in favour of. If that file no longer exists, this quarantined entry is the library’s copy — the install failed somewhere after the move — and it is kept regardless of age, with a log line saying why. A collector that trusts its clock more than the filesystem turns a recoverable incident into a deletion.
The sweep is throttled per directory. A .purge stamp keeps each directory to at most one scan an hour. The same shares are walked by the rest of the pipeline every few minutes, and a garbage collector that re-lists a network directory on every pass becomes the bottleneck it was meant to prevent.
Deletion is per file, never recursive. Each candidate is os.removed individually, its sidecar cleaned up after it, and a failure on one file is counted and logged rather than aborting the sweep or escalating to a tree delete. A retention policy that runs unattended for years has to be boring at the level of single operations, because that is the level at which it will eventually be wrong.
The Tests Had the Same Bug as the Code
The install-gate tests faked expiry the obvious way: backdate a file’s mtime and assert the collector removes it. Those helpers were built against the wrong clock, so once the collector stopped reading mtime they would have gone on passing while testing nothing at all — a green suite measuring an operation the code no longer performs.
So the fixtures now backdate the sidecar ts as well, and two properties are pinned explicitly:
- An old original replaced today keeps its full 72 hours. Faked with a 2014 mtime and a fresh sidecar, which is the exact case that motivated the change.
- A missing sidecar may only ever make an entry look younger. With the sidecar removed, the dated directory is what answers, and the answer is the end of that day — one direction of error only.
The second test is the one that protects the first fix from its own future. A fallback chain is a series of opportunities to reintroduce the original bug at a lower precedence level, so the direction of each fallback’s error is now an assertion rather than a comment.
What Generalises Past This Directory
A retention window is an event, not a property of an object. Every “keep this for N days” rule needs a timestamp written at the moment retention starts, in a place the retained object cannot rewrite. mtime describes the content’s history: for a 2014 film it is 2014, which is correct and irrelevant. Reusing it as a custody clock is a category error that reads like a shortcut.
The primitive that made the quarantine safe is the one that made the clock wrong. Same-volume rename is atomic, free, and metadata-preserving; those are the same three properties. Nothing about os.replace misbehaved here. The lesson is that a cheap atomic move relocates an object’s history along with it, and any retention logic layered on top has to record its own start time rather than borrow the object’s.
The direction of a fallback’s error is a design decision. A retention clock that errs young deletes data; one that errs old wastes disk. Every ambiguity in this code now resolves toward the second, and the cost of that choice is bounded by the sweep interval: at worst a file lives an extra day.
Measure the promise, not the mechanism. The mechanism was verified — the rename was atomic, the rollback worked, the sweep ran every hour. What had never been measured is the property being promised to a person: for how long can this file still be restored? That question costs one comparison per entry, and nothing in the pipeline was asking it.
The Result
- 86 of 118 quarantined originals were expired on arrival (73%); after the change, an entry’s hold starts at its arrival timestamp and 0 of 118 are counted as expired before their time.
- A 2014 film replaced today gets the full 72 hours, and a file downloaded two hours ago now gets 72 hours instead of the 70 the old rule was granting it.
- The three safety invariants are unchanged: last copy never collected, per-directory sweep throttled to one an hour, deletion always per file.
- Two new tests pin the clock in both directions, and the expiry fixtures now backdate the arrival timestamp instead of the file’s.
Related: the three install gates that made the quarantine necessary in the first place — tail decode, two-phase commit, unbuffered readback — are written up in Five Automated Quality Gates for Video Transcoding.
Dealing with flaky containers, cloud cost bloat, or unexplained outages?
Real production reliability comes from understanding the kernel and the protocol layers, not just restarting containers. I manage 162 containers across 3 hosts running 24/7 client workloads.