Skip to main content

Library folder claims

Not released yet (comes after 1.7.0)

Every photo has one owner, so no two users' libraries may overlap (#2034): a scan directory, an upload folder or a further library folder may not be the same as, inside or above a folder of another user's library. A scan only imports files that lie in the library, outside the folders the user left out (#791).

The lock​

apps/backend/api/accounts/library_lock.py holds one lock for every library folder of every user:

  • hold_library_folders() takes it for a write: pg_advisory_xact_lock on PostgreSQL, until the transaction ends. On SQLite it does nothing; every transaction.atomic() there is BEGIN IMMEDIATE (librephotos/settings/_sqlite.py), so one writer runs at a time from the start of its transaction.
  • share_library_folders() takes it shared for a scan's insert: pg_advisory_xact_lock_shared, so inserts do not wait for each other, only for a change of a library.

The writes that take it, each in the transaction that writes:

WriteWhere
a user's scan directory or upload folderManageUserSerializer.update -> claim_directories (api/accounts/directories.py)
a scan directory given on user creationUserSerializer.create -> claim_directories
the folder AUTO_CREATE_USER_DIRECTORY assignsauto_create_user_directory
a further library folder added or removedadd_library_folder, remove_library_folder (api/photos/library_folders.py)
the folders a user leaves outset_excluded_folders (api/photos/excluded_folders.py)

Each write checks before the lock, so a clash answers at once, and checks again under the lock against what other requests committed meanwhile. Everything that reads the disk runs before the lock: the folder exists, and realpath folds a symlink or junction onto its target. The request resolves each path once (ResolvedPaths in api/accounts/directories.py) and the checks under the lock compare those resolved spellings, the candidate's and every stored folder's, so an alias such as link for another user's real is refused also when the two claims run at the same moment. Only a folder another request committed while this one checked is resolved under the lock, so a hung mount does not hold the lock for every claim. A clash found under the lock is the same 400 as before, naming the field (scan_directory, upload_directory or path). The Django admin's user form writes columns without these checks, as before.

Up to 1.7.0 the check read the other users' folders and the write came later, outside any lock. On PostgreSQL (READ COMMITTED) two admins could each pass the check and save overlapping folders; on SQLite a library folder add held the write lock, but a scan directory change checked outside its transaction.

A scan during a change​

scan_photos reads the library once, before it walks: the further folders (library_folder_paths), from which it walks and computes library_of(user, folders).version(), a hash of the roots and the folders left out. Each file group goes into the queue with that version (handle_file_group(..., library=<version>)).

A file group checks twice (api/directory_watcher/file_handlers.py):

  1. Before it reads any file, left_library compares the library the task loaded with the walk's version; when they differ, a group with a path outside the current library is skipped without making File rows.
  2. In the transaction that inserts or adopts the photo (_photo_for_group), _require_library takes the lock shared, reads the user's library again and goes on only when the version is the walk's or every path is still in the library. Otherwise it raises LeftLibraryError and the group is skipped. A change of the library waits for that insert to commit, and an insert waits for a change that holds the lock, so the check and the insert see the same library.

A skipped group counts toward the scan's progress and does not fail it: the job's result counts the media files (not their XMP sidecars) in files_left_library and lists one notice, "N files were not imported because their folder left the library during the scan" (count_left_library in api/directory_watcher/utils.py), which marks the scan partial_failure like a folder it skipped. A group refused under the lock has already made its File rows; they stay without a photo, and the scan of the folder's next owner takes them over, as for a path another user scanned first. A folder added to the library during a scan changes the version, but its files are still in the library, so they are imported. A group queued without a version (a test, or a task queued before 1.8.0) is not checked. Uploads and the Nextcloud scan do not go through file groups.

Up to 1.7.0 a scan imported the file groups it had queued even after an admin moved the library, removed the folder or the user left it out, and the old folder could then be given to another user while the first still got its photos.

Tests and models​

  • apps/backend/api/tests/users_and_auth/test_library_folder_claims.py: a scan directory, an upload folder and a library folder claimed by another request between the check and the write, two folder adds, and the folder auto_create_user_directory assigns.
  • apps/backend/api/tests/scanning/test_scan_after_library_change.py: groups queued before a library move or before their folder was left out, a move while a group reads its files, and the cases that still import.
  • apps/backend/api/tests/users_and_auth/test_library_folders_lock_postgres.py: on PostgreSQL, a second claim waits on the lock (pg_blocking_pids) and is refused, and an insert waits for a library move and skips the old folder.
  • verification/auth/DirOverlap.tla, verification/auth/ScanDirChange.tla and verification/sharing/LibraryFolderOverlap.tla.