Skip to content

Commit a722441

Browse files
authored
Merge pull request #11513 from glours/use-go-ps-pid-lock
Double check watch pid if detected as still running on Windows
2 parents c5a88de + 9b0d1ff commit a722441

File tree

5 files changed

+79
-7
lines changed

5 files changed

+79
-7
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/hashicorp/go-version v1.6.0
2424
github.com/jonboulle/clockwork v0.4.0
2525
github.com/mattn/go-shellwords v1.0.12
26+
github.com/mitchellh/go-ps v1.0.0
2627
github.com/mitchellh/mapstructure v1.5.0
2728
github.com/moby/buildkit v0.13.0-beta1.0.20231219135447-957cb50df991
2829
github.com/moby/patternmatcher v0.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ github.com/miekg/pkcs11 v1.1.1 h1:Ugu9pdy6vAYku5DEpVWVFPYnzV+bxB+iRdbuFSu7TvU=
331331
github.com/miekg/pkcs11 v1.1.1/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
332332
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
333333
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
334+
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
335+
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
334336
github.com/mitchellh/mapstructure v0.0.0-20150613213606-2caf8efc9366/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
335337
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
336338
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=

internal/locker/pidfile.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ package locker
1818

1919
import (
2020
"fmt"
21-
"os"
2221
"path/filepath"
23-
24-
"github.com/docker/docker/pkg/pidfile"
2522
)
2623

2724
type Pidfile struct {
@@ -36,7 +33,3 @@ func NewPidfile(projectName string) (*Pidfile, error) {
3633
path := filepath.Join(run, fmt.Sprintf("%s.pid", projectName))
3734
return &Pidfile{path: path}, nil
3835
}
39-
40-
func (f *Pidfile) Lock() error {
41-
return pidfile.Write(f.path, os.Getpid())
42-
}

internal/locker/pidfile_unix.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright 2023 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package locker
20+
21+
import (
22+
"os"
23+
24+
"github.com/docker/docker/pkg/pidfile"
25+
)
26+
27+
func (f *Pidfile) Lock() error {
28+
return pidfile.Write(f.path, os.Getpid())
29+
}

internal/locker/pidfile_windows.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//go:build windows
2+
3+
/*
4+
Copyright 2023 Docker Compose CLI authors
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package locker
20+
21+
import (
22+
"github.com/docker/docker/pkg/pidfile"
23+
"github.com/mitchellh/go-ps"
24+
"os"
25+
)
26+
27+
func (f *Pidfile) Lock() error {
28+
newPID := os.Getpid()
29+
err := pidfile.Write(f.path, newPID)
30+
if err != nil {
31+
// Get PID registered in the file
32+
pid, errPid := pidfile.Read(f.path)
33+
if errPid != nil {
34+
return err
35+
}
36+
// Some users faced issues on Windows where the process written in the pidfile was identified as still existing
37+
// So we used a 2nd process library to verify if this not a false positive feedback
38+
// Check if the process exists
39+
process, errPid := ps.FindProcess(pid)
40+
if process == nil && errPid == nil {
41+
// If the process does not exist, remove the pidfile and try to lock again
42+
_ = os.Remove(f.path)
43+
return pidfile.Write(f.path, newPID)
44+
}
45+
}
46+
return err
47+
}

0 commit comments

Comments
 (0)