From adf5d8c4c9c506008eb4f6b78d272729b3ac76fe Mon Sep 17 00:00:00 2001 From: strogiyotec Date: Fri, 4 Sep 2020 16:11:55 -0700 Subject: [PATCH 1/2] delete socket file after accidental crash --- server.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server.go b/server.go index c57ed82..7bb44a9 100644 --- a/server.go +++ b/server.go @@ -2,7 +2,10 @@ package main import ( "encoding/json" + "errors" + "fmt" "net" + "os" ) // Server listens on a Unix domain socket @@ -39,6 +42,17 @@ func (s *Server) Stop() { } func NewServer(runner *TaskRunner, config *Config) (*Server, error) { + //check if socket file exists + if _, err := os.Stat(config.SocketPath); os.IsExist(err) { + _, err := net.Dial("unix", config.SocketPath) + //if error then sock file was saved after crash + if err != nil { + os.Remove(config.SocketPath) + } else { + // another instance of pomo is running + return nil, errors.New(fmt.Sprintf("Socket %s is already in use", config.SocketPath)) + } + } listener, err := net.Listen("unix", config.SocketPath) if err != nil { return nil, err From 7f85672bca9114bbe441727c06b7ee54e58d9154 Mon Sep 17 00:00:00 2001 From: strogiyotec Date: Sun, 6 Sep 2020 15:29:48 -0700 Subject: [PATCH 2/2] simplify sock file exists check --- server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.go b/server.go index 7bb44a9..87c5c7e 100644 --- a/server.go +++ b/server.go @@ -43,7 +43,7 @@ func (s *Server) Stop() { func NewServer(runner *TaskRunner, config *Config) (*Server, error) { //check if socket file exists - if _, err := os.Stat(config.SocketPath); os.IsExist(err) { + if _, err := os.Stat(config.SocketPath); err == nil { _, err := net.Dial("unix", config.SocketPath) //if error then sock file was saved after crash if err != nil {