| 26 | ) |
| 27 | |
| 28 | func (f *Pidfile) Lock() error { |
| 29 | newPID := os.Getpid() |
| 30 | err := pidfile.Write(f.path, newPID) |
| 31 | if err != nil { |
| 32 | // Get PID registered in the file |
| 33 | pid, errPid := pidfile.Read(f.path) |
| 34 | if errPid != nil { |
| 35 | return err |
| 36 | } |
| 37 | // Some users faced issues on Windows where the process written in the pidfile was identified as still existing |
| 38 | // So we used a 2nd process library to verify if this not a false positive feedback |
| 39 | // Check if the process exists |
| 40 | process, errPid := ps.FindProcess(pid) |
| 41 | if process == nil && errPid == nil { |
| 42 | // If the process does not exist, remove the pidfile and try to lock again |
| 43 | _ = os.Remove(f.path) |
| 44 | return pidfile.Write(f.path, newPID) |
| 45 | } |
| 46 | } |
| 47 | return err |
| 48 | } |