Skip to content

Commit 2f6f7c3

Browse files
committed
fix: check whether an instance is already running on startup
Signed-off-by: Tommy van der Vorst <tommy@pixelspark.nl>
1 parent 5043561 commit 2f6f7c3

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

SushitrainCore/src/sushitrain.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"syscall"
3030
"time"
3131

32+
"github.com/gofrs/flock"
3233
"github.com/syncthing/syncthing/lib/build"
3334
"github.com/syncthing/syncthing/lib/config"
3435
"github.com/syncthing/syncthing/lib/events"
@@ -63,6 +64,7 @@ type Client struct {
6364
extraneousIgnored []string
6465
Measurements *Measurements
6566
logHandler *logHandler
67+
appLock *flock.Flock
6668
}
6769

6870
type Change struct {
@@ -598,6 +600,16 @@ func (clt *Client) Load(resetDeltaIdxs bool) error {
598600
}
599601
clt.config = config
600602

603+
// Check if we are the only instance running
604+
clt.appLock = flock.New(locations.Get(locations.LockFile))
605+
slog.Info("Attempting to obtain application lock at", "path", locations.Get(locations.LockFile))
606+
locked, err := clt.appLock.TryLock()
607+
if err != nil {
608+
return fmt.Errorf("failed to obtain lock: %w", err)
609+
} else if !locked {
610+
return fmt.Errorf("The app cannot be started, as it appears it is already running. If this error persists, try restarting your device.")
611+
}
612+
601613
// Default retention interval taken from Syncthing's CLI default
602614
dbDeleteRetentionInterval := time.Duration(4320) * time.Hour
603615
// It really wants to set up a temporary API while migrating...

0 commit comments

Comments
 (0)