From 9b0d1ffcf80ca8d7d63a441b18c2327ddc682ef6 Mon Sep 17 00:00:00 2001 From: Guillaume Lours <705411+glours@users.noreply.github.com> Date: Fri, 16 Feb 2024 17:17:16 +0100 Subject: [PATCH] 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 <705411+glours@users.noreply.github.com> --- go.mod | 1 + go.sum | 2 ++ internal/locker/pidfile.go | 7 ----- internal/locker/pidfile_unix.go | 29 ++++++++++++++++++ internal/locker/pidfile_windows.go | 47 ++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 internal/locker/pidfile_unix.go create mode 100644 internal/locker/pidfile_windows.go diff --git a/go.mod b/go.mod index dd28fa487..06555b35b 100644 --- a/go.mod +++ b/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 diff --git a/go.sum b/go.sum index 19dd1afd4..abfe07b7b 100644 --- a/go.sum +++ b/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= diff --git a/internal/locker/pidfile.go b/internal/locker/pidfile.go index ea688fb66..08dcea1f3 100644 --- a/internal/locker/pidfile.go +++ b/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()) -} diff --git a/internal/locker/pidfile_unix.go b/internal/locker/pidfile_unix.go new file mode 100644 index 000000000..484b65d82 --- /dev/null +++ b/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()) +} diff --git a/internal/locker/pidfile_windows.go b/internal/locker/pidfile_windows.go new file mode 100644 index 000000000..9f8d4c3ee --- /dev/null +++ b/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 +}