Copies without metadata and Nextcloud changes
Which file a copy is of
A copy is named <image_hash>.<content hash>.<ext>, with the content hash of the photo's main file (content_tag in apps/backend/api/sharing/copies.py). The picture can change while the photo keeps its image_hash: the scan that reads a replaced file gives the File row a new hash (a video, a similar picture), and another file of the photo can become its main file (the owner chooses it, the first one is missing, an image takes the place of its RAW). The name changes with it. The next request makes the new copy, and the copies of the same photo with other names go once it is in place. The copy of an edited picture has the version of the edit in its name instead (EditedPicture).
The name comes from the database, not from the file: a mount that gives the same file another time does not have it copied again, and a copy of the main file as the last scan read it is served while the original is offline. Without one an offline original is a 404, without a count: the copy of the picture before is never served instead.
Up to 1.7.0 every copy was named <image_hash>.<ext> and stayed for good, whatever became of the file. Nothing says which file such a copy is of, and its date does not either: the main file now can be an older one. They are not served any more. The first request after the upgrade makes a new copy, which counts as one of the recipient's hourly copies, and the old one goes with it or with the next daily cleanup.
A rotation written into the file changes the picture but not the File row, so _adopt_written_orientation removes the photo's copies after its commit.
After it has made a copy, ensure_copy reads the photo's row again (_source). The copy goes when the photo was purged or given another image_hash meanwhile (a 404), or has another file or orientation (a 503, the client asks again). What a killed process leaves goes with the daily cleanup (remove_orphaned_copies): copies of no photo of that owner, unfinished ones and those of 1.7.0, all older than an hour.
Visitors of public pages have no hourly bound (public_copies_allowed), there is nobody to count. The name bounds them: one copy for each picture the owner made public, whoever asks and how often.
The copy lock
A recipient of a library shared without original files gets a copy without metadata (ensure_copy in apps/backend/api/sharing/copies.py), made on the first request and kept under MEDIA_ROOT/library_copies/<owner id>/. One request makes a given copy; the others get a 503 with Retry-After at once.
_copy_lock opens the copy's stripe, one of 1024 lock files MEDIA_ROOT/library_copy_locks/<n>.lock with n the crc32 of <owner id>/<image_hash> modulo 1024 (copy_lock_path), and takes an OS lock on it with try_lock from librephotos/instance_lock.py: flock on Linux and macOS, msvcrt.locking on Windows. The OS drops the lock when its holder closes the file or dies, so a killed worker leaves nothing to take over. The 1024 files are empty and never removed, so no request ever locks a file another request has just unlinked. A request holds one stripe and never waits for one, so stripes cannot deadlock. Two different copies on one stripe exclude each other too: while k copies are being made, a request for another one gets a needless 503 with a chance of about k/1024, and its client asks again after Retry-After.
Up to 1.7.0 the lock was a file made with O_EXCL. A request took over a lock older than STALE_LOCK_SECONDS or one that had gone, and every request removed the lock file by path when it finished, so it could remove another request's fresh lock and two requests made the same copy.
The bound on new copies
A recipient makes at most COPIES_PER_HOUR new copies an hour, counted on their LibraryShare row or, through albums and shared people, on their CopyBudget row. _count_copy is one conditional UPDATE a step: one more within the current hour, else a new hour at one, else one more again for the request that lost the start of the hour to another. Up to 1.7.0 that request got a 503 although nothing had been made in the hour yet.
Forgetting an owner's copies
When an owner's last library share is deleted, by the API or by a cascade, their copies go:
forget_copies_after_last_share(post_delete) locks the owner's User row (select_for_update(no_key=True)) before it counts the remaining shares. On PostgreSQL two deletes of an owner's last two shares then run one after the other, and the second sees the first one's commit; before, each saw the other's share (write skew under READ COMMITTED) and neither forgot. On SQLite the delete transaction isBEGIN IMMEDIATEand was already serial.- It queues
forget_owner_copiesin the delete's transaction (enqueue_task_at), so the job commits with the delete and survives a kill; before,on_commitcalled it and a kill right after the commit lost it. The job does nothing when the owner shares their library again by the time it runs. That check and itsrmtreeare not one step: a share made and a copy finished in between are removed with the folder, and the copy is made again on its next request (it counts once more against the recipient's hourly budget). Holding every stripe around the two would stall all copy requests for the length of thermtree. ensure_copylooks for a share of the owner again after the copy is in place, and removes it (a 404) when there is none: a copy a request finished after the folder was removed would stay otherwise. Copies for an album or a shared person are not checked: their owner may have no library share.
Nextcloud changes a stopped scan downloaded
The Nextcloud scan (apps/backend/nextcloud/directory_watcher.py) downloads every new and changed file first, then imports them one by one. A scan that stopped in between (cancelled, killed, lease lost) left the new file on disk with the remote modification time, so the next scan found it CURRENT and, its path being indexed, skipped it for good.
_sync_local_copies now skips a CURRENT, indexed copy only when it was read as it is: the File row's indexed_mtime_ns (recorded by handle_new_image, #2296) is the copy's st_mtime_ns now (recorded_reads, _read_as_it_is). download sets that record to UNREAD_DOWNLOAD (0, which no file's time matches) before its os.replace, so a downloaded copy is not read as it is until the import records it again. That covers rows indexed before #2296, which have no record, and a remote edit that kept the modification time. A row without a record that no scan downloaded since, a sidecar and an exact copy of another file keep the old rule.
A cancel stops the scan before the next download too (_until_cancelled), not only between two imports; what it downloaded is imported by the next scan. One run works with the user row its task loaded: the server, the account and the directory it lists, and the folder below which detach_deleted_files flags missing files, do not change when the settings are saved meanwhile.
Tests and models
apps/backend/api/tests/media_serving/test_library_share_media.py: a held lock is a 503 however old, a lock file a killed request left does not block, the forget runs as a queued job (noon_commit), keeps the copies of a library shared again, and a copy finished after the last share ended is removed. A replaced file, another main file and one that stands in for a missing one get a new copy and the old one goes, a copy of 1.7.0 is made again, an offline original keeps its copy but never shows the picture it replaced, changing file times make one copy, a turn written into the file drops it, a copy finished after the photo was purged is removed, and the daily cleanup removes what belongs to no photo and the copies of 1.7.0.apps/backend/api/tests/sharing_and_public/test_copies_forget_postgres.py: on PostgreSQL, the delete of an owner's second-to-last share and of their last share wait on the owner row (pg_blocking_pids) and the forget is queued once.apps/backend/api/tests/scanning/test_nextcloud_remote_changes.py: a change downloaded by a stopped scan is imported by the next one, and not again after that, also for a row indexed before reads were recorded and for an edit that kept the modification time.apps/backend/api/tests/sharing_and_public/test_share_write_races_postgres.py: two copies at the start of an hour are both counted, and two at once stay within the bound.verification/sharing/NextcloudChange.tla,CopiesForget.tla,CopyLock.tla,CopyFollowsFile.tlaandCopyBudget.tla.