Fix a race condition that leads to a CouchDB conflict #4158
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If Alice and Bob shares a file within a Cozy to Cozy sharing. The file is present on both cozy instances, and the two users make a change on this file concurrently. For example, Alice adds the file to a photo album, and Bob renames it. A race condition may happen when Bob's instance sends its update to Alice's instance. The CouchDB document will then been saved in CouchDB with a conflict (two leaf revisions).
In general, it is not possible to save a document with a revision 2-bbb, when the document is already at revision 2-aaa: CouchDB responds with a 409 Conflict. But, the share-track is doing a bulk update with the option new_edit: false. This is done because the replication process needs to force the same revision on Alice's Cozy that it is on Bob's Cozy. But, it also enable to create a conflict in CouchDB.
The fix is to reload the file between the steps 6 and 7 (when the VFS lock has been acquired), and check that the revision has not changed. It would be nice to load the document when the VFS lock is acquired, but it is not possible currently because of how the code is organized.