Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent user starting wails app twice #709

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions main_wails.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main
import (
"context"
"embed"
"net"

"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/service"
Expand All @@ -20,6 +21,14 @@ var assets embed.FS
var appIcon []byte

func main() {
// Get a port lock on a rare port to prevent the app running twice
listener, err := net.Listen("tcp", "0.0.0.0:21420")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

crazy. does this work across OS?
and it just listens but does not do anything? can we maybe write a pid-file like file to th working dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think writing a file is not so good because there is no guarantee it will be cleaned up. Trying to reserve a port seems safer to me

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a point. though most processes do write pid files for that case. I just have never seen an app reserving a port for this.
was wondering what are potential side effects.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is a very creative approach, also wondered about side effects, but the fact that you don't run into lockfile problems is quite great.

if err != nil {
log.Println("Another instance of Alby Hub is already running.")
return
}
defer listener.Close()

log.Info("Alby Hub starting in WAILS mode")
ctx, cancel := context.WithCancel(context.Background())
svc, _ := service.NewService(ctx)
Expand Down
Loading