[Twisted-Python] My twisted application could not start after reboot be cause of "twistd.pid"

Tim Allen tim at commsecure.com.au
Tue Mar 16 03:27:40 EDT 2010


On 03/16/2010 06:07 PM, Peter Cai wrote:
> I think an init.d script could resolve this problem.
> But I am curious about why "turning off power without shutdown twisted
> server before" could cause such a problem.
>
> Is it a bug or it has some more fundamental reason?

When twistd starts up, it creates a .pid file as a signal that it's 
running. If you try and start it a second time, it notices that the .pid 
file exists, and refuses to run a second time. When twistd is shut down 
cleanly, it removes the .pid file so that it can be run again.

Unfortunately, this means that if twistd is shutdown *uncleanly* (by a 
power failure, or SIGKILL, or some other means), the .pid file must be 
removed before twistd will start again.

This isn't really a bug in twistd as such; almost all Unix daemons use 
the same fundamental system. Try running "ls /var/run/*.pid" on a Linux 
system, for example.

It's possible to add refinements to the basic .pid system, to make it 
more resistant to this kind of problem (off the top of my head, checking 
that the named PID exists, that it's a process of the right kind, 
checking for stale .pid files with exclusive file locks), but all of 
them require more code to implement, reduce portability, and still have 
corner-cases that would require an admin to manually delete the .pid 
file anyway.



More information about the Twisted-Python mailing list