How to log and chain signals

Hi everyone,

I’ve been running my own ruby dispatcher on my website for a few years
now, but I keep having problems with FCGI and no amount of logging has
ever come up with any answers…

The problem is that the processes spawned and managed by apache’s fcgi
server (Fast CGI) sometimes quit for no apparent reason or keep running
even after they have been asked to shutdown.

I’ve been totally flummoxed by all this until I read today that FCGI
installs its own Signal Handlers: I had been living in a dream world
where a thread has a single path of execution and simply couldn’t figure
out where things went wrong (I’m more of a Java man than a Unix guy).

I’m fairly certain now that it’s something to do with FCGI responding
incorrectly to signals and I want to log the signal without blocking
their handling.

My aim would be to log the signal being received by the process and then
execute whatever would originally have happened.

My naive attempt at doing this for SIGTERM is below:

PREV_SIGNAL_HANDLERS = Hash.new

PREV_SIGNAL_HANDLERS[‘TERM’] = trap(“TERM”) do
puts “(pid: #{Process.pid}) TERM signal received.”;
puts “previous handler: #{PREV_SIGNAL_HANDLERS[‘TERM’]} of type:
#{PREV_SIGNAL_HANDLERS[‘TERM’].class}”

if PREV_SIGNAL_HANDLERS[‘TERM’].class == Proc.class then
$stderr.puts “(pid: #{Process.pid}) calling previous call chain”
PREV_SIGNAL_HANDLERS[‘TERM’].call
else
$stderr.puts “(pid: #{Process.pid}) calling handler:
#{PREV_SIGNAL_HANDLERS[‘TERM’]}”
trap(‘TERM’, PREV_SIGNAL_HANDLERS[‘TERM’])
Process.kill(‘TERM’, Process.pid)
end
end

This works fine in a sample script, but doesn’t seem to be working at
all when I add it to my dispatcher…

Any advice would be appreciated.

Best regards,

Frank