Repository navigation
Description
What happened?
When three or more devices change the same file at (nearly) the same moment, one of the edits can be lost permanently: it does not survive as the file, as a conflict copy, or in the folder's version archive.
Cause. conflictName() builds the conflict copy's name from the current time (to the second) and the winning version's ModifiedBy:
name[:len(name)-len(ext)] + time.Now().Format(".sync-conflict-20060102-150405-") + lastModBy + extIf two devices both lose to the same winner and resolve the conflict within the same second, they create conflict copies with identical names but different contents. Those two copies then conflict with each other, and moveForConflict() handles a conflict on a conflict copy by deleting it (the behaviour introduced for #2605):
if isConflict(name) {
l.Infoln("Conflict on existing conflict copy; not copying again")
if err := f.mtimefs.Remove(name); ...The Remove bypasses the folder's versioner, so the losing edit is gone everywhere.
Steps to reproduce
- Four devices: a hub device
Hthat shares one send-receive folder with three devicesA,B,C(star topology;A/B/Cdon't connect to each other). Simple versioning onH. - On
A,BandC, write different content to the same new file within ~1 s, e.g.echo "A version" > f.txt(andB/C). - Wait for everything to settle.
Expected: f.txt plus two conflict copies; all three versions survive.
Actual: in 3 of 5 rounds, f.txt plus only one conflict copy; the third version exists nowhere (not on any device, not in H's .stversions). Every device ends up identical, so nothing looks wrong. The losing device logs:
INF Synced file (file.name=f.txt ... )
INF Conflict on existing conflict copy; not copying again (path=f.sync-conflict-20261114-235922-CCCCCCC.txt ...)
(CCCCCCC is the winner C's short ID; both A and B created f.sync-conflict-20261114-235922-CCCCCCC.txt in the same second.)
Two devices editing at once never loses data (one loser, one copy). It takes two losers to the same winner resolving their conflicts within the same wall-clock second, so at least three devices. It is rare for people typing, much less rare for tools/scripts writing a shared file on several machines.
Suggested fix (tested; patch below):
- Make conflict-copy names unique per device by also including the short ID of the device that creates the copy (
-<winner>-<local>). The existingexistingConflictsglob (.sync-conflict-????????-??????*) still matches, somaxConflictscleanup keeps working. - When a conflict copy does conflict again, archive it with the versioner if the folder has one instead of
Remove-ing it, so even an unexpected collision is recoverable.
--- a/lib/model/folder_sendrecv.go
+++ b/lib/model/folder_sendrecv.go
@@ -1920,6 +1920,15 @@
func (f *sendReceiveFolder) moveForConflict(name, lastModBy string, scanChan chan<- string) error {
if isConflict(name) {
f.sl.Info("Conflict on existing conflict copy; not copying again", slogutil.FilePath(name))
+ // Keep the replaced conflict copy in the version archive when the
+ // folder has one, instead of deleting it. Conflict copies are the
+ // losing side of a conflict; deleting one loses that edit for good.
+ if f.versioner != nil {
+ if err := f.inWritableDir(f.versioner.Archive, name); err != nil && !fs.IsNotExist(err) {
+ return fmt.Errorf("%s: %w", contextRemovingOldItem, err)
+ }
+ return nil
+ }
if err := f.mtimefs.Remove(name); err != nil && !fs.IsNotExist(err) {
return fmt.Errorf("%s: %w", contextRemovingOldItem, err)
}
@@ -1934,7 +1943,11 @@
}
metricFolderConflictsTotal.WithLabelValues(f.ID).Inc()
- newName := conflictName(name, lastModBy)
+ // Name the copy after the device making it too. Two devices that both
+ // lose to the same winner in the same second used to pick the identical
+ // name; the copies then conflicted with each other and one was deleted.
+ newName := conflictName(name, lastModBy+"-"+f.shortID.String())
err := f.mtimefs.Rename(name, newName)With this patch, go test ./lib/model/ ./lib/versioner/ passes, and the reproduction above lost nothing in 15 of 15 rounds (each round leaves f.txt plus two distinct copies, e.g. f.sync-conflict-…-<winner>-<loserA>.txt and …-<winner>-<loserB>.txt). Happy to open a PR if this direction is acceptable.
Syncthing version
v2.1.7 (also checked main: moveForConflict / conflictName unchanged)
Platform & operating system
Linux amd64 on all four devices (several distros)
Relevant log output
INF Synced file (folder.type=sendreceive file.name=f.txt file.size=18 blocks.local=0 blocks.download=1)
INF Conflict on existing conflict copy; not copying again (path=f.sync-conflict-20261115-000018-BBBBBBB.txt folder.type=sendreceive)
INF Synced file (folder.type=sendreceive file.name=f.sync-conflict-20261115-000018-BBBBBBB.txt file.size=18 blocks.local=0 blocks.download=1)
Activity