liuqian1990 / memcached

Automatically exported from code.google.com/p/memcached
0 stars 0 forks source link

PID file should be created before daemonizing #321

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Some monitoring programs rely on the presence of a PID file to determine 
whether a daemon successfully started. memcached is creating the PID file after 
daemonizing, resulting in a short period where it looks as though the daemon 
failed to start. This is easy to demonstrate. Try the following several times. 
You will always see "cat: foo.pid: No such file or directory" displayed before 
the actual PID.

rm -f foo.pid; killall -q -w memcached; memcached -d -P `pwd`/foo.pid; cat 
foo.pid; sleep 1; cat foo.pid

Ideally, most of the initialization should be done before daemonizing so that 
when the fork happens, memcached is definitely going to stay up and is ready to 
take requests, but I can appreciate that this isn't always straightforward. 
However, simply creating the PID file before forking should not be a problem.

I recently had to make the same change for a Ruby project. It's a different 
language but exactly the same principle applies.

https://github.com/adhearsion/adhearsion/commit/7ed93239e281c25bc5a45a89b1f2da45
84dbc778#lib/adhearsion/foundation/custom_daemonizer.rb

Original issue reported on code.google.com by JerseyChewi@gmail.com on 22 May 2013 at 10:29

GoogleCodeExporter commented 9 years ago
I worded that badly. "forking" and "daemonizing" aren't exactly the same thing. 
The initialization can happen after forking but should occur before 
daemonizing, i.e. before the parent exits.

Original comment by JerseyChewi@gmail.com on 22 May 2013 at 10:35