Writes of shares
Album settings
Every request that changes a user album saves it through save_album (apps/backend/api/albums/members.py): the columns the request changed and created_on, which the sidebar sorts by. A row that is gone is a 404. Up to 1.7.0 the requests saved the whole row as they had loaded it. A "select all" adds its photos between the two, so the save wrote back a title, a lock or a What members see switch changed meanwhile, and inserted an album deleted meanwhile again.
Photos added, an album shared, or photos taken out while the album is deleted are a 404 too (while_the_album_stays), a 500 before. A foreign key is only checked at the commit, so the helper wraps the transaction. When the album is still there, something else the request names went meanwhile (the account, a photo), and the answer is a 409. After the save the album is read again, so the answer has the switches as they are, not as the request loaded them.
Album members
set_member_role locks the membership row. Without one there is nothing to lock, so two requests sharing the same album with the same person both insert it. The second waits for the first on the unique constraint, and starts over with the row the first committed (_joined); it was an IntegrityError before. Unsharing someone who has no membership does nothing. Before, it still wrote the tombstone that takes the album off their phone, also when a share of the album with them was committed at the same time.
The album row is not locked: add_photos holds the membership row when it bumps the album, so the other order could deadlock.
Library shares
LibraryShareViewSet (apps/backend/api/views/library_shares.py) writes a share in one transaction with the bump of the photos it redacts (_library_share_saved in api/sync_signals.py). Django sends post_save after the save has committed, so a process killed in between left the phone of the recipient with the places and names the switch had taken away. Ending a share needs no such transaction: post_delete runs inside the delete's own. A second share of the same pair that passes the first check is refused by the unique constraint, as the same library_already_shared; a change of a share that ended meanwhile is a 404.
The recipients of a person
set_recipients (apps/backend/api/faces/person_shares.py) takes the row lock of the owner's account first, so two lists are saved one after the other and the second sees the rows of the first. With it go the accounts of the people on the old and the new list (see below). Then come, in the order of people and faces:
- On the first share, the photos the person is labelled on, in pk order (
lock_photos). Their approvals are written here and take a key lock on each photo at the commit, and a replaced file holds its photo before it asks for the person. Only the photos that were locked are approved: one labelled meanwhile waits for the owner's review. - The person's row. A row that is gone, because the person was deleted or merged into another one meanwhile, is a 404 and nothing is saved.
- The rows of the share.
Without the person's row lock the request wrote the rows of the share first and the person last. A merge or a delete of the person goes the other way round, and PostgreSQL ended one of the two with a deadlock.
The order of account locks
A transaction that needs the row of an account and other rows locks the accounts first, all it needs in one query in the order of their ids: lock_users in apps/backend/api/accounts/user_locks.py. After them come the library share, the person, the photo and the rest, in the order of their own. The advisory locks of sign-up and of the library folders (hold_signups, hold_library_folders) come before the accounts.
| Transaction | Accounts it locks first |
|---|---|
Deleting accounts (_lock_accounts in accounts/deletion.py) | every account deleted together, deleted, and the owners of the libraries shared with them |
| Changing or ending a library share | the owner |
| Starting a library share | the owner and the recipient |
set_recipients | the owner, the people on the list before and on the new one |
A report of removed files (ScanFilesView) | the account whose files they are |
A place (places/membership.py) | the owner |
A job for everyone (lock_all_starts) | every account |
lock_users takes FOR NO KEY UPDATE, so other requests can still write rows that point at the account, whose foreign key takes a key lock, while a large deletion runs. A job start waits for it.
After the accounts, a deletion locks the user's library (_lock_library): the photos in pk order, the clustering lock and the people in pk order, as every change of people and faces does. Django's own updates take the rows as they lie in the table. A person delete or a merge that had locked one person and waited for the next could then wait for the deletion, and the deletion for it.
Without this order PostgreSQL ended one of the two with a deadlock: a deletion met a switch of a library share, a report of removed files met a change of a library share, or a person shared with an account of a lower id met the start of a job for everyone.
What a delete removes is read before its transaction begins. A library share or a job that another request commits in between points at the account, and the delete fails on the foreign key with everything as it was. DeleteUserViewSet then runs it once more, with the new row.
Rows deleted can only have once
Every owner key is SET(get_deleted_user), so what must be unique per owner is made ready before the rows change hands, in prepare_user_deletion:
| Constraint | What happens to the second row |
|---|---|
A title, a day or a name (AlbumUser, AlbumAuto, AlbumPlace, AlbumThing, Tag) | _hand_over_unique_rows: renamed, dropped or merged |
One unfinished import of uploads (LongRunningJob) | _seal_upload_imports ends it |
One unknown cluster (Cluster) | _hand_over_unknown_cluster: its faces move to the unknown cluster deleted has, and it is deleted. The first one is handed over as it is |
One photo per main file (Photo) | _share_main_files: the photo keeps its main file and is marked shares_main_file, which takes it out of the constraint. The two photos are never merged, because each has its own shares, captions and people |
Accounts deleted together (Delete selected in the Django admin) are all prepared before the first row changes hands, so the photos are also compared with those of the other accounts.
Giving up the main file instead would make the photo show another file: without one, a motion clip, the timeline filters and the sync feed have nothing to go by, and the other files of the photo may be pictures that were never shared. Only photos of deleted are ever marked; scans and Set as main file keep to one photo per main file.
A new model, or a new constraint with an owner whose rows go to deleted, needs a line here and code for the second row. test_everything_unique_per_owner_is_known finds it and fails until then. A row an account has once and that is never handed over, such as its Nextcloud folder (NextcloudFolder, SET_NULL), stays without an account and is in nobody's way. In 1.7.0 the row of the unknown cluster was missing, and the second deletion failed with an IntegrityError. The constraint on the main file is new in 1.8.0.
The links of a deleted account
turn_off_public_links (apps/backend/api/accounts/deletion.py) is a post_delete receiver of the account. By then the photos and albums belong to deleted, in the same transaction, and their rows are locked: a link the owner turned on since the deletion began is seen and turned off, and a later one waits for the commit and is refused, because the link endpoints look for the owner again under the row lock. Up to 1.7.0 it ran before the photos changed hands, and a link turned on in between stayed on for good, as nobody can sign in as deleted.
Tests and models
apps/backend/api/tests/sharing_and_public/test_share_write_races.pyandtest_share_write_races_postgres.py(two connections).apps/backend/api/tests/users_and_auth/test_delete_accounts_in_a_row.py: three accounts with everything an account can own, deleted one after another, together, and after one that an older version deleted.verification/sharing/AlbumSave.tla,AlbumMember.tla,LibraryShareSend.tla,PersonRecipients.tla,DeletedOwnerLinks.tla,DeletedLibraries.tlaandAccountLocks.tla.