Skip to content

Commit

Permalink
Merge pull request #11513 from glours/use-go-ps-pid-lock
Browse files Browse the repository at this point in the history
Double check watch pid if detected as still running on Windows
  • Loading branch information
glours committed Feb 22, 2024
2 parents c5a88de + 9b0d1ff commit a722441
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
7 changes: 0 additions & 7 deletions internal/locker/pidfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ package locker

import (
"fmt"
"os"
"path/filepath"

"github.com/docker/docker/pkg/pidfile"
)

type Pidfile struct {
Expand All @@ -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 changes: 29 additions & 0 deletions internal/locker/pidfile_unix.go
Original file line number Diff line number Diff line change
@@ -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 changes: 47 additions & 0 deletions internal/locker/pidfile_windows.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit a722441

Please sign in to comment.