1 # -*- encoding: binary -*-
6 require 'unicorn/socket_helper'
7 require 'unicorn/const'
8 require 'unicorn/http_request'
9 require 'unicorn/configurator'
10 require 'unicorn/util'
11 require 'unicorn/tee_input'
12 require 'unicorn/http_response'
14 # Unicorn module containing all of the classes (include C extensions) for running
15 # a Unicorn web server. It contains a minimalist HTTP server with just enough
16 # functionality to service web application requests fast as possible.
19 # raised inside TeeInput when a client closes the socket inside the
20 # application dispatch. This is always raised with an empty backtrace
21 # since there is nothing in the application stack that is responsible
22 # for client shutdowns/disconnects.
23 class ClientShutdown < EOFError
27 def run(app, options = {})
28 HttpServer.new(app, options).start.join
31 # This returns a lambda to pass in as the app, this does not "build" the
32 # app (which we defer based on the outcome of "preload_app" in the
33 # Unicorn config). The returned lambda will be called when it is
34 # time to build the app.
36 # allow Configurator to parse cli switches embedded in the ru file
37 Unicorn::Configurator::RACKUP.update(:file => ru, :optparse => opts)
39 # always called after config file parsing, may be called after forking
44 raw.sub!(/^__END__\n.*/, '')
45 eval("Rack::Builder.new {(#{raw}\n)}.to_app", TOPLEVEL_BINDING, ru)
48 Object.const_get(File.basename(ru, '.rb').capitalize)
51 pp({ :inner_app => inner_app }) if $DEBUG
53 # return value, matches rackup defaults based on env
57 use Rack::CommonLogger, $stderr
58 use Rack::ShowExceptions
64 use Rack::CommonLogger, $stderr
73 # returns an array of strings representing TCP listen socket addresses
74 # and Unix domain socket paths. This is useful for use with
75 # Raindrops::Middleware under Linux: http://raindrops.bogomips.org/
77 HttpServer::LISTENERS.map { |io| SocketHelper.sock_name(io) }
81 # This is the process manager of Unicorn. This manages worker
82 # processes which in turn handle the I/O and application process.
83 # Listener sockets are started in the master process and shared with
84 # forked worker children.
86 class HttpServer < Struct.new(:app, :timeout, :worker_processes,
87 :before_fork, :after_fork, :before_exec,
88 :logger, :pid, :listener_opts, :preload_app,
89 :reexec_pid, :orig_app, :init_listeners,
90 :master_pid, :config, :ready_pipe, :user)
91 include ::Unicorn::SocketHelper
93 # prevents IO objects in here from being GC-ed
96 # all bound listener sockets
99 # This hash maps PIDs to Workers
102 # We use SELF_PIPE differently in the master and worker processes:
104 # * The master process never closes or reinitializes this once
105 # initialized. Signal handlers in the master process will write to
106 # it to wake up the master from IO.select in exactly the same manner
107 # djb describes in http://cr.yp.to/docs/selfpipe.html
109 # * The workers immediately close the pipe they inherit from the
110 # master and replace it with a new pipe after forking. This new
111 # pipe is also used to wakeup from IO.select from inside (worker)
112 # signal handlers. However, workers *close* the pipe descriptors in
113 # the signal handlers to raise EBADF in IO.select instead of writing
114 # like we do in the master. We cannot easily use the reader set for
115 # IO.select because LISTENERS is already that set, and it's extra
116 # work (and cycles) to distinguish the pipe FD from the reader set
117 # once IO.select returns. So we're lazy and just close the pipe when
118 # a (rare) signal arrives in the worker and reinitialize the pipe later.
121 # signal queue used for self-piping
124 # constant lookups are faster and we're single-threaded/non-reentrant
125 REQUEST = HttpRequest.new
127 # We populate this at startup so we can figure out how to reexecute
128 # and upgrade the currently running instance of Unicorn
129 # This Hash is considered a stable interface and changing its contents
130 # will allow you to switch between different installations of Unicorn
131 # or even different installations of the same applications without
132 # downtime. Keys of this constant Hash are described as follows:
134 # * 0 - the path to the unicorn/unicorn_rails executable
135 # * :argv - a deep copy of the ARGV array the executable originally saw
136 # * :cwd - the working directory of the application, this is where
137 # you originally started Unicorn.
139 # To change your unicorn executable to a different path without downtime,
140 # you can set the following in your Unicorn config file, HUP and then
141 # continue with the traditional USR2 + QUIT upgrade steps:
143 # Unicorn::HttpServer::START_CTX[0] = "/home/bofh/1.9.2/bin/unicorn"
145 :argv => ARGV.map { |arg| arg.dup },
147 # favor ENV['PWD'] since it is (usually) symlink aware for
148 # Capistrano and like systems
150 a = File.stat(pwd = ENV['PWD'])
151 b = File.stat(Dir.pwd)
152 a.ino == b.ino && a.dev == b.dev ? pwd : Dir.pwd
160 # This class and its members can be considered a stable interface
161 # and will not change in a backwards-incompatible fashion between
162 # releases of Unicorn. You may need to access it in the
163 # before_fork/after_fork hooks. See the Unicorn::Configurator RDoc
165 class Worker < Struct.new(:nr, :tmp, :switched)
167 # worker objects may be compared to just plain numbers
172 # Changes the worker process to the specified +user+ and +group+
173 # This is only intended to be called from within the worker
174 # process from the +after_fork+ hook. This should be called in
175 # the +after_fork+ hook after any priviledged functions need to be
176 # run (e.g. to set per-worker CPU affinity, niceness, etc)
178 # Any and all errors raised within this method will be propagated
179 # directly back to the caller (usually the +after_fork+ hook.
180 # These errors commonly include ArgumentError for specifying an
181 # invalid user/group and Errno::EPERM for insufficient priviledges
182 def user(user, group = nil)
183 # we do not protect the caller, checking Process.euid == 0 is
184 # insufficient because modern systems have fine-grained
185 # capabilities. Let the caller handle any and all errors.
186 uid = Etc.getpwnam(user).uid
187 gid = Etc.getgrnam(group).gid if group
188 Unicorn::Util.chown_logs(uid, gid)
190 if gid && Process.egid != gid
191 Process.initgroups(user, gid)
192 Process::GID.change_privilege(gid)
194 Process.euid != uid and Process::UID.change_privilege(uid)
200 # Creates a working server on host:port (strange things happen if
201 # port isn't a Number). Use HttpServer::run to start the server and
202 # HttpServer.run.join to join the thread that's processing
203 # incoming requests on the socket.
204 def initialize(app, options = {})
207 self.ready_pipe = options.delete(:ready_pipe)
208 self.init_listeners = options[:listeners] ? options[:listeners].dup : []
209 self.config = Configurator.new(options.merge(:use_defaults => true))
210 self.listener_opts = {}
212 # we try inheriting listeners first, so we bind them later.
213 # we don't write the pid file until we've bound listeners in case
214 # unicorn was started twice by mistake. Even though our #pid= method
215 # checks for stale/existing pid files, race conditions are still
216 # possible (and difficult/non-portable to avoid) and can be likely
217 # to clobber the pid if the second start was in quick succession
218 # after the first, so we rely on the listener binding to fail in
219 # that case. Some tests (in and outside of this source tree) and
220 # monitoring tools may also rely on pid files existing before we
221 # attempt to connect to the listener(s)
222 config.commit!(self, :skip => [:listeners, :pid])
226 # Runs the thing. Returns self so you can run join on it
228 BasicSocket.do_not_reverse_lookup = true
230 # inherit sockets from parents, they need to be plain Socket objects
231 # before they become UNIXServer or TCPServer
232 inherited = ENV['UNICORN_FD'].to_s.split(/,/).map do |fd|
233 io = Socket.for_fd(fd.to_i)
234 set_server_sockopt(io, listener_opts[sock_name(io)])
236 logger.info "inherited addr=#{sock_name(io)} fd=#{fd}"
240 config_listeners = config[:listeners].dup
241 LISTENERS.replace(inherited)
243 # we start out with generic Socket objects that get cast to either
244 # TCPServer or UNIXServer objects; but since the Socket objects
245 # share the same OS-level file descriptor as the higher-level *Server
246 # objects; we need to prevent Socket objects from being garbage-collected
247 config_listeners -= listener_names
248 if config_listeners.empty? && LISTENERS.empty?
249 config_listeners << Unicorn::Const::DEFAULT_LISTEN
250 init_listeners << Unicorn::Const::DEFAULT_LISTEN
251 START_CTX[:argv] << "-l#{Unicorn::Const::DEFAULT_LISTEN}"
253 config_listeners.each { |addr| listen(addr) }
254 raise ArgumentError, "no listeners" if LISTENERS.empty?
256 # this pipe is used to wake us up from select(2) in #join when signals
257 # are trapped. See trap_deferred.
260 # setup signal handlers before writing pid file in case people get
261 # trigger happy and send signals as soon as the pid file exists.
262 # Note that signals don't actually get handled until the #join method
263 QUEUE_SIGS.each { |sig| trap_deferred(sig) }
264 trap(:CHLD) { |_| awaken_master }
265 self.pid = config[:pid]
268 build_app! if preload_app
269 maintain_worker_count
273 # replaces current listener set with +listeners+. This will
274 # close the socket if it will not exist in the new listener set
275 def listeners=(listeners)
276 cur_names, dead_names = [], []
277 listener_names.each do |name|
279 # mark unlinked sockets as dead so we can rebind them
280 (File.socket?(name) ? cur_names : dead_names) << name
285 set_names = listener_names(listeners)
286 dead_names.concat(cur_names - set_names).uniq!
288 LISTENERS.delete_if do |io|
289 if dead_names.include?(sock_name(io))
290 IO_PURGATORY.delete_if do |pio|
291 pio.fileno == io.fileno && (pio.close rescue nil).nil? # true
293 (io.close rescue nil).nil? # true
295 set_server_sockopt(io, listener_opts[sock_name(io)])
300 (set_names - cur_names).each { |addr| listen(addr) }
303 def stdout_path=(path); redirect_io($stdout, path); end
304 def stderr_path=(path); redirect_io($stderr, path); end
307 HttpRequest::DEFAULTS["rack.logger"] = super
310 # sets the path for the PID file of the master process
313 if x = valid_pid?(path)
314 return path if pid && path == pid && x == $$
315 raise ArgumentError, "Already running on PID:#{x} " \
316 "(or pid=#{path} is stale)"
319 unlink_pid_safe(pid) if pid
323 tmp = "#{File.dirname(path)}/#{rand}.#$$"
324 File.open(tmp, File::RDWR|File::CREAT|File::EXCL, 0644)
329 File.rename(fp.path, path)
335 # add a given address to the +listeners+ set, idempotently
336 # Allows workers to add a private, per-process listener via the
337 # after_fork hook. Very useful for debugging and testing.
338 # +:tries+ may be specified as an option for the number of times
339 # to retry, and +:delay+ may be specified as the time in seconds
340 # to delay between retries.
341 # A negative value for +:tries+ indicates the listen will be
342 # retried indefinitely, this is useful when workers belonging to
343 # different masters are spawned during a transparent upgrade.
344 def listen(address, opt = {}.merge(listener_opts[address] || {}))
345 address = config.expand_addr(address)
346 return if String === address && listener_names.include?(address)
348 delay = opt[:delay] || 0.5
349 tries = opt[:tries] || 5
351 io = bind_listen(address, opt)
352 unless TCPServer === io || UNIXServer === io
356 logger.info "listening on addr=#{sock_name(io)} fd=#{io.fileno}"
359 rescue Errno::EADDRINUSE => err
360 logger.error "adding listener failed addr=#{address} (in use)"
361 raise err if tries == 0
363 logger.error "retrying in #{delay} seconds " \
364 "(#{tries < 0 ? 'infinite' : tries} tries left)"
368 logger.fatal "error adding listener addr=#{address}"
373 # monitors children and receives signals forever
374 # (or until a termination signal is sent). This handles signals
375 # one-at-a-time time and we'll happily drop signals in case somebody
376 # is signalling us too often.
379 last_check = Time.now
382 logger.info "master process ready" # test_exec.rb relies on this message
384 ready_pipe.syswrite($$.to_s)
385 ready_pipe.close rescue nil
386 self.ready_pipe = nil
393 # avoid murdering workers after our master process (or the
394 # machine) comes out of suspend/hibernation
395 if (last_check + timeout) >= (last_check = Time.now)
398 # wait for workers to wakeup on suspend
399 master_sleep(timeout/2.0 + 1)
401 maintain_worker_count if respawn
403 when :QUIT # graceful shutdown
405 when :TERM, :INT # immediate shutdown
408 when :USR1 # rotate logs
409 logger.info "master reopening logs..."
410 Unicorn::Util.reopen_logs
411 logger.info "master done reopening logs"
412 kill_each_worker(:USR1)
413 when :USR2 # exec binary, stay alive in case something went wrong
416 if Process.ppid == 1 || Process.getpgrp != $$
418 logger.info "gracefully stopping all workers"
419 kill_each_worker(:QUIT)
421 logger.info "SIGWINCH ignored because we're not daemonized"
424 self.worker_processes += 1
426 self.worker_processes -= 1 if self.worker_processes > 0
429 if config.config_file
431 redo # immediate reaping since we may have QUIT workers
432 else # exec binary and exit if there's no config file
433 logger.info "config_file not present, reexecuting binary"
442 logger.error "Unhandled master loop exception #{e.inspect}."
443 logger.error e.backtrace.join("\n")
446 stop # gracefully shutdown all workers on our way out
447 logger.info "master complete"
448 unlink_pid_safe(pid) if pid
451 # Terminates all workers, but does not exit master process
452 def stop(graceful = true)
454 limit = Time.now + timeout
455 until WORKERS.empty? || Time.now > limit
456 kill_each_worker(graceful ? :QUIT : :TERM)
460 kill_each_worker(:KILL)
465 # list of signals we care about and trap in master.
466 QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :USR2, :HUP,
469 # defer a signal for later processing in #join (master process)
470 def trap_deferred(signal)
471 trap(signal) do |sig_nr|
472 if SIG_QUEUE.size < 5
476 logger.error "ignoring SIG#{signal}, queue=#{SIG_QUEUE.inspect}"
481 # wait for a signal hander to wake us up and then consume the pipe
482 # Wake up every second anyways to run murder_lazy_workers
483 def master_sleep(sec)
485 IO.select([ SELF_PIPE.first ], nil, nil, sec) or return
486 SELF_PIPE.first.read_nonblock(Const::CHUNK_SIZE, HttpRequest::BUF)
487 rescue Errno::EAGAIN, Errno::EINTR
494 SELF_PIPE.last.write_nonblock('.') # wakeup master process from select
495 rescue Errno::EAGAIN, Errno::EINTR
496 # pipe is full, master should wake up anyways
501 # reaps all unreaped workers
505 wpid, status = Process.waitpid2(-1, Process::WNOHANG)
507 if reexec_pid == wpid
508 logger.error "reaped #{status.inspect} exec()-ed"
510 self.pid = pid.chomp('.oldbin') if pid
513 worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
514 logger.info "reaped #{status.inspect} " \
515 "worker=#{worker.nr rescue 'unknown'}"
522 # reexecutes the START_CTX with a new binary
526 Process.kill(0, reexec_pid)
527 logger.error "reexec-ed child already running PID:#{reexec_pid}"
535 old_pid = "#{pid}.oldbin"
538 self.pid = old_pid # clear the path for a new pid file
540 logger.error "old PID:#{valid_pid?(old_pid)} running with " \
541 "existing pid=#{old_pid}, refusing rexec"
544 logger.error "error writing pid=#{old_pid} #{e.class} #{e.message}"
549 self.reexec_pid = fork do
550 listener_fds = LISTENERS.map { |sock| sock.fileno }
551 ENV['UNICORN_FD'] = listener_fds.join(',')
552 Dir.chdir(START_CTX[:cwd])
553 cmd = [ START_CTX[0] ].concat(START_CTX[:argv])
555 # avoid leaking FDs we don't know about, but let before_exec
556 # unset FD_CLOEXEC, if anything else in the app eventually
557 # relies on FD inheritence.
558 (3..1024).each do |io|
559 next if listener_fds.include?(io)
560 io = IO.for_fd(io) rescue nil
563 io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
565 logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
566 before_exec.call(self)
569 proc_name 'master (old)'
572 # forcibly terminate all workers that haven't checked in in timeout
573 # seconds. The timeout is implemented using an unlinked File
574 # shared between the parent process and each worker. The worker
575 # runs File#chmod to modify the ctime of the File. If the ctime
576 # is stale for >timeout seconds, then we'll kill the corresponding
578 def murder_lazy_workers
579 WORKERS.dup.each_pair do |wpid, worker|
580 stat = worker.tmp.stat
581 # skip workers that disable fchmod or have never fchmod-ed
582 stat.mode == 0100600 and next
583 (diff = (Time.now - stat.ctime)) <= timeout and next
584 logger.error "worker=#{worker.nr} PID:#{wpid} timeout " \
585 "(#{diff}s > #{timeout}s), killing"
586 kill_worker(:KILL, wpid) # take no prisoners for timeout violations
590 def spawn_missing_workers
591 (0...worker_processes).each do |worker_nr|
592 WORKERS.values.include?(worker_nr) and next
593 worker = Worker.new(worker_nr, Unicorn::Util.tmpio)
594 before_fork.call(self, worker)
596 ready_pipe.close if ready_pipe
597 self.ready_pipe = nil
603 def maintain_worker_count
604 (off = WORKERS.size - worker_processes) == 0 and return
605 off < 0 and return spawn_missing_workers
606 WORKERS.dup.each_pair { |wpid,w|
607 w.nr >= worker_processes and kill_worker(:QUIT, wpid) rescue nil
611 # if we get any error, try to write something back to the client
612 # assuming we haven't closed the socket, but don't get hung up
613 # if the socket is already closed or broken. We'll always ensure
614 # the socket is closed at the end of this function
615 def handle_error(client, e)
617 when EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
618 Const::ERROR_500_RESPONSE
619 when HttpParserError # try to tell the client they're bad
620 Const::ERROR_400_RESPONSE
622 logger.error "Read error: #{e.inspect}"
623 logger.error e.backtrace.join("\n")
624 Const::ERROR_500_RESPONSE
626 client.write_nonblock(msg)
632 # once a client is accepted, it is processed in its entirety here
633 # in 3 easy steps: read request, call app, write app response
634 def process_client(client)
635 client.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
636 response = app.call(env = REQUEST.read(client))
638 if 100 == response.first.to_i
639 client.write(Const::EXPECT_100_RESPONSE)
640 env.delete(Const::HTTP_EXPECT)
641 response = app.call(env)
643 HttpResponse.write(client, response, HttpRequest::PARSER.headers?)
645 handle_error(client, e)
648 # gets rid of stuff the worker has no business keeping track of
649 # to free some resources and drops all sig handlers.
650 # traps for USR1, USR2, and HUP may be set in the after_fork Proc
652 def init_worker_process(worker)
653 QUEUE_SIGS.each { |sig| trap(sig, nil) }
654 trap(:CHLD, 'DEFAULT')
656 proc_name "worker[#{worker.nr}]"
659 WORKERS.values.each { |other| other.tmp.close rescue nil }
661 LISTENERS.each { |sock| sock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }
662 worker.tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
663 after_fork.call(self, worker) # can drop perms
664 worker.user(*user) if user.kind_of?(Array) && ! worker.switched
665 self.timeout /= 2.0 # halve it for select()
666 build_app! unless preload_app
669 def reopen_worker_logs(worker_nr)
670 logger.info "worker=#{worker_nr} reopening logs..."
671 Unicorn::Util.reopen_logs
672 logger.info "worker=#{worker_nr} done reopening logs"
676 # runs inside each forked worker, this sits around and waits
677 # for connections and doesn't die until the parent dies (or is
678 # given a INT, QUIT, or TERM signal)
679 def worker_loop(worker)
681 init_worker_process(worker)
682 nr = 0 # this becomes negative if we need to reopen logs
683 alive = worker.tmp # tmp is our lifeline to the master process
686 # closing anything we IO.select on will raise EBADF
687 trap(:USR1) { nr = -65536; SELF_PIPE.first.close rescue nil }
688 trap(:QUIT) { alive = nil; LISTENERS.each { |s| s.close rescue nil } }
689 [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
690 logger.info "worker=#{worker.nr} ready"
694 nr < 0 and reopen_worker_logs(worker.nr)
697 # we're a goner in timeout seconds anyways if alive.chmod
698 # breaks, so don't trap the exception. Using fchmod() since
699 # futimes() is not available in base Ruby and I very strongly
700 # prefer temporary files to be unlinked for security,
701 # performance and reliability reasons, so utime is out. No-op
702 # changes with chmod doesn't update ctime on all filesystems; so
703 # we change our counter each and every time (after process_client
704 # and before IO.select).
705 alive.chmod(m = 0 == m ? 1 : 0)
709 process_client(sock.accept_nonblock)
711 alive.chmod(m = 0 == m ? 1 : 0)
712 rescue Errno::EAGAIN, Errno::ECONNABORTED
717 # make the following bet: if we accepted clients this round,
718 # we're probably reasonably busy, so avoid calling select()
719 # and do a speculative accept_nonblock on ready listeners
720 # before we sleep again in select().
721 redo unless nr == 0 # (nr < 0) => reopen logs
723 ppid == Process.ppid or return
724 alive.chmod(m = 0 == m ? 1 : 0)
726 # timeout used so we can detect parent death:
727 ret = IO.select(LISTENERS, nil, SELF_PIPE, timeout) or redo
736 logger.error "Unhandled listen loop exception #{e.inspect}."
737 logger.error e.backtrace.join("\n")
742 # delivers a signal to a worker and fails gracefully if the worker
743 # is no longer running.
744 def kill_worker(signal, wpid)
746 Process.kill(signal, wpid)
748 worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
752 # delivers a signal to each worker
753 def kill_each_worker(signal)
754 WORKERS.keys.each { |wpid| kill_worker(signal, wpid) }
757 # unlinks a PID file at given +path+ if it contains the current PID
758 # still potentially racy without locking the directory (which is
759 # non-portable and may interact badly with other programs), but the
760 # window for hitting the race condition is small
761 def unlink_pid_safe(path)
762 (File.read(path).to_i == $$ and File.unlink(path)) rescue nil
765 # returns a PID if a given path contains a non-stale PID file,
768 wpid = File.read(path).to_i
769 wpid <= 0 and return nil
771 Process.kill(0, wpid)
774 # don't unlink stale pid files, racy without non-portable locking...
782 logger.info "reloading config_file=#{config.config_file}"
783 config[:listeners].replace(init_listeners)
786 kill_each_worker(:QUIT)
787 Unicorn::Util.reopen_logs
789 build_app! if preload_app
790 logger.info "done reloading config_file=#{config.config_file}"
791 rescue StandardError, LoadError, SyntaxError => e
792 logger.error "error reloading config_file=#{config.config_file}: " \
793 "#{e.class} #{e.message} #{e.backtrace}"
794 self.app = loaded_app
798 # returns an array of string names for the given listener array
799 def listener_names(listeners = LISTENERS)
800 listeners.map { |io| sock_name(io) }
804 if app.respond_to?(:arity) && app.arity == 0
805 if defined?(Gem) && Gem.respond_to?(:refresh)
806 logger.info "Refreshing Gem list"
814 $0 = ([ File.basename(START_CTX[0]), tag
815 ]).concat(START_CTX[:argv]).join(' ')
818 def redirect_io(io, path)
819 File.open(path, 'ab') { |fp| io.reopen(fp) } if path
824 SELF_PIPE.each { |io| io.close rescue nil }
825 SELF_PIPE.replace(IO.pipe)
826 SELF_PIPE.each { |io| io.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) }