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

1111 __version__ = "1.1.0"
1212
1313 DEFAULT_PID_DIR = "/var/run/"
14-logger = logging.getLogger("PidFile")
1514
1615
1716 class PidFileError(Exception):
5958 self._setup()
6059
6160 def _setup(self):
61+ self.logger = logging.getLogger("PidFile")
62+ self.logger.debug("%r entering setup", self)
6263 if self.filename is None:
6364 self.pid = os.getpid()
6465 self.filename = self._make_filename()
6566 self._register_term_signal()
6667
68+ # setup should only be performed once
69+ self.lazy = False
70+
6771 def _make_filename(self):
6872 pidname = self.pidname
6973 piddir = self.piddir
101105 signal.signal(signal.SIGTERM, term_signal_handler)
102106
103107 def check(self):
104- logger.debug("%r check pidfile: %s", self, self.filename)
105108
106109 def inner_check(fh):
107110 try:
124127 self.close(fh=fh, cleanup=False)
125128 raise PidFileAlreadyRunningError("Program already running with pid: %d" % pid)
126129
130+ if self.lazy:
131+ self._setup()
132+
133+ self.logger.debug("%r check pidfile: %s", self, self.filename)
134+
127135 if self.fh is None:
128136 if self.filename and os.path.isfile(self.filename):
129137 with open(self.filename, "r") as fh:
132140 inner_check(self.fh)
133141
134142 def create(self):
135- logger.debug("%r create pidfile: %s", self, self.filename)
136143 if self.lazy:
137144 self._setup()
145+
146+ self.logger.debug("%r create pidfile: %s", self, self.filename)
138147 self.fh = open(self.filename, 'a+')
139148 if self.lock_pidfile:
140149 try:
156165 atexit.register(self.close)
157166
158167 def close(self, fh=None, cleanup=True):
159- logger.debug("%r closing pidfile: %s", self, self.filename)
160168 if not fh:
161169 fh = self.fh
162170 try:
163- if fh is None:
171+ if fh is None or self.lazy is True:
164172 return
173+ self.logger.debug("%r closing pidfile: %s", self, self.filename)
165174 fh.close()
166175 except IOError as exc:
167176 # ignore error when file was already closed

The check that tells the two apart

failpass·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.

Other bugs found in trbs/pid