Whole file
trbs/pid
The author described this change as “1. setup logger during _setup() since the default handle/stream may get closed 2. fix explicit call to check() and add test for double lazy check.”. It counts as a record because the check below fails on the code as it stood at 034bca117 and passes on 6e256070c, with nothing else changed between the two runs.
Projecttrbs/pid
Fix saved2015-03-26
Sharing licenceApache-2.0 · LICENSE
Change size+14 −5
What the code was meant to do, written into the code itself as a save note
1. setup logger during _setup() since the default handle/stream may get closed 2. fix explicit call to check() and add test for double lazy check.
The change
| 11 | 11 | __version__ = "1.1.0" | |
| 12 | 12 | ||
| 13 | 13 | DEFAULT_PID_DIR = "/var/run/" | |
| 14 | - | logger = logging.getLogger("PidFile") | |
| 15 | 14 | ||
| 16 | 15 | ||
| 17 | 16 | class PidFileError(Exception): | |
| ⋯ | |||
| 59 | 58 | self._setup() | |
| 60 | 59 | ||
| 61 | 60 | def _setup(self): | |
| 61 | + | self.logger = logging.getLogger("PidFile") | |
| 62 | + | self.logger.debug("%r entering setup", self) | |
| 62 | 63 | if self.filename is None: | |
| 63 | 64 | self.pid = os.getpid() | |
| 64 | 65 | self.filename = self._make_filename() | |
| 65 | 66 | self._register_term_signal() | |
| 66 | 67 | ||
| 68 | + | # setup should only be performed once | |
| 69 | + | self.lazy = False | |
| 70 | + | ||
| 67 | 71 | def _make_filename(self): | |
| 68 | 72 | pidname = self.pidname | |
| 69 | 73 | piddir = self.piddir | |
| ⋯ | |||
| 101 | 105 | signal.signal(signal.SIGTERM, term_signal_handler) | |
| 102 | 106 | ||
| 103 | 107 | def check(self): | |
| 104 | - | logger.debug("%r check pidfile: %s", self, self.filename) | |
| 105 | 108 | ||
| 106 | 109 | def inner_check(fh): | |
| 107 | 110 | try: | |
| ⋯ | |||
| 124 | 127 | self.close(fh=fh, cleanup=False) | |
| 125 | 128 | raise PidFileAlreadyRunningError("Program already running with pid: %d" % pid) | |
| 126 | 129 | ||
| 130 | + | if self.lazy: | |
| 131 | + | self._setup() | |
| 132 | + | ||
| 133 | + | self.logger.debug("%r check pidfile: %s", self, self.filename) | |
| 134 | + | ||
| 127 | 135 | if self.fh is None: | |
| 128 | 136 | if self.filename and os.path.isfile(self.filename): | |
| 129 | 137 | with open(self.filename, "r") as fh: | |
| ⋯ | |||
| 132 | 140 | inner_check(self.fh) | |
| 133 | 141 | ||
| 134 | 142 | def create(self): | |
| 135 | - | logger.debug("%r create pidfile: %s", self, self.filename) | |
| 136 | 143 | if self.lazy: | |
| 137 | 144 | self._setup() | |
| 145 | + | ||
| 146 | + | self.logger.debug("%r create pidfile: %s", self, self.filename) | |
| 138 | 147 | self.fh = open(self.filename, 'a+') | |
| 139 | 148 | if self.lock_pidfile: | |
| 140 | 149 | try: | |
| ⋯ | |||
| 156 | 165 | atexit.register(self.close) | |
| 157 | 166 | ||
| 158 | 167 | def close(self, fh=None, cleanup=True): | |
| 159 | - | logger.debug("%r closing pidfile: %s", self, self.filename) | |
| 160 | 168 | if not fh: | |
| 161 | 169 | fh = self.fh | |
| 162 | 170 | try: | |
| 163 | - | if fh is None: | |
| 171 | + | if fh is None or self.lazy is True: | |
| 164 | 172 | return | |
| 173 | + | self.logger.debug("%r closing pidfile: %s", self, self.filename) | |
| 165 | 174 | fh.close() | |
| 166 | 175 | except IOError as exc: | |
| 167 | 176 | # ignore error when file was already closed | |
The check that tells the two apart
fail→pass·tests/test_pid.py::test_pid_double_lazy_check_already_running
Check file tests/test_pid.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before it034bca117d814e064e9d6a0fa16c48781f9b27ba
Broken version dated2015-03-26
Modulepid.__init__
Units changedPidFile
Fingerprint855e230fb02d58c0
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.