You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by GitBox <gi...@apache.org> on 2021/09/16 04:41:33 UTC

[GitHub] [apisix-dashboard] freecodelover commented on a change in pull request #2054: fix: perform pid lookup for existing pid file

freecodelover commented on a change in pull request #2054:
URL: https://github.com/apache/apisix-dashboard/pull/2054#discussion_r709752587



##########
File path: api/internal/utils/pid.go
##########
@@ -18,19 +18,62 @@
 package utils
 
 import (
+	"bytes"
 	"fmt"
 	"io/ioutil"
 	"os"
+	"runtime"
 	"strconv"
+	"syscall"
+	"time"
+
+	"github.com/pkg/errors"
 )
 
 // WritePID write pid to the given file path.
 func WritePID(filepath string, forceStart bool) error {
-	if pid, err := ReadPID(filepath); err == nil {
-		if !forceStart {
-			return fmt.Errorf("Instance of Manager API maybe running with a pid %d. If not, please run Manager API with '-f' or '--force' flag\n", pid)
+	pidStatAction := func() error {
+		pid, err := ReadPID(filepath)
+		if err != nil {
+			return nil
+		}
+		// In unix system this always succeeds, doesn't ensure if the process at all exist or not.
+		proc, err := os.FindProcess(pid)
+		if err != nil {
+			return errors.Errorf("instance of Manager API maybe running with a pid %d. If not, please run Manager API with '-f' or '--force' flag. Error :%v\n", pid, err)
+		}
+
+		// Traditional unix way to check the existence of the particular PID.
+		err = proc.Signal(syscall.Signal(0))
+		// No entry of the process in process control block.
+		if err != nil {
+			return nil
+		}
+
+		// Windows has a tendency to pick unused old PIDs and allocate that to new process, so we won't kill it.
+		if runtime.GOOS != "windows" && forceStart {
+			fmt.Println("Killing existing instance of Manager API")

Review comment:
       It would be better to add the pid into the message.

##########
File path: api/internal/utils/pid.go
##########
@@ -18,19 +18,62 @@
 package utils
 
 import (
+	"bytes"
 	"fmt"
 	"io/ioutil"
 	"os"
+	"runtime"
 	"strconv"
+	"syscall"
+	"time"
+
+	"github.com/pkg/errors"
 )
 
 // WritePID write pid to the given file path.
 func WritePID(filepath string, forceStart bool) error {
-	if pid, err := ReadPID(filepath); err == nil {
-		if !forceStart {
-			return fmt.Errorf("Instance of Manager API maybe running with a pid %d. If not, please run Manager API with '-f' or '--force' flag\n", pid)
+	pidStatAction := func() error {
+		pid, err := ReadPID(filepath)
+		if err != nil {
+			return nil
+		}
+		// In unix system this always succeeds, doesn't ensure if the process at all exist or not.
+		proc, err := os.FindProcess(pid)
+		if err != nil {
+			return errors.Errorf("instance of Manager API maybe running with a pid %d. If not, please run Manager API with '-f' or '--force' flag. Error :%v\n", pid, err)

Review comment:
       I think `os. FindProcess` will return an error when the process does not exist in windows according to:
   https://golang.org/src/os/exec_windows.go
   
   So, we should not return the error here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@apisix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org