Remove the lock if the process isn't running. I.e. process does not respond to signal.
(self)
| 181 | os.unlink(self.path) |
| 182 | |
| 183 | def remove_if_stale(self): |
| 184 | """Remove the lock if the process isn't running. |
| 185 | |
| 186 | I.e. process does not respond to signal. |
| 187 | """ |
| 188 | try: |
| 189 | pid = self.read_pid() |
| 190 | except ValueError: |
| 191 | print('Broken pidfile found - Removing it.', file=sys.stderr) |
| 192 | self.remove() |
| 193 | return True |
| 194 | if not pid: |
| 195 | self.remove() |
| 196 | return True |
| 197 | if pid == os.getpid(): |
| 198 | # this can be common in k8s pod with PID of 1 - don't kill |
| 199 | self.remove() |
| 200 | return True |
| 201 | |
| 202 | try: |
| 203 | os.kill(pid, 0) |
| 204 | except OSError as exc: |
| 205 | if exc.errno == errno.ESRCH or exc.errno == errno.EPERM: |
| 206 | print('Stale pidfile exists - Removing it.', file=sys.stderr) |
| 207 | self.remove() |
| 208 | return True |
| 209 | except SystemError: |
| 210 | print('Stale pidfile exists - Removing it.', file=sys.stderr) |
| 211 | self.remove() |
| 212 | return True |
| 213 | return False |
| 214 | |
| 215 | def write_pid(self): |
| 216 | pid = os.getpid() |