From 190a59842c6908f99ccfcf8ae9b4d2548c3d4cbd Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sat, 12 Apr 2025 05:46:57 -0700 Subject: [PATCH] fix(syncthing): use separate lock file instead of locking the certificate (fixes #10053) (#10054) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apparently that nukes the cert under some circumstances on some Windows 🤷 --- cmd/syncthing/main.go | 10 ++++++++-- lib/locations/locations.go | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index 1faff628e..943d11dc8 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -376,7 +376,7 @@ func (options serveOptions) Run() error { if options.Upgrade { release, err := checkUpgrade() if err == nil { - lf := flock.New(locations.Get(locations.CertFile)) + lf := flock.New(locations.Get(locations.LockFile)) locked, err := lf.TryLock() if err != nil { l.Warnln("Upgrade:", err) @@ -386,6 +386,8 @@ func (options serveOptions) Run() error { } else { err = upgrade.To(release) } + _ = lf.Unlock() + _ = os.Remove(locations.Get(locations.LockFile)) } if err != nil { l.Warnln("Upgrade:", err) @@ -546,7 +548,7 @@ func syncthingMain(options serveOptions) { } // Ensure we are the only running instance - lf := flock.New(locations.Get(locations.CertFile)) + lf := flock.New(locations.Get(locations.LockFile)) locked, err := lf.TryLock() if err != nil { l.Warnln("Failed to acquire lock:", err) @@ -692,6 +694,10 @@ func syncthingMain(options serveOptions) { pprof.StopCPUProfile() } + // Best effort remove lockfile, doesn't matter if it succeeds + _ = lf.Unlock() + _ = os.Remove(locations.Get(locations.LockFile)) + os.Exit(int(status)) } diff --git a/lib/locations/locations.go b/lib/locations/locations.go index 197c686eb..bbf11d919 100644 --- a/lib/locations/locations.go +++ b/lib/locations/locations.go @@ -33,6 +33,7 @@ const ( AuditLog LocationEnum = "auditLog" GUIAssets LocationEnum = "guiAssets" DefFolder LocationEnum = "defFolder" + LockFile LocationEnum = "lockFile" ) type BaseDirEnum string @@ -124,6 +125,7 @@ var locationTemplates = map[LocationEnum]string{ AuditLog: "${data}/audit-%{timestamp}.log", GUIAssets: "${config}/gui", DefFolder: "${userHome}/Sync", + LockFile: "${data}/syncthing.lock", } var locations = make(map[LocationEnum]string)