Transfer-Encoding: chunked streaming input support
[unicorn.git] / lib / unicorn.rb
blobf45c6131a89118b1012537af836c2f1499e8e482
1 require 'fcntl'
2 require 'unicorn/socket_helper'
3 autoload :Rack, 'rack'
5 # Unicorn module containing all of the classes (include C extensions) for running
6 # a Unicorn web server.  It contains a minimalist HTTP server with just enough
7 # functionality to service web application requests fast as possible.
8 module Unicorn
9   autoload :Const, 'unicorn/const'
10   autoload :HttpRequest, 'unicorn/http_request'
11   autoload :HttpResponse, 'unicorn/http_response'
12   autoload :Configurator, 'unicorn/configurator'
13   autoload :TeeInput, 'unicorn/tee_input'
14   autoload :ChunkedReader, 'unicorn/chunked_reader'
15   autoload :Util, 'unicorn/util'
17   class << self
18     def run(app, options = {})
19       HttpServer.new(app, options).start.join
20     end
21   end
23   # This is the process manager of Unicorn. This manages worker
24   # processes which in turn handle the I/O and application process.
25   # Listener sockets are started in the master process and shared with
26   # forked worker children.
27   class HttpServer
28     attr_reader :logger
29     include ::Unicorn::SocketHelper
31     # prevents IO objects in here from being GC-ed
32     IO_PURGATORY = []
34     # all bound listener sockets
35     LISTENERS = []
37     # This hash maps PIDs to Workers
38     WORKERS = {}
40     # See: http://cr.yp.to/docs/selfpipe.html
41     SELF_PIPE = []
43     # signal queue used for self-piping
44     SIG_QUEUE = []
46     # constant lookups are faster and we're single-threaded/non-reentrant
47     REQUEST = HttpRequest.new
49     # We populate this at startup so we can figure out how to reexecute
50     # and upgrade the currently running instance of Unicorn
51     START_CTX = {
52       :argv => ARGV.map { |arg| arg.dup },
53       # don't rely on Dir.pwd here since it's not symlink-aware, and
54       # symlink dirs are the default with Capistrano...
55       :cwd => `/bin/sh -c pwd`.chomp("\n"),
56       :zero => $0.dup,
57     }
59     Worker = Struct.new(:nr, :tempfile) unless defined?(Worker)
60     class Worker
61       # worker objects may be compared to just plain numbers
62       def ==(other_nr)
63         self.nr == other_nr
64       end
65     end
67     # Creates a working server on host:port (strange things happen if
68     # port isn't a Number).  Use HttpServer::run to start the server and
69     # HttpServer.run.join to join the thread that's processing
70     # incoming requests on the socket.
71     def initialize(app, options = {})
72       @app = app
73       @pid = nil
74       @reexec_pid = 0
75       @init_listeners = options[:listeners] ? options[:listeners].dup : []
76       @config = Configurator.new(options.merge(:use_defaults => true))
77       @listener_opts = {}
78       @config.commit!(self, :skip => [:listeners, :pid])
79       @orig_app = app
80     end
82     # Runs the thing.  Returns self so you can run join on it
83     def start
84       BasicSocket.do_not_reverse_lookup = true
86       # inherit sockets from parents, they need to be plain Socket objects
87       # before they become UNIXServer or TCPServer
88       inherited = ENV['UNICORN_FD'].to_s.split(/,/).map do |fd|
89         io = Socket.for_fd(fd.to_i)
90         set_server_sockopt(io, @listener_opts[sock_name(io)])
91         IO_PURGATORY << io
92         logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
93         server_cast(io)
94       end
96       config_listeners = @config[:listeners].dup
97       LISTENERS.replace(inherited)
99       # we start out with generic Socket objects that get cast to either
100       # TCPServer or UNIXServer objects; but since the Socket objects
101       # share the same OS-level file descriptor as the higher-level *Server
102       # objects; we need to prevent Socket objects from being garbage-collected
103       config_listeners -= listener_names
104       if config_listeners.empty? && LISTENERS.empty?
105         config_listeners << Unicorn::Const::DEFAULT_LISTEN
106       end
107       config_listeners.each { |addr| listen(addr) }
108       raise ArgumentError, "no listeners" if LISTENERS.empty?
109       self.pid = @config[:pid]
110       build_app! if @preload_app
111       maintain_worker_count
112       self
113     end
115     # replaces current listener set with +listeners+.  This will
116     # close the socket if it will not exist in the new listener set
117     def listeners=(listeners)
118       cur_names, dead_names = [], []
119       listener_names.each do |name|
120         if "/" == name[0..0]
121           # mark unlinked sockets as dead so we can rebind them
122           (File.socket?(name) ? cur_names : dead_names) << name
123         else
124           cur_names << name
125         end
126       end
127       set_names = listener_names(listeners)
128       dead_names += cur_names - set_names
129       dead_names.uniq!
131       LISTENERS.delete_if do |io|
132         if dead_names.include?(sock_name(io))
133           IO_PURGATORY.delete_if do |pio|
134             pio.fileno == io.fileno && (pio.close rescue nil).nil? # true
135           end
136           (io.close rescue nil).nil? # true
137         else
138           set_server_sockopt(io, @listener_opts[sock_name(io)])
139           false
140         end
141       end
143       (set_names - cur_names).each { |addr| listen(addr) }
144     end
146     def stdout_path=(path); redirect_io($stdout, path); end
147     def stderr_path=(path); redirect_io($stderr, path); end
149     def logger=(obj)
150       REQUEST.logger = @logger = obj
151     end
153     # sets the path for the PID file of the master process
154     def pid=(path)
155       if path
156         if x = valid_pid?(path)
157           return path if @pid && path == @pid && x == $$
158           raise ArgumentError, "Already running on PID:#{x} " \
159                                "(or pid=#{path} is stale)"
160         end
161       end
162       unlink_pid_safe(@pid) if @pid
163       File.open(path, 'wb') { |fp| fp.syswrite("#$$\n") } if path
164       @pid = path
165     end
167     # add a given address to the +listeners+ set, idempotently
168     # Allows workers to add a private, per-process listener via the
169     # @after_fork hook.  Very useful for debugging and testing.
170     def listen(address, opt = {}.merge(@listener_opts[address] || {}))
171       return if String === address && listener_names.include?(address)
173       if io = bind_listen(address, opt)
174         unless TCPServer === io || UNIXServer === io
175           IO_PURGATORY << io
176           io = server_cast(io)
177         end
178         logger.info "listening on addr=#{sock_name(io)} fd=#{io.fileno}"
179         LISTENERS << io
180       else
181         logger.error "adding listener failed addr=#{address} (in use)"
182         raise Errno::EADDRINUSE, address
183       end
184     end
186     # monitors children and receives signals forever
187     # (or until a termination signal is sent).  This handles signals
188     # one-at-a-time time and we'll happily drop signals in case somebody
189     # is signalling us too often.
190     def join
191       # this pipe is used to wake us up from select(2) in #join when signals
192       # are trapped.  See trap_deferred
193       init_self_pipe!
194       respawn = true
196       QUEUE_SIGS.each { |sig| trap_deferred(sig) }
197       trap(:CHLD) { |sig_nr| awaken_master }
198       proc_name 'master'
199       logger.info "master process ready" # test_exec.rb relies on this message
200       begin
201         loop do
202           reap_all_workers
203           case SIG_QUEUE.shift
204           when nil
205             murder_lazy_workers
206             maintain_worker_count if respawn
207             master_sleep
208           when :QUIT # graceful shutdown
209             break
210           when :TERM, :INT # immediate shutdown
211             stop(false)
212             break
213           when :USR1 # rotate logs
214             logger.info "master reopening logs..."
215             Unicorn::Util.reopen_logs
216             logger.info "master done reopening logs"
217             kill_each_worker(:USR1)
218           when :USR2 # exec binary, stay alive in case something went wrong
219             reexec
220           when :WINCH
221             if Process.ppid == 1 || Process.getpgrp != $$
222               respawn = false
223               logger.info "gracefully stopping all workers"
224               kill_each_worker(:QUIT)
225             else
226               logger.info "SIGWINCH ignored because we're not daemonized"
227             end
228           when :TTIN
229             @worker_processes += 1
230           when :TTOU
231             @worker_processes -= 1 if @worker_processes > 0
232           when :HUP
233             respawn = true
234             if @config.config_file
235               load_config!
236               redo # immediate reaping since we may have QUIT workers
237             else # exec binary and exit if there's no config file
238               logger.info "config_file not present, reexecuting binary"
239               reexec
240               break
241             end
242           end
243         end
244       rescue Errno::EINTR
245         retry
246       rescue Object => e
247         logger.error "Unhandled master loop exception #{e.inspect}."
248         logger.error e.backtrace.join("\n")
249         retry
250       end
251       stop # gracefully shutdown all workers on our way out
252       logger.info "master complete"
253       unlink_pid_safe(@pid) if @pid
254     end
256     # Terminates all workers, but does not exit master process
257     def stop(graceful = true)
258       kill_each_worker(graceful ? :QUIT : :TERM)
259       timeleft = @timeout
260       step = 0.2
261       reap_all_workers
262       until WORKERS.empty?
263         sleep(step)
264         reap_all_workers
265         (timeleft -= step) > 0 and next
266         kill_each_worker(:KILL)
267       end
268     ensure
269       self.listeners = []
270     end
272     private
274     # list of signals we care about and trap in master.
275     QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP,
276                    :TTIN, :TTOU ].freeze
278     # defer a signal for later processing in #join (master process)
279     def trap_deferred(signal)
280       trap(signal) do |sig_nr|
281         if SIG_QUEUE.size < 5
282           SIG_QUEUE << signal
283           awaken_master
284         else
285           logger.error "ignoring SIG#{signal}, queue=#{SIG_QUEUE.inspect}"
286         end
287       end
288     end
290     # wait for a signal hander to wake us up and then consume the pipe
291     # Wake up every second anyways to run murder_lazy_workers
292     def master_sleep
293       begin
294         ready = IO.select([SELF_PIPE.first], nil, nil, 1) or return
295         ready.first && ready.first.first or return
296         loop { SELF_PIPE.first.read_nonblock(Const::CHUNK_SIZE) }
297       rescue Errno::EAGAIN, Errno::EINTR
298       end
299     end
301     def awaken_master
302       begin
303         SELF_PIPE.last.write_nonblock('.') # wakeup master process from select
304       rescue Errno::EAGAIN, Errno::EINTR
305         # pipe is full, master should wake up anyways
306         retry
307       end
308     end
310     # reaps all unreaped workers
311     def reap_all_workers
312       begin
313         loop do
314           pid, status = Process.waitpid2(-1, Process::WNOHANG)
315           pid or break
316           if @reexec_pid == pid
317             logger.error "reaped #{status.inspect} exec()-ed"
318             @reexec_pid = 0
319             self.pid = @pid.chomp('.oldbin') if @pid
320             proc_name 'master'
321           else
322             worker = WORKERS.delete(pid) and worker.tempfile.close rescue nil
323             logger.info "reaped #{status.inspect} " \
324                         "worker=#{worker.nr rescue 'unknown'}"
325           end
326         end
327       rescue Errno::ECHILD
328       end
329     end
331     # reexecutes the START_CTX with a new binary
332     def reexec
333       if @reexec_pid > 0
334         begin
335           Process.kill(0, @reexec_pid)
336           logger.error "reexec-ed child already running PID:#{@reexec_pid}"
337           return
338         rescue Errno::ESRCH
339           @reexec_pid = 0
340         end
341       end
343       if @pid
344         old_pid = "#{@pid}.oldbin"
345         prev_pid = @pid.dup
346         begin
347           self.pid = old_pid  # clear the path for a new pid file
348         rescue ArgumentError
349           logger.error "old PID:#{valid_pid?(old_pid)} running with " \
350                        "existing pid=#{old_pid}, refusing rexec"
351           return
352         rescue Object => e
353           logger.error "error writing pid=#{old_pid} #{e.class} #{e.message}"
354           return
355         end
356       end
358       @reexec_pid = fork do
359         listener_fds = LISTENERS.map { |sock| sock.fileno }
360         ENV['UNICORN_FD'] = listener_fds.join(',')
361         Dir.chdir(START_CTX[:cwd])
362         cmd = [ START_CTX[:zero] ] + START_CTX[:argv]
364         # avoid leaking FDs we don't know about, but let before_exec
365         # unset FD_CLOEXEC, if anything else in the app eventually
366         # relies on FD inheritence.
367         (3..1024).each do |io|
368           next if listener_fds.include?(io)
369           io = IO.for_fd(io) rescue nil
370           io or next
371           IO_PURGATORY << io
372           io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
373         end
374         logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
375         @before_exec.call(self)
376         exec(*cmd)
377       end
378       proc_name 'master (old)'
379     end
381     # forcibly terminate all workers that haven't checked in in @timeout
382     # seconds.  The timeout is implemented using an unlinked tempfile
383     # shared between the parent process and each worker.  The worker
384     # runs File#chmod to modify the ctime of the tempfile.  If the ctime
385     # is stale for >@timeout seconds, then we'll kill the corresponding
386     # worker.
387     def murder_lazy_workers
388       diff = stat = nil
389       WORKERS.dup.each_pair do |pid, worker|
390         stat = begin
391           worker.tempfile.stat
392         rescue => e
393           logger.warn "worker=#{worker.nr} PID:#{pid} stat error: #{e.inspect}"
394           kill_worker(:QUIT, pid)
395           next
396         end
397         stat.mode == 0100000 and next
398         (diff = (Time.now - stat.ctime)) <= @timeout and next
399         logger.error "worker=#{worker.nr} PID:#{pid} timeout " \
400                      "(#{diff}s > #{@timeout}s), killing"
401         kill_worker(:KILL, pid) # take no prisoners for @timeout violations
402       end
403     end
405     def spawn_missing_workers
406       (0...@worker_processes).each do |worker_nr|
407         WORKERS.values.include?(worker_nr) and next
408         begin
409           Dir.chdir(START_CTX[:cwd])
410         rescue Errno::ENOENT => err
411           logger.fatal "#{err.inspect} (#{START_CTX[:cwd]})"
412           SIG_QUEUE << :QUIT # forcibly emulate SIGQUIT
413           return
414         end
415         tempfile = Tempfile.new(nil) # as short as possible to save dir space
416         tempfile.unlink # don't allow other processes to find or see it
417         worker = Worker.new(worker_nr, tempfile)
418         @before_fork.call(self, worker)
419         pid = fork { worker_loop(worker) }
420         WORKERS[pid] = worker
421       end
422     end
424     def maintain_worker_count
425       (off = WORKERS.size - @worker_processes) == 0 and return
426       off < 0 and return spawn_missing_workers
427       WORKERS.dup.each_pair { |pid,w|
428         w.nr >= @worker_processes and kill_worker(:QUIT, pid) rescue nil
429       }
430     end
432     # once a client is accepted, it is processed in its entirety here
433     # in 3 easy steps: read request, call app, write app response
434     def process_client(app, client)
435       HttpResponse.write(client, app.call(REQUEST.read(client)))
436     # if we get any error, try to write something back to the client
437     # assuming we haven't closed the socket, but don't get hung up
438     # if the socket is already closed or broken.  We'll always ensure
439     # the socket is closed at the end of this function
440     rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
441       client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
442       client.close rescue nil
443     rescue HttpParserError # try to tell the client they're bad
444       client.write_nonblock(Const::ERROR_400_RESPONSE) rescue nil
445       client.close rescue nil
446     rescue Object => e
447       client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
448       client.close rescue nil
449       logger.error "Read error: #{e.inspect}"
450       logger.error e.backtrace.join("\n")
451     end
453     # gets rid of stuff the worker has no business keeping track of
454     # to free some resources and drops all sig handlers.
455     # traps for USR1, USR2, and HUP may be set in the @after_fork Proc
456     # by the user.
457     def init_worker_process(worker)
458       QUEUE_SIGS.each { |sig| trap(sig, 'IGNORE') }
459       trap(:CHLD, 'DEFAULT')
460       SIG_QUEUE.clear
461       proc_name "worker[#{worker.nr}]"
462       START_CTX.clear
463       init_self_pipe!
464       WORKERS.values.each { |other| other.tempfile.close! rescue nil }
465       WORKERS.clear
466       LISTENERS.each { |sock| sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
467       worker.tempfile.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
468       @after_fork.call(self, worker) # can drop perms
469       @timeout /= 2.0 # halve it for select()
470       HttpRequest::TEE.setup
471       build_app! unless @preload_app
472     end
474     def reopen_worker_logs(worker_nr)
475       @logger.info "worker=#{worker_nr} reopening logs..."
476       Unicorn::Util.reopen_logs
477       @logger.info "worker=#{worker_nr} done reopening logs"
478       init_self_pipe!
479     end
481     # runs inside each forked worker, this sits around and waits
482     # for connections and doesn't die until the parent dies (or is
483     # given a INT, QUIT, or TERM signal)
484     def worker_loop(worker)
485       master_pid = Process.ppid # slightly racy, but less memory usage
486       init_worker_process(worker)
487       nr = 0 # this becomes negative if we need to reopen logs
488       alive = worker.tempfile # tempfile is our lifeline to the master process
489       ready = LISTENERS
490       t = ti = 0
492       # closing anything we IO.select on will raise EBADF
493       trap(:USR1) { nr = -65536; SELF_PIPE.first.close rescue nil }
494       trap(:QUIT) { alive = nil; LISTENERS.each { |s| s.close rescue nil } }
495       [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
496       @logger.info "worker=#{worker.nr} ready"
497       app = @app
499       begin
500         nr < 0 and reopen_worker_logs(worker.nr)
501         nr = 0
503         # we're a goner in @timeout seconds anyways if alive.chmod
504         # breaks, so don't trap the exception.  Using fchmod() since
505         # futimes() is not available in base Ruby and I very strongly
506         # prefer temporary files to be unlinked for security,
507         # performance and reliability reasons, so utime is out.  No-op
508         # changes with chmod doesn't update ctime on all filesystems; so
509         # we change our counter each and every time (after process_client
510         # and before IO.select).
511         t == (ti = Time.now.to_i) or alive.chmod(t = ti)
513         ready.each do |sock|
514           begin
515             process_client(app, sock.accept_nonblock)
516             nr += 1
517             t == (ti = Time.now.to_i) or alive.chmod(t = ti)
518           rescue Errno::EAGAIN, Errno::ECONNABORTED
519           end
520           break if nr < 0
521         end
523         # make the following bet: if we accepted clients this round,
524         # we're probably reasonably busy, so avoid calling select()
525         # and do a speculative accept_nonblock on every listener
526         # before we sleep again in select().
527         redo unless nr == 0 # (nr < 0) => reopen logs
529         master_pid == Process.ppid or return
530         alive.chmod(t = 0)
531         begin
532           # timeout used so we can detect parent death:
533           ret = IO.select(LISTENERS, nil, SELF_PIPE, @timeout) or redo
534           ready = ret.first
535         rescue Errno::EINTR
536           ready = LISTENERS
537         rescue Errno::EBADF
538           nr < 0 or return
539         end
540       rescue Object => e
541         if alive
542           logger.error "Unhandled listen loop exception #{e.inspect}."
543           logger.error e.backtrace.join("\n")
544         end
545       end while alive
546     end
548     # delivers a signal to a worker and fails gracefully if the worker
549     # is no longer running.
550     def kill_worker(signal, pid)
551       begin
552         Process.kill(signal, pid)
553       rescue Errno::ESRCH
554         worker = WORKERS.delete(pid) and worker.tempfile.close rescue nil
555       end
556     end
558     # delivers a signal to each worker
559     def kill_each_worker(signal)
560       WORKERS.keys.each { |pid| kill_worker(signal, pid) }
561     end
563     # unlinks a PID file at given +path+ if it contains the current PID
564     # useful as an at_exit handler.
565     def unlink_pid_safe(path)
566       (File.read(path).to_i == $$ and File.unlink(path)) rescue nil
567     end
569     # returns a PID if a given path contains a non-stale PID file,
570     # nil otherwise.
571     def valid_pid?(path)
572       if File.exist?(path) && (pid = File.read(path).to_i) > 1
573         begin
574           Process.kill(0, pid)
575           return pid
576         rescue Errno::ESRCH
577         end
578       end
579       nil
580     end
582     def load_config!
583       begin
584         logger.info "reloading config_file=#{@config.config_file}"
585         @config[:listeners].replace(@init_listeners)
586         @config.reload
587         @config.commit!(self)
588         kill_each_worker(:QUIT)
589         Unicorn::Util.reopen_logs
590         @app = @orig_app
591         build_app! if @preload_app
592         logger.info "done reloading config_file=#{@config.config_file}"
593       rescue Object => e
594         logger.error "error reloading config_file=#{@config.config_file}: " \
595                      "#{e.class} #{e.message}"
596       end
597     end
599     # returns an array of string names for the given listener array
600     def listener_names(listeners = LISTENERS)
601       listeners.map { |io| sock_name(io) }
602     end
604     def build_app!
605       if @app.respond_to?(:arity) && @app.arity == 0
606         if defined?(Gem) && Gem.respond_to?(:refresh)
607           logger.info "Refreshing Gem list"
608           Gem.refresh
609         end
610         @app = @app.call
611       end
612     end
614     def proc_name(tag)
615       $0 = ([ File.basename(START_CTX[:zero]), tag ] +
616               START_CTX[:argv]).join(' ')
617     end
619     def redirect_io(io, path)
620       File.open(path, 'a') { |fp| io.reopen(fp) } if path
621       io.sync = true
622     end
624     def init_self_pipe!
625       SELF_PIPE.each { |io| io.close rescue nil }
626       SELF_PIPE.replace(IO.pipe)
627       SELF_PIPE.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
628     end
630   end