torfsen/service

Starting a service with service.start(block=True) makes the caller process delete the pid

dralves opened this issue · 4 comments

platform=macos
version=0.5.1

The logic on service.start() seems to rely on the caller process dying really fast, before the daemon calls self.pid_file.acquire(). If this doesn't happen (for instance because the caller does some more work or because block=True) then, when the caller dies and the automated cleanup kicks in, the caller will delete the pid file after the daemon calls self.pid_file.acquire() and the file gets deleted without being recreated.

Thanks for your report, @dralves!

I'm not sure whether I understand you correctly: in particular, which automated cleanup are you referring to? There are only two places in the code where a previously created PID file is removed:

PidFile (the one from pypi "pid"), on create(), registers a callback to be executed on process exit (line 185 of https://github.com/trbs/pid/blob/master/pid/__init__.py). This makes both the coordinator process (the one that starts the daemon process) and the daemon process race to delete this file. If the coordinator process dies fast, before the daemon process executes self.pid_file.acquire() (

self.pid_file.acquire()
) then it's ok, the coordinator deletes the file but the daemon process re-creates it after that and the file persists. If, on the other hand, the coordinator takes a bit of time to die, or blocks until the daemon is running, then it will delete the file from underneath the daemon process and the file gets deleted.

Finally found time to fix this. Thanks again, @dralves, for the detailed report!

Fix released in version 0.5.2.