1 # -*- encoding: binary -*-
4 require 'unicorn/socket_helper'
7 # Unicorn module containing all of the classes (include C extensions) for running
8 # a Unicorn web server. It contains a minimalist HTTP server with just enough
9 # functionality to service web application requests fast as possible.
11 autoload :Const, 'unicorn/const'
12 autoload :HttpRequest, 'unicorn/http_request'
13 autoload :HttpResponse, 'unicorn/http_response'
14 autoload :Configurator, 'unicorn/configurator'
15 autoload :TeeInput, 'unicorn/tee_input'
16 autoload :Util, 'unicorn/util'
19 def run(app, options = {})
20 HttpServer.new(app, options).start.join
24 # This is the process manager of Unicorn. This manages worker
25 # processes which in turn handle the I/O and application process.
26 # Listener sockets are started in the master process and shared with
27 # forked worker children.
29 class HttpServer < Struct.new(:listener_opts, :timeout, :worker_processes,
30 :before_fork, :after_fork, :before_exec,
31 :logger, :pid, :app, :preload_app,
32 :reexec_pid, :orig_app, :init_listeners,
34 include ::Unicorn::SocketHelper
36 # prevents IO objects in here from being GC-ed
39 # all bound listener sockets
42 # This hash maps PIDs to Workers
45 # We use SELF_PIPE differently in the master and worker processes:
47 # * The master process never closes or reinitializes this once
48 # initialized. Signal handlers in the master process will write to
49 # it to wake up the master from IO.select in exactly the same manner
50 # djb describes in http://cr.yp.to/docs/selfpipe.html
52 # * The workers immediately close the pipe they inherit from the
53 # master and replace it with a new pipe after forking. This new
54 # pipe is also used to wakeup from IO.select from inside (worker)
55 # signal handlers. However, workers *close* the pipe descriptors in
56 # the signal handlers to raise EBADF in IO.select instead of writing
57 # like we do in the master. We cannot easily use the reader set for
58 # IO.select because LISTENERS is already that set, and it's extra
59 # work (and cycles) to distinguish the pipe FD from the reader set
60 # once IO.select returns. So we're lazy and just close the pipe when
61 # a (rare) signal arrives in the worker and reinitialize the pipe later.
64 # signal queue used for self-piping
67 # constant lookups are faster and we're single-threaded/non-reentrant
68 REQUEST = HttpRequest.new
70 # We populate this at startup so we can figure out how to reexecute
71 # and upgrade the currently running instance of Unicorn
72 # This Hash is considered a stable interface and changing its contents
73 # will allow you to switch between different installations of Unicorn
74 # or even different installations of the same applications without
75 # downtime. Keys of this constant Hash are described as follows:
77 # * 0 - the path to the unicorn/unicorn_rails executable
78 # * :argv - a deep copy of the ARGV array the executable originally saw
79 # * :cwd - the working directory of the application, this is where
80 # you originally started Unicorn.
82 # The following example may be used in your Unicorn config file to
83 # change your working directory during a config reload (HUP) without
84 # upgrading or restarting:
86 # Dir.chdir(Unicorn::HttpServer::START_CTX[:cwd] = path)
88 # To change your unicorn executable to a different path without downtime,
89 # you can set the following in your Unicorn config file, HUP and then
90 # continue with the traditional USR2 + QUIT upgrade steps:
92 # Unicorn::HttpServer::START_CTX[0] = "/home/bofh/1.9.2/bin/unicorn"
94 :argv => ARGV.map { |arg| arg.dup },
95 # don't rely on Dir.pwd here since it's not symlink-aware, and
96 # symlink dirs are the default with Capistrano...
97 :cwd => `/bin/sh -c pwd`.chomp("\n"),
101 # This class and its members can be considered a stable interface
102 # and will not change in a backwards-incompatible fashion between
103 # releases of Unicorn. You may need to access it in the
104 # before_fork/after_fork hooks. See the Unicorn::Configurator RDoc
106 class Worker < Struct.new(:nr, :tmp)
108 # worker objects may be compared to just plain numbers
114 # Creates a working server on host:port (strange things happen if
115 # port isn't a Number). Use HttpServer::run to start the server and
116 # HttpServer.run.join to join the thread that's processing
117 # incoming requests on the socket.
118 def initialize(app, options = {})
121 self.init_listeners = options[:listeners] ? options[:listeners].dup : []
122 self.config = Configurator.new(options.merge(:use_defaults => true))
123 self.listener_opts = {}
125 # we try inheriting listeners first, so we bind them later.
126 # we don't write the pid file until we've bound listeners in case
127 # unicorn was started twice by mistake. Even though our #pid= method
128 # checks for stale/existing pid files, race conditions are still
129 # possible (and difficult/non-portable to avoid) and can be likely
130 # to clobber the pid if the second start was in quick succession
131 # after the first, so we rely on the listener binding to fail in
132 # that case. Some tests (in and outside of this source tree) and
133 # monitoring tools may also rely on pid files existing before we
134 # attempt to connect to the listener(s)
135 config.commit!(self, :skip => [:listeners, :pid])
139 # Runs the thing. Returns self so you can run join on it
141 BasicSocket.do_not_reverse_lookup = true
143 # inherit sockets from parents, they need to be plain Socket objects
144 # before they become UNIXServer or TCPServer
145 inherited = ENV['UNICORN_FD'].to_s.split(/,/).map do |fd|
146 io = Socket.for_fd(fd.to_i)
147 set_server_sockopt(io, listener_opts[sock_name(io)])
149 logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
153 config_listeners = config[:listeners].dup
154 LISTENERS.replace(inherited)
156 # we start out with generic Socket objects that get cast to either
157 # TCPServer or UNIXServer objects; but since the Socket objects
158 # share the same OS-level file descriptor as the higher-level *Server
159 # objects; we need to prevent Socket objects from being garbage-collected
160 config_listeners -= listener_names
161 if config_listeners.empty? && LISTENERS.empty?
162 config_listeners << Unicorn::Const::DEFAULT_LISTEN
163 init_listeners << Unicorn::Const::DEFAULT_LISTEN
164 START_CTX[:argv] << "-l#{Unicorn::Const::DEFAULT_LISTEN}"
166 config_listeners.each { |addr| listen(addr) }
167 raise ArgumentError, "no listeners" if LISTENERS.empty?
168 self.pid = config[:pid]
170 build_app! if preload_app
171 maintain_worker_count
175 # replaces current listener set with +listeners+. This will
176 # close the socket if it will not exist in the new listener set
177 def listeners=(listeners)
178 cur_names, dead_names = [], []
179 listener_names.each do |name|
181 # mark unlinked sockets as dead so we can rebind them
182 (File.socket?(name) ? cur_names : dead_names) << name
187 set_names = listener_names(listeners)
188 dead_names.concat(cur_names - set_names).uniq!
190 LISTENERS.delete_if do |io|
191 if dead_names.include?(sock_name(io))
192 IO_PURGATORY.delete_if do |pio|
193 pio.fileno == io.fileno && (pio.close rescue nil).nil? # true
195 (io.close rescue nil).nil? # true
197 set_server_sockopt(io, listener_opts[sock_name(io)])
202 (set_names - cur_names).each { |addr| listen(addr) }
205 def stdout_path=(path); redirect_io($stdout, path); end
206 def stderr_path=(path); redirect_io($stderr, path); end
208 alias_method :set_pid, :pid=
211 # sets the path for the PID file of the master process
214 if x = valid_pid?(path)
215 return path if pid && path == pid && x == $$
216 raise ArgumentError, "Already running on PID:#{x} " \
217 "(or pid=#{path} is stale)"
220 unlink_pid_safe(pid) if pid
224 tmp = "#{File.dirname(path)}/#{rand}.#$$"
225 File.open(tmp, File::RDWR|File::CREAT|File::EXCL, 0644)
230 File.rename(fp.path, path)
236 # add a given address to the +listeners+ set, idempotently
237 # Allows workers to add a private, per-process listener via the
238 # after_fork hook. Very useful for debugging and testing.
239 # +:tries+ may be specified as an option for the number of times
240 # to retry, and +:delay+ may be specified as the time in seconds
241 # to delay between retries.
242 # A negative value for +:tries+ indicates the listen will be
243 # retried indefinitely, this is useful when workers belonging to
244 # different masters are spawned during a transparent upgrade.
245 def listen(address, opt = {}.merge(listener_opts[address] || {}))
246 address = config.expand_addr(address)
247 return if String === address && listener_names.include?(address)
249 delay = opt[:delay] || 0.5
250 tries = opt[:tries] || 5
252 io = bind_listen(address, opt)
253 unless TCPServer === io || UNIXServer === io
257 logger.info "listening on addr=#{sock_name(io)} fd=#{io.fileno}"
260 rescue Errno::EADDRINUSE => err
261 logger.error "adding listener failed addr=#{address} (in use)"
262 raise err if tries == 0
264 logger.error "retrying in #{delay} seconds " \
265 "(#{tries < 0 ? 'infinite' : tries} tries left)"
271 # monitors children and receives signals forever
272 # (or until a termination signal is sent). This handles signals
273 # one-at-a-time time and we'll happily drop signals in case somebody
274 # is signalling us too often.
276 # this pipe is used to wake us up from select(2) in #join when signals
277 # are trapped. See trap_deferred
281 QUEUE_SIGS.each { |sig| trap_deferred(sig) }
282 trap(:CHLD) { |sig_nr| awaken_master }
284 logger.info "master process ready" # test_exec.rb relies on this message
291 maintain_worker_count if respawn
293 when :QUIT # graceful shutdown
295 when :TERM, :INT # immediate shutdown
298 when :USR1 # rotate logs
299 logger.info "master reopening logs..."
300 Unicorn::Util.reopen_logs
301 logger.info "master done reopening logs"
302 kill_each_worker(:USR1)
303 when :USR2 # exec binary, stay alive in case something went wrong
306 if Process.ppid == 1 || Process.getpgrp != $$
308 logger.info "gracefully stopping all workers"
309 kill_each_worker(:QUIT)
311 logger.info "SIGWINCH ignored because we're not daemonized"
314 self.worker_processes += 1
316 self.worker_processes -= 1 if self.worker_processes > 0
319 if config.config_file
321 redo # immediate reaping since we may have QUIT workers
322 else # exec binary and exit if there's no config file
323 logger.info "config_file not present, reexecuting binary"
332 logger.error "Unhandled master loop exception #{e.inspect}."
333 logger.error e.backtrace.join("\n")
336 stop # gracefully shutdown all workers on our way out
337 logger.info "master complete"
338 unlink_pid_safe(pid) if pid
341 # Terminates all workers, but does not exit master process
342 def stop(graceful = true)
344 kill_each_worker(graceful ? :QUIT : :TERM)
351 (timeleft -= step) > 0 and next
352 kill_each_worker(:KILL)
358 # list of signals we care about and trap in master.
359 QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP,
362 # defer a signal for later processing in #join (master process)
363 def trap_deferred(signal)
364 trap(signal) do |sig_nr|
365 if SIG_QUEUE.size < 5
369 logger.error "ignoring SIG#{signal}, queue=#{SIG_QUEUE.inspect}"
374 # wait for a signal hander to wake us up and then consume the pipe
375 # Wake up every second anyways to run murder_lazy_workers
378 ready = IO.select([SELF_PIPE.first], nil, nil, 1) or return
379 ready.first && ready.first.first or return
380 loop { SELF_PIPE.first.read_nonblock(Const::CHUNK_SIZE) }
381 rescue Errno::EAGAIN, Errno::EINTR
387 SELF_PIPE.last.write_nonblock('.') # wakeup master process from select
388 rescue Errno::EAGAIN, Errno::EINTR
389 # pipe is full, master should wake up anyways
394 # reaps all unreaped workers
398 wpid, status = Process.waitpid2(-1, Process::WNOHANG)
400 if reexec_pid == wpid
401 logger.error "reaped #{status.inspect} exec()-ed"
403 self.pid = pid.chomp('.oldbin') if pid
406 worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
407 logger.info "reaped #{status.inspect} " \
408 "worker=#{worker.nr rescue 'unknown'}"
415 # reexecutes the START_CTX with a new binary
419 Process.kill(0, reexec_pid)
420 logger.error "reexec-ed child already running PID:#{reexec_pid}"
428 old_pid = "#{pid}.oldbin"
431 self.pid = old_pid # clear the path for a new pid file
433 logger.error "old PID:#{valid_pid?(old_pid)} running with " \
434 "existing pid=#{old_pid}, refusing rexec"
437 logger.error "error writing pid=#{old_pid} #{e.class} #{e.message}"
442 self.reexec_pid = fork do
443 listener_fds = LISTENERS.map { |sock| sock.fileno }
444 ENV['UNICORN_FD'] = listener_fds.join(',')
445 Dir.chdir(START_CTX[:cwd])
446 cmd = [ START_CTX[0] ].concat(START_CTX[:argv])
448 # avoid leaking FDs we don't know about, but let before_exec
449 # unset FD_CLOEXEC, if anything else in the app eventually
450 # relies on FD inheritence.
451 (3..1024).each do |io|
452 next if listener_fds.include?(io)
453 io = IO.for_fd(io) rescue nil
456 io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
458 logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
459 before_exec.call(self)
462 proc_name 'master (old)'
465 # forcibly terminate all workers that haven't checked in in timeout
466 # seconds. The timeout is implemented using an unlinked File
467 # shared between the parent process and each worker. The worker
468 # runs File#chmod to modify the ctime of the File. If the ctime
469 # is stale for >timeout seconds, then we'll kill the corresponding
471 def murder_lazy_workers
473 WORKERS.dup.each_pair do |wpid, worker|
477 logger.warn "worker=#{worker.nr} PID:#{wpid} stat error: #{e.inspect}"
478 kill_worker(:QUIT, wpid)
481 stat.mode == 0100000 and next
482 (diff = (Time.now - stat.ctime)) <= timeout and next
483 logger.error "worker=#{worker.nr} PID:#{wpid} timeout " \
484 "(#{diff}s > #{timeout}s), killing"
485 kill_worker(:KILL, wpid) # take no prisoners for timeout violations
489 def spawn_missing_workers
490 (0...worker_processes).each do |worker_nr|
491 WORKERS.values.include?(worker_nr) and next
493 Dir.chdir(START_CTX[:cwd])
494 rescue Errno::ENOENT => err
495 logger.fatal "#{err.inspect} (#{START_CTX[:cwd]})"
496 SIG_QUEUE << :QUIT # forcibly emulate SIGQUIT
499 worker = Worker.new(worker_nr, Unicorn::Util.tmpio)
500 before_fork.call(self, worker)
501 WORKERS[fork { worker_loop(worker) }] = worker
505 def maintain_worker_count
506 (off = WORKERS.size - worker_processes) == 0 and return
507 off < 0 and return spawn_missing_workers
508 WORKERS.dup.each_pair { |wpid,w|
509 w.nr >= worker_processes and kill_worker(:QUIT, wpid) rescue nil
513 # once a client is accepted, it is processed in its entirety here
514 # in 3 easy steps: read request, call app, write app response
515 def process_client(client)
516 client.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
517 response = app.call(env = REQUEST.read(client))
519 if 100 == response.first.to_i
520 client.write(Const::EXPECT_100_RESPONSE)
521 env.delete(Const::HTTP_EXPECT)
522 response = app.call(env)
524 HttpResponse.write(client, response, HttpRequest::PARSER.headers?)
525 # if we get any error, try to write something back to the client
526 # assuming we haven't closed the socket, but don't get hung up
527 # if the socket is already closed or broken. We'll always ensure
528 # the socket is closed at the end of this function
529 rescue EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
530 client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
531 client.close rescue nil
532 rescue HttpParserError # try to tell the client they're bad
533 client.write_nonblock(Const::ERROR_400_RESPONSE) rescue nil
534 client.close rescue nil
536 client.write_nonblock(Const::ERROR_500_RESPONSE) rescue nil
537 client.close rescue nil
538 logger.error "Read error: #{e.inspect}"
539 logger.error e.backtrace.join("\n")
542 # gets rid of stuff the worker has no business keeping track of
543 # to free some resources and drops all sig handlers.
544 # traps for USR1, USR2, and HUP may be set in the after_fork Proc
546 def init_worker_process(worker)
547 QUEUE_SIGS.each { |sig| trap(sig, nil) }
548 trap(:CHLD, 'DEFAULT')
550 proc_name "worker[#{worker.nr}]"
553 WORKERS.values.each { |other| other.tmp.close rescue nil }
555 LISTENERS.each { |sock| sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
556 worker.tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
557 after_fork.call(self, worker) # can drop perms
558 self.timeout /= 2.0 # halve it for select()
559 build_app! unless preload_app
562 def reopen_worker_logs(worker_nr)
563 logger.info "worker=#{worker_nr} reopening logs..."
564 Unicorn::Util.reopen_logs
565 logger.info "worker=#{worker_nr} done reopening logs"
569 # runs inside each forked worker, this sits around and waits
570 # for connections and doesn't die until the parent dies (or is
571 # given a INT, QUIT, or TERM signal)
572 def worker_loop(worker)
574 init_worker_process(worker)
575 nr = 0 # this becomes negative if we need to reopen logs
576 alive = worker.tmp # tmp is our lifeline to the master process
580 # closing anything we IO.select on will raise EBADF
581 trap(:USR1) { nr = -65536; SELF_PIPE.first.close rescue nil }
582 trap(:QUIT) { alive = nil; LISTENERS.each { |s| s.close rescue nil } }
583 [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
584 logger.info "worker=#{worker.nr} ready"
587 nr < 0 and reopen_worker_logs(worker.nr)
590 # we're a goner in timeout seconds anyways if alive.chmod
591 # breaks, so don't trap the exception. Using fchmod() since
592 # futimes() is not available in base Ruby and I very strongly
593 # prefer temporary files to be unlinked for security,
594 # performance and reliability reasons, so utime is out. No-op
595 # changes with chmod doesn't update ctime on all filesystems; so
596 # we change our counter each and every time (after process_client
597 # and before IO.select).
598 t == (ti = Time.now.to_i) or alive.chmod(t = ti)
602 process_client(sock.accept_nonblock)
604 t == (ti = Time.now.to_i) or alive.chmod(t = ti)
605 rescue Errno::EAGAIN, Errno::ECONNABORTED
610 # make the following bet: if we accepted clients this round,
611 # we're probably reasonably busy, so avoid calling select()
612 # and do a speculative accept_nonblock on ready listeners
613 # before we sleep again in select().
614 redo unless nr == 0 # (nr < 0) => reopen logs
616 ppid == Process.ppid or return
619 # timeout used so we can detect parent death:
620 ret = IO.select(LISTENERS, nil, SELF_PIPE, timeout) or redo
629 logger.error "Unhandled listen loop exception #{e.inspect}."
630 logger.error e.backtrace.join("\n")
635 # delivers a signal to a worker and fails gracefully if the worker
636 # is no longer running.
637 def kill_worker(signal, wpid)
639 Process.kill(signal, wpid)
641 worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
645 # delivers a signal to each worker
646 def kill_each_worker(signal)
647 WORKERS.keys.each { |wpid| kill_worker(signal, wpid) }
650 # unlinks a PID file at given +path+ if it contains the current PID
651 # still potentially racy without locking the directory (which is
652 # non-portable and may interact badly with other programs), but the
653 # window for hitting the race condition is small
654 def unlink_pid_safe(path)
655 (File.read(path).to_i == $$ and File.unlink(path)) rescue nil
658 # returns a PID if a given path contains a non-stale PID file,
661 wpid = File.read(path).to_i
662 wpid <= 0 and return nil
664 Process.kill(0, wpid)
667 # don't unlink stale pid files, racy without non-portable locking...
674 logger.info "reloading config_file=#{config.config_file}"
675 config[:listeners].replace(init_listeners)
678 kill_each_worker(:QUIT)
679 Unicorn::Util.reopen_logs
681 build_app! if preload_app
682 logger.info "done reloading config_file=#{config.config_file}"
684 logger.error "error reloading config_file=#{config.config_file}: " \
685 "#{e.class} #{e.message}"
689 # returns an array of string names for the given listener array
690 def listener_names(listeners = LISTENERS)
691 listeners.map { |io| sock_name(io) }
695 if app.respond_to?(:arity) && app.arity == 0
696 if defined?(Gem) && Gem.respond_to?(:refresh)
697 logger.info "Refreshing Gem list"
705 $0 = ([ File.basename(START_CTX[0]), tag
706 ]).concat(START_CTX[:argv]).join(' ')
709 def redirect_io(io, path)
710 File.open(path, 'ab') { |fp| io.reopen(fp) } if path
715 SELF_PIPE.each { |io| io.close rescue nil }
716 SELF_PIPE.replace(IO.pipe)
717 SELF_PIPE.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }