The problem with PID files

April 6, 2008

The problem with PID files is that while you can use a PID to verify that your daemon isn't running, you can't use it alone to verify that your daemon is in fact present. You have to use other mechanisms, like trying to communicate with it, to verify that whatever has the PID actually is your daemon, instead of something that has inherited the PID.

Most of the time the risk that people think about with this is PID rollover: your daemon dies, the system churns through tens of thousands of processes, and something else has reused the daemon's old PID by the time you get around to trying to restart it. But PID rollover (as rare as it is in practice) is not the only problem.

Consider a daemon that is started relatively early in the system's life (at boot time or shortly afterwards), and that stores its PID file in a non-volatile location, one that isn't scrubbed at boot time. Sooner or later one of the other things that get started early on are going to get the PID that your daemon was using last time around, and then things go to heck.

(This is exactly the problem I have with pulseaudio on my home machine. My work machine gets rebooted much less frequently, which both lowers the chance that it will happen and increases the chance that the stuff that removes old unused files from /tmp will have deleted the PID file by the time I reboot.)

Also, please do not use your PID file to send signals to your daemon before first verifying that it actually is your daemon. If the process is not your daemon, there is no guarantee that it will be at all happy to receive a SIGUSR1 or whatever out of the blue.


Comments on this page:

From 75.194.3.108 at 2008-04-08 07:34:43:

Wouldn't a simpler dictum be "don't store your PID files in a place that won't get scrubbed at boottime"?

I mean, the other things you mention are all well and good, but I can't imagine why anyone would think it a good idea to store PID files somewhere that isn't scrubbed at boot.

A obvious corollary: OS vendors should provide well-known locations that are scrubbed at boot, and that are writeable by any user who will be starting long-running daemon processes.

By cks at 2008-04-08 08:20:24:

The general PID rollover issue is present even with a volatile place to store PID files, so I still want people to deal with the general problem.

From 74.59.152.153 at 2008-04-08 23:31:27:

Just as a side note, the PID rollover/reuse problem can append much faster on OSes that randomizes PID numbers allocation, like OpenBSD for example.

-- Koider

Written on 06 April 2008.
« What I needed to make my custom Fedora 8 environment work
Get statistics »

Page tools: View Source, View Normal, Add Comment.
Search:
Login: Password:
Atom Syndication: Recent Comments.

Last modified: Sun Apr 6 22:22:40 2008
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.