Debian Codegraph @

README.signals

raw
1 = Handling of SIGUSR1
2
3 When you request a 'graceful' restart of Apache, mod_fastcgi sends a SIGUSR1
4 to each of the fastcgi worker processes. The intention is that each one
5 should finish the current request, and then exit, at which point Apache will
6 restart it. Of course, if the worker isn't doing anything, it should die
7 immediately.
8
9 This is implemented in the fcgi C library as follows:
10
11 - a signal handler is installed for SIGUSR1, which just sets a flag
12 (shutdownPending) and returns
13 - fcgi sits inside an accept() call waiting for a new request
14 - if this accept() call terminates with EINTR, and this flag is set, then
15 it returns with no request
16
17 Unfortunately, Ruby defeats this mechanism in at least two ways:
18
19 1. Ruby installs its own signal handlers for a host of common signals,
20 including USR1. The fcgi library will not install its own handler if it
21 detects that a handler has already been set (i.e. the current handler is not
22 SIG_DFL)
23
24 2. When Ruby installs its own signal handlers, it does so with SA_RESTART
25 set. This means that the accept() call does not terminate with EINTR; it is
26 restarted automatically by the OS.
27
28 When a signal comes in during the accept(), Ruby's own handler does nothing
29 except store it in a queue to be processed later. It is only when the
30 accept() call completes, i.e. when a genuine new request comes in, that Ruby
31 takes action. Unfortunately it's too late by then, and if that
32 already-accepted request is not honoured, a 500 Internal Error will be
33 returned to the client.
34
35 The simplest solution to this would be to remove Ruby's SIGUSR1 handler
36 before initialising the FastCGI library.
37
38 However, a cleaner solution is to call rb_thread_select before going into
39 FastCGI's accept loop. If a signal happens during the select, it can be
40 handled using Ruby's normal mechanisms. This also gives a very useful
41 side-benefit, which is that FCGI::accept no longer blocks out other Ruby
42 threads. The program below demonstrates this problem; its background logging
43 thread is supposed to write a message every 10 seconds, but under older
44 versions of ruby-fcgi it does not do so if it is waiting for a new request.
45
46 #!/usr/local/bin/ruby
47 require "fcgi"
48
49 Thread.new do
50 f = File.new("/tmp/fcgi.log","a")
51 f.sync=true
52 while true
53 f.puts "#{Time.now.to_s} pid #{$$}"
54 sleep 10
55 end
56 end
57
58 FCGI.each_cgi {|cgi|
59 name = cgi['name'][0]
60 puts cgi.header
61 puts "Hey! You are #{name} " if name
62 puts "Connecting from #{cgi.remote_addr}"
63 }
64
65 Having protected the accept() with a ruby select(), you can then handle
66 signals as follows:
67
68 - call FCGI::accept (it will raise an exception if USR1 is called)
69 - install a USR1 handler
70 - process the request
71 - remove the USR1 handler
72
73 The USR1 handler should set a flag to note if a USR1 signal came in while
74 the request was being processed; you terminate the loop if it was set. The
75 overall effect is that USR1 will cause the process to terminate, but without
76 causing a half-completed request.