Quellcode durchsuchen

Add a fallback check of Watch pid on Windows
False positives were detected when checking the previous watch process state

Signed-off-by: Guillaume Lours <[email protected]>

Guillaume Lours vor 1 Jahr
Ursprung
Commit
9b0d1ffcf8
5 geänderte Dateien mit 79 neuen und 7 gelöschten Zeilen
  1. 1 0
      go.mod
  2. 2 0
      go.sum
  3. 0 7
      internal/locker/pidfile.go
  4. 29 0
      internal/locker/pidfile_unix.go
  5. 47 0
      internal/locker/pidfile_windows.go

+ 1 - 0
go.mod

@@ -23,6 +23,7 @@ require (
 	github.com/hashicorp/go-version v1.6.0
 	github.com/jonboulle/clockwork v0.4.0
 	github.com/mattn/go-shellwords v1.0.12
+	github.com/mitchellh/go-ps v1.0.0
 	github.com/mitchellh/mapstructure v1.5.0
 	github.com/moby/buildkit v0.13.0-beta1.0.20231219135447-957cb50df991
 	github.com/moby/patternmatcher v0.6.0

+ 2 - 0
go.sum

@@ -331,6 +331,8 @@ github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU=
 github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
 github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
 github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
+github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
+github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
 github.com/mitchellh/mapstructure v0.0.0-20150613213606-2caf8efc9366/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
 github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
 github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=

+ 0 - 7
internal/locker/pidfile.go

@@ -18,10 +18,7 @@ package locker
 
 import (
 	"fmt"
-	"os"
 	"path/filepath"
-
-	"github.com/docker/docker/pkg/pidfile"
 )
 
 type Pidfile struct {
@@ -36,7 +33,3 @@ func NewPidfile(projectName string) (*Pidfile, error) {
 	path := filepath.Join(run, fmt.Sprintf("%s.pid", projectName))
 	return &Pidfile{path: path}, nil
 }
-
-func (f *Pidfile) Lock() error {
-	return pidfile.Write(f.path, os.Getpid())
-}

+ 29 - 0
internal/locker/pidfile_unix.go

@@ -0,0 +1,29 @@
+//go:build !windows
+
+/*
+   Copyright 2023 Docker Compose CLI authors
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package locker
+
+import (
+	"os"
+
+	"github.com/docker/docker/pkg/pidfile"
+)
+
+func (f *Pidfile) Lock() error {
+	return pidfile.Write(f.path, os.Getpid())
+}

+ 47 - 0
internal/locker/pidfile_windows.go

@@ -0,0 +1,47 @@
+//go:build windows
+
+/*
+   Copyright 2023 Docker Compose CLI authors
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+package locker
+
+import (
+	"github.com/docker/docker/pkg/pidfile"
+	"github.com/mitchellh/go-ps"
+	"os"
+)
+
+func (f *Pidfile) Lock() error {
+	newPID := os.Getpid()
+	err := pidfile.Write(f.path, newPID)
+	if err != nil {
+		// Get PID registered in the file
+		pid, errPid := pidfile.Read(f.path)
+		if errPid != nil {
+			return err
+		}
+		// Some users faced issues on Windows where the process written in the pidfile was identified as still existing
+		// So we used a 2nd process library to verify if this not a false positive feedback
+		// Check if the process exists
+		process, errPid := ps.FindProcess(pid)
+		if process == nil && errPid == nil {
+			// If the process does not exist, remove the pidfile and try to lock again
+			_ = os.Remove(f.path)
+			return pidfile.Write(f.path, newPID)
+		}
+	}
+	return err
+}