Merge pull request #11513 from glours/use-go-ps-pid-lock

Double check watch pid if detected as still running on Windows
This commit is contained in:
Guillaume Lours 2024-02-22 16:21:47 +01:00 committed by GitHub
commit a7224411b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 79 additions and 7 deletions

1
go.mod
View File

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

2
go.sum
View File

@ -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/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 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= 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 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 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=

View File

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

View File

@ -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())
}

View File

@ -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
}