3 require 'unicorn/socket'
4 require 'unicorn/const'
5 require 'unicorn/http_request'
6 require 'unicorn/http_response'
7 require 'unicorn/configurator'
9 # Unicorn module containing all of the classes (include C extensions) for running
10 # a Unicorn web server. It contains a minimalist HTTP server with just enough
11 # functionality to service web application requests fast as possible.
14 def run(app, options = {})
15 HttpServer.new(app, options).start.join
19 # This is the process manager of Unicorn. This manages worker
20 # processes which in turn handle the I/O and application process.
21 # Listener sockets are started in the master process and shared with
22 # forked worker children.
26 include ::Unicorn::SocketHelper
29 :argv => ARGV.map { |arg| arg.dup },
32 :environ => {}.merge!(ENV),
36 Worker = Struct.new(:nr, :tempfile) unless defined?(Worker)
38 # worker objects may be compared to just plain numbers
44 # Creates a working server on host:port (strange things happen if
45 # port isn't a Number). Use HttpServer::run to start the server and
46 # HttpServer.workers.join to join the thread that's processing
47 # incoming requests on the socket.
48 def initialize(app, options = {})
49 start_ctx = options.delete(:start_ctx)
50 @start_ctx = DEFAULT_START_CTX.dup
51 @start_ctx.merge!(start_ctx) if start_ctx
56 @io_purgatory = [] # prevents IO objects in here from being GC-ed
57 @request = @rd_sig = @wr_sig = nil
59 @config = Configurator.new(options.merge(:use_defaults => true))
60 @config.commit!(self, :skip => [:listeners, :pid])
64 # Runs the thing. Returns self so you can run join on it
66 BasicSocket.do_not_reverse_lookup = true
68 # inherit sockets from parents, they need to be plain Socket objects
69 # before they become UNIXServer or TCPServer
70 inherited = ENV['UNICORN_FD'].to_s.split(/,/).map do |fd|
71 io = Socket.for_fd(fd.to_i)
72 set_server_sockopt(io)
74 logger.info "inherited: #{io} fd=#{fd} addr=#{sock_name(io)}"
78 config_listeners = @config[:listeners].dup
79 @listeners.replace(inherited)
81 # we start out with generic Socket objects that get cast to either
82 # TCPServer or UNIXServer objects; but since the Socket objects
83 # share the same OS-level file descriptor as the higher-level *Server
84 # objects; we need to prevent Socket objects from being garbage-collected
85 config_listeners -= listener_names
86 config_listeners.each { |addr| listen(addr) }
87 listen(Const::DEFAULT_LISTENER) if @listeners.empty?
88 self.pid = @config[:pid]
93 # replaces current listener set with +listeners+. This will
94 # close the socket if it will not exist in the new listener set
95 def listeners=(listeners)
96 cur_names = listener_names
97 set_names = listener_names(listeners)
98 dead_names = cur_names - set_names
100 @listeners.delete_if do |io|
101 if dead_names.include?(sock_name(io))
102 @io_purgatory.delete_if { |pio| pio.fileno == io.fileno }
110 (set_names - cur_names).each { |addr| listen(addr) }
113 # sets the path for the PID file of the master process
116 if x = valid_pid?(path)
117 return path if @pid && path == @pid && x == $$
118 raise ArgumentError, "Already running on PID:#{x} " \
119 "(or pid=#{path} is stale)"
121 File.open(path, 'wb') { |fp| fp.syswrite("#{$$}\n") }
122 at_exit { unlink_pid_safe(path) }
124 unlink_pid_safe(@pid) if @pid && @pid != path
128 # sets the path for running the master and worker process, useful for
129 # running and reexecuting from a symlinked path like Capistrano allows
131 Dir.chdir(path) if path
135 # add a given address to the +listeners+ set, idempotently
136 # Allows workers to add a private, per-process listener via the
137 # @after_fork hook. Very useful for debugging and testing.
139 return if String === address && listener_names.include?(address)
141 if io = bind_listen(address, @backlog)
142 if Socket == io.class
146 logger.info "#{io} listening on PID:#{$$} " \
147 "fd=#{io.fileno} addr=#{sock_name(io)}"
150 logger.error "adding listener failed addr=#{address} (in use)"
151 raise Errno::EADDRINUSE, address
155 # monitors children and receives signals forever
156 # (or until a termination signal is sent). This handles signals
157 # one-at-a-time time and we'll happily drop signals in case somebody
158 # is signalling us too often.
160 # this pipe is used to wake us up from select(2) in #join when signals
161 # are trapped. See trap_deferred
162 @rd_sig, @wr_sig = IO.pipe unless (@rd_sig && @wr_sig)
163 @rd_sig.nonblock = @wr_sig.nonblock = true
166 $0 = "unicorn master"
167 logger.info "master process ready" # test relies on this message
174 spawn_missing_workers
175 when 'QUIT' # graceful shutdown
177 when 'TERM', 'INT' # immediate shutdown
180 when 'USR1' # user-defined (probably something like log reopening)
181 kill_each_worker('USR1')
183 when 'USR2' # exec binary, stay alive in case something went wrong
187 if @config.config_file
190 redo # immediate reaping since we may have QUIT workers
191 else # exec binary and exit if there's no config file
192 logger.info "config_file not present, reexecuting binary"
197 logger.error "master process in unknown mode: #{@mode}, resetting"
203 IO.select([@rd_sig], nil, nil, 1) or next
204 rescue Errno::EINTR # next
206 ready[0] && ready[0][0] or next
207 begin # just consume the pipe when we're awakened, @mode is set
208 loop { @rd_sig.sysread(Const::CHUNK_SIZE) }
209 rescue Errno::EAGAIN, Errno::EINTR # next
215 logger.error "Unhandled master loop exception #{e.inspect}."
216 logger.error e.backtrace.join("\n")
220 stop # gracefully shutdown all workers on our way out
221 logger.info "master PID:#{$$} join complete"
224 # Terminates all workers, but does not exit master process
225 def stop(graceful = true)
226 kill_each_worker(graceful ? 'QUIT' : 'TERM')
230 until @workers.empty?
233 (timeleft -= step) > 0 and next
234 kill_each_worker('KILL')
242 # list of signals we care about and trap in master.
243 TRAP_SIGS = %w(QUIT INT TERM USR1 USR2 HUP).map { |x| x.freeze }.freeze
245 # defer a signal for later processing in #join (master process)
246 def trap_deferred(signal)
247 trap(signal) do |sig_nr|
248 # we only handle/defer one signal at a time and ignore all others
249 # until we're ready again. Queueing signals can lead to more bugs,
250 # and simplicity is the most important thing
251 TRAP_SIGS.each { |sig| trap(sig, 'IGNORE') }
255 @wr_sig.syswrite('.') # wakeup master process from IO.select
267 TRAP_SIGS.each { |sig| trap_deferred(sig) }
270 # reaps all unreaped workers
274 pid = waitpid(-1, WNOHANG) or break
275 if @reexec_pid == pid
276 logger.error "reaped exec()-ed PID:#{pid} status=#{$?.exitstatus}"
278 self.pid = @pid.chomp('.oldbin') if @pid
280 worker = @workers.delete(pid)
281 worker.tempfile.close rescue nil
282 logger.info "reaped PID:#{pid} " \
283 "worker=#{worker.nr rescue 'unknown'} " \
284 "status=#{$?.exitstatus}"
291 # reexecutes the @start_ctx with a new binary
295 Process.kill(0, @reexec_pid)
296 logger.error "reexec-ed child already running PID:#{@reexec_pid}"
304 old_pid = "#{@pid}.oldbin"
307 self.pid = old_pid # clear the path for a new pid file
309 logger.error "old PID:#{valid_pid?(old_pid)} running with " \
310 "existing pid=#{old_pid}, refusing rexec"
313 logger.error "error writing pid=#{old_pid} #{e.class} #{e.message}"
318 @reexec_pid = fork do
319 @rd_sig.close if @rd_sig
320 @wr_sig.close if @wr_sig
321 @workers.values.each { |other| other.tempfile.close rescue nil }
323 ENV.replace(@start_ctx[:environ])
324 ENV['UNICORN_FD'] = @listeners.map { |sock| sock.fileno }.join(',')
325 File.umask(@start_ctx[:umask])
326 Dir.chdir(@cwd || @start_ctx[:cwd])
327 cmd = [ @start_ctx[:zero] ] + @start_ctx[:argv]
328 logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
333 # forcibly terminate all workers that haven't checked in in @timeout
334 # seconds. The timeout is implemented using an unlinked tempfile
335 # shared between the parent process and each worker. The worker
336 # runs File#chmod to modify the ctime of the tempfile. If the ctime
337 # is stale for >@timeout seconds, then we'll kill the corresponding
339 def murder_lazy_workers
341 @workers.each_pair do |pid, worker|
342 (now - worker.tempfile.ctime) <= @timeout and next
343 logger.error "worker=#{worker.nr} PID:#{pid} is too old, killing"
344 kill_worker('KILL', pid) # take no prisoners for @timeout violations
345 worker.tempfile.close rescue nil
349 def spawn_missing_workers
350 return if @workers.size == @worker_processes
351 (0...@worker_processes).each do |worker_nr|
352 @workers.values.include?(worker_nr) and next
353 tempfile = Tempfile.new('') # as short as possible to save dir space
354 tempfile.unlink # don't allow other processes to find or see it
356 worker = Worker.new(worker_nr, tempfile)
357 @before_fork.call(self, worker.nr)
358 pid = fork { worker_loop(worker) }
359 @workers[pid] = worker
363 # once a client is accepted, it is processed in its entirety here
364 # in 3 easy steps: read request, call app, write app response
365 def process_client(client)
366 env = @request.read(client) or return
367 app_response = @app.call(env)
368 HttpResponse.write(client, app_response)
369 rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
370 client.closed? or client.close rescue nil
372 logger.error "Read error: #{e.inspect}"
373 logger.error e.backtrace.join("\n")
376 client.closed? or client.close
378 logger.error "Client error: #{e.inspect}"
379 logger.error e.backtrace.join("\n")
384 # gets rid of stuff the worker has no business keeping track of
385 # to free some resources and drops all sig handlers.
386 # traps for USR1, USR2, and HUP may be set in the @after_fork Proc
388 def init_worker_process(worker)
389 TRAP_SIGS.each { |sig| trap(sig, 'IGNORE') }
390 trap('CHLD', 'DEFAULT')
391 $0 = "unicorn worker[#{worker.nr}]"
392 @rd_sig.close if @rd_sig
393 @wr_sig.close if @wr_sig
394 @workers.values.each { |other| other.tempfile.close rescue nil }
397 @mode = @start_ctx = @workers = @rd_sig = @wr_sig = nil
398 @listeners.each { |sock| set_cloexec(sock) }
399 ENV.delete('UNICORN_FD')
400 @after_fork.call(self, worker.nr) if @after_fork
401 @request = HttpRequest.new(logger)
404 # runs inside each forked worker, this sits around and waits
405 # for connections and doesn't die until the parent dies (or is
406 # given a INT, QUIT, or TERM signal)
407 def worker_loop(worker)
408 init_worker_process(worker)
410 tempfile = worker.tempfile
414 %w(TERM INT).each { |sig| trap(sig) { exit(0) } } # instant shutdown
417 @listeners.each { |sock| sock.close rescue nil } # break IO.select
420 while alive && @master_pid == ppid
421 # we're a goner in @timeout seconds anyways if tempfile.chmod
422 # breaks, so don't trap the exception. Using fchmod() since
423 # futimes() is not available in base Ruby and I very strongly
424 # prefer temporary files to be unlinked for security,
425 # performance and reliability reasons, so utime is out. No-op
426 # changes with chmod doesn't update ctime on all filesystems; so
427 # we increment our counter each and every time.
428 tempfile.chmod(nr += 1)
439 accepted = client.sync = true
440 client.nonblock = false
441 set_client_sockopt(client) if TCPSocket === client
442 process_client(client)
443 rescue Errno::ECONNABORTED
444 # client closed the socket even before accept
445 if client && !client.closed?
446 client.close rescue nil
449 tempfile.chmod(nr += 1)
453 # make the following bet: if we accepted clients this round,
454 # we're probably reasonably busy, so avoid calling select(2)
455 # and try to do a blind non-blocking accept(2) on everything
456 # before we sleep again in select
461 tempfile.chmod(nr += 1)
462 # timeout used so we can detect parent death:
463 ret = IO.select(@listeners, nil, nil, @timeout/2.0) or next
465 rescue Errno::EBADF => e
469 rescue SystemExit => e
473 logger.error "Unhandled listen loop exception #{e.inspect}."
474 logger.error e.backtrace.join("\n")
480 # delivers a signal to a worker and fails gracefully if the worker
481 # is no longer running.
482 def kill_worker(signal, pid)
486 worker = @workers.delete(pid) and worker.tempfile.close rescue nil
490 # delivers a signal to each worker
491 def kill_each_worker(signal)
492 @workers.keys.each { |pid| kill_worker(signal, pid) }
495 # unlinks a PID file at given +path+ if it contains the current PID
496 # useful as an at_exit handler.
497 def unlink_pid_safe(path)
498 (File.read(path).to_i == $$ and File.unlink(path)) rescue nil
501 # returns a PID if a given path contains a non-stale PID file,
504 if File.exist?(path) && (pid = File.read(path).to_i) > 1
516 logger.info "reloading config_file=#{@config.config_file}"
518 @config.commit!(self)
519 kill_each_worker('QUIT')
520 logger.info "done reloading config_file=#{@config.config_file}"
522 logger.error "error reloading config_file=#{@config.config_file}: " \
523 "#{e.class} #{e.message}"
527 # returns an array of string names for the given listener array
528 def listener_names(listeners = @listeners)
529 listeners.map { |io| sock_name(io) }