Skip to content

Commit

Permalink
fix: startup crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
suphon-t committed Jan 12, 2024
1 parent b7e37b3 commit d7708c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 13 deletions.
3 changes: 2 additions & 1 deletion DotLocal/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Defaults
import SecureXPC

class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
override init() {
_ = HelperManager.shared
ClientManager.shared.checkInstalled()
}

Expand Down
24 changes: 13 additions & 11 deletions internal/daemon/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewAPIServer(logger *zap.Logger, dotlocal *DotLocal) (*APIServer, error) {
}

func (s *APIServer) Start(ctx context.Context) error {
err := s.killExistingProcess()
err := s.killExistingProcessIfNeeded()
if err != nil {
return err
}
Expand Down Expand Up @@ -75,7 +75,7 @@ func (s *APIServer) Stop() error {
return nil
}

func (s *APIServer) killExistingProcess() error {
func (s *APIServer) killExistingProcessIfNeeded() error {
_, err := os.Stat(util.GetApiSocketPath())
if err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand All @@ -85,8 +85,17 @@ func (s *APIServer) killExistingProcess() error {
}
s.logger.Info("Killing existing process", zap.String("path", util.GetApiSocketPath()))

_ = killExistingProcess()

_ = os.Remove(util.GetPidPath())
_ = os.Remove(util.GetApiSocketPath())

return nil
}

func killExistingProcess() error {
pidBytes, err := os.ReadFile(util.GetPidPath())
if err != nil {
if err == nil {
return err
}
pid, err := strconv.Atoi(string(pidBytes))
Expand All @@ -98,17 +107,10 @@ func (s *APIServer) killExistingProcess() error {
if err != nil {
return err
}
_ = process.Kill()

err = os.Remove(util.GetPidPath())
err = process.Kill()
if err != nil {
return err
}
err = os.Remove(util.GetApiSocketPath())
if err != nil {
return err
}

return nil
}

Expand Down
12 changes: 11 additions & 1 deletion internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@ func CreateTmpFile() (string, error) {
var dotlocalPath *string

func GetDotlocalPath() string {
return "/var/run/dotlocal"
if dotlocalPath == nil {
dir := "/var/run/dotlocal"
dotlocalPath = &dir

err := os.MkdirAll(dir, 0755)
if err != nil {
panic(err)
}
}

return *dotlocalPath
}

func GetApiSocketPath() string {
Expand Down

0 comments on commit d7708c9

Please sign in to comment.