about summary refs log tree commit homepage
path: root/lib/unicorn.rb
DateCommit message (Collapse)
2015-04-24support TempfileReaper in deployment and development envs 4.x-stable
rack 1.6 added a TempfileReaper middleware to cleanup temporary files. Enable it by default for users running rack 1.6 or later to avoid leaving temporary files around.
2013-12-09rework master-to-worker signaling to use a pipe
Signaling using normal kill(2) is preserved, but the master now prefers to signal workers using a pipe rather than kill(2). Non-graceful signals (:TERM/:KILL) are still sent using kill(2), as they ask for immediate shutdown. This change is necessary to avoid triggering the ubf (unblocking function) for rb_thread_call_without_gvl (and similar) functions extensions. Most notably, this fixes compatibility with newer versions of the 'pg' gem which will cancel a running DB query if signaled[1]. This also has the nice side-effect of allowing a premature master death (assuming preload_app didn't cause the master to spawn off rogue child daemons). Note: users should also refrain from using "killall" if using the 'pg' gem or something like it. Unfortunately, this increases FD usage in the master as the writable end of the pipe is preserved in the master. This limit the number of worker processes the master may run to the open file limit of the master process. Increasing the open file limit of the master process may be needed. However, the FD use on the workers is reduced by one as the internal self-pipe is no longer used. Thus, overall pipe allocation for the kernel remains unchanged. [1] - pg is correct to cancel a query, as it cannot know if the signal was for a) graceful unicorn shutdown or b) oh-noes-I-started-a-bad-query-ABORT-ABORT-ABORT!!
2013-06-07Make -N/--no-default-middleware option work
This fixes the -N (a.k.a. --no-defaut-middleware) option, which was not working. The problem was that Unicorn::Configurator::RACKUP is cleared before the lambda returned by Unicorn.builder is run, which means that checking whether the :no_default_middleware option was set from the lambda could not detect anything. This patch copies it to a local variable that won't get clobbered, restoring the feature. [ew: squashed test commit into the fix, whitespace fixes] Signed-off-by: Eric Wong <normalperson@yhbt.net>
2013-01-29Add -N or --no-default-middleware option.
This would prevent Unicorn from adding default middleware, as if RACK_ENV were always none. (not development nor deployment) This should also be applied to `rainbows' and `zbatery' as well. One of the reasons to add this is to avoid conflicting RAILS_ENV and RACK_ENV. It would be helpful in the case where a Rails application and Rack application are composed together, while we want Rails app runs under development and Rack app runs under none (if we don't want those default middleware), and we don't really want to make RAILS_ENV set to development and RACK_ENV to none because it might be confusing. Note that Rails would also look into RACK_ENV. Another reason for this is that only `rackup' would be inserting those default middleware. Both `thin' and `puma' would not do this, nor does Rack::Handler.get.run which is used in Sinatra. So using this option would make it work differently from `rackup' but somehow more similar to `thin' or `puma'. Discussion thread on the mailing list: http://rubyforge.org/pipermail/mongrel-unicorn/2013-January/001675.html Signed-off-by: Eric Wong <normalperson@yhbt.net>
2012-08-02bind listeners after loading for preload_app users
In the case where preload_app is true, delay binding new listeners until after loading the application. Some applications have very long load times (especially Rails apps with Ruby 1.9.2). Binding listeners early may cause a load balancer to incorrectly believe the unicorn workers are ready to serve traffic even while the app is being loaded. Once a listener is bound, connect() requests from the load balancer succeed until the listen backlog is filled. This allows requests to pile up for a bit (depending on backlog size) before getting rejected by the kernel. By the time the application is loaded and ready-to-run, requests in the listen backlog are likely stale and not useful to process. Processes inheriting listeners do not suffer this effect, as the old process should still be capable of serving new requests. This change does not improve the situation for the preload_app=false (default) use case. There may not be a solution for preload_app=false users using large applications. Fortunately Ruby 1.9.3+ improves load times of large applications significantly over 1.9.2 so this should be less of a problem in the future. Reported via private email sent on 2012-06-29T22:59:10Z
2011-08-19Rack::Chunked and ContentLength middlewares by default
This is needed to match the behavior of Rack::Server for RACK_ENV=(deployment|development), actually. This won't affect users of other RACK_ENV values. This change has minor performance consequences, so users negatively affected should set RACK_ENV to "none" instead for full control of their middleware stack. This mainly affects Rainbows!/Zbatery users since they have persistent connections and /need/ Content-Length or Transfer-Encoding:chunked headers.
2011-08-19filter exception messages with control characters
We do not want to affect terminals of users who view our log files.
2011-06-24allow multiline comments in config.ru
This matches the latest Rack behavior. We can't just use Rack::Builder.parse_file because our option parser logic is slightly different and incompatible. ref: rack commit d31cf2b7c0c77c04510c08d95776315ceb24ba54
2011-06-17error logging is more consistent
Backtraces are now formatted properly (with timestamps) and exceptions will be logged more consistently and similar to Logger defaults: "#{exc.message} (#{e.class})" backtrace.each { |line| ... } This may break some existing monitoring scripts, but errors will be more standardized and easier to check moving forward.
2011-06-15memory reductions in worker process
There's absolutely no need to keep the OptionParser around in worker processes.
2011-06-11runtime stack size reductions
This reduces the size of `caller` by 5 frames, which should make backtraces easier-to-read, raising exceptions less expensive, and reduce GC runtime.
2011-04-18documentation cleanup/reduction
Don't clutter up our RDoc/website with things that users of Unicorn don't need to see. This should make user-relevant documentation easier to find, especially since Unicorn is NOT intended to be an API.
2010-11-11add stream_input class and build tee_input on it
We will eventually expose a Unicorn::StreamInput object as "rack.input" for Rack 2.x applications. StreamInput allows applications to avoid buffering input to disk, removing the (potentially expensive) rewindability requirement of Rack 1.x. TeeInput is also rewritten to build off StreamInput for simplicity. The only regression is that TeeInput#rewind forces us to consume an unconsumed stream before returning, a negligible price to pay for decreased complexity.
2010-10-07start using more compact parser API
This should be easier for Rainbows! to use
2010-10-06various cleanups and reduce indentation
This also affects some constant scoping rules, but hopefully makes things easier to follow. Accessing ivars (not via accessor methods) are also slightly faster, so use them in the criticial process_client code path.
2010-10-05update comment about non-blocking accept()
Thanks to kgio, we no longer use accept_nonblock.
2010-10-05Unicorn::Util.tmpio => Unicorn::TmpIO.new
This is slightly shorter and hopefully easier to find.
2010-10-05start using kgio, the kinder, gentler I/O library
This should hopefully make the non-blocking accept() situation more tolerable under Ruby 1.9.2.
2010-10-04http_request: avoid globals
Rainbows! will be able to reuse this.
2010-10-04split out worker to a separate file
This hopefully makes things easier to read, follow, and find since it's mostly documentation...
2010-10-04http_response: avoid singleton method
There's no need for a response class or object since Rack just uses an array as the response. So use a procedural style which allows for easier understanding. We shall also support keepalive/pipelining in the future, too.
2010-08-07miscellaneous loop and begin cleanups
These are minor changes to remove unnecessary loop nesting and begin usage to reduce our code size and hopefully simplify flow for readers.
2010-08-07log ERROR messages if workers exit with failure
Something is wrong if workers exit with a non-zero status, so we'll increase the log level to help prevent people from missing it.
2010-07-16SIGTTIN works after SIGWINCH
In addition to SIGHUP, it should be possible to gradually bring workers back up (to avoid overloading the machine) when rolling back upgrades after SIGWINCH. Noticed-by: Lawrence Pit ref: http://mid.gmane.org/4C3F8C9F.2090903@gmail.com
2010-07-13SIGHUP deals w/ dual master pid path scenario
As described in our SIGNALS documentation, sending SIGHUP to the old master (to respawn SIGWINCH-ed children) while the new master (spawned from SIGUSR2) is active is useful for backing out of an upgrade before sending SIGQUIT to the new master. Unfortunately, the SIGHUP signal to the old master will cause the ".oldbin" pid file to be reset to the non-".oldbin" version and thus attempt to clobber the pid file in use by the to-be-terminated new master process. Thanks to the previous commit to prevent redaemonization in the new master, the old master can reliably detect if the new master is active while it is reloading the config file. Thanks to Lawrence Pit for discovering this bug. ref: http://mid.gmane.org/4C3BEACF.7040301@gmail.com
2010-07-08cleanup "stringio" require
"stringio" is part of the Ruby distro and we use it in multiple places, so avoid re-requiring it.
2010-07-08prefer "[]" to "first"/"last" where possible
"[]" is slightly faster under Ruby 1.9 (but slightly slower under 1.8).
2010-06-17revert rbx-only workaround for IO#reopen
This is fixed upstream in Rubinius by commit b630ad9ddb4544a62e8e2282ba7dc59c4269bad7
2010-06-15workaround rbx not reopening logs to stderr/stdout
While log reopening worked reliably for newly-created File objects in the unit tests, the $stderr and $stdout handles that get redirected did not get reopened reliably under Rubinius. We work around this by relying on Rubinius internals and directly setting the @path instance variable. This is harmless for MRI and should be harmless for other any other Ruby implementations we'll eventually support. ref: http://github.com/evanphx/rubinius/issues/360
2010-06-10respect "working_directory" wrt config.ru
Since we added support for the "working_directory" parameter, it often became unclear where/when certain paths would be bound. There are some extremely nasty dependencies and ordering issues when doing this. It's all pretty fragile, but works for now and we even have a full integration test to keep it working. I plan on cleaning this up 2.x.x to be less offensive to look at (Rainbows! and Zbatery are a bit tied to this at the moment). Thanks to Pierre Baillet for reporting this. ref: http://mid.gmane.org/AANLkTimKb7JARr_69nfVrJLvMZH3Gvs1o_KwZFLKfuxy@mail.gmail.com
2010-06-08spew backtrace when config reloading fails
It can be useful to track down problems with
2010-06-08cleanup master_sleep loop
No need for extra checks when we're just doing read_nonblock. We'll also avoid reallocating a 16K buffer every time we sleep, we can just reuse the buffer the workers normally use to process request.
2010-05-06doc: kill Dir.chdir example
Modern version of Unicorn have working_directory available and should use that instead.
2010-05-06always load Rack up front at startup
It's too complicated and error-prone to allow apps to use a different version of Rack than the one Unicorn would otherwise use by default. If an app requires a different version of Rack than what Unicorn would load by default, it is recommended they only install that version of Rack (and no others) since Unicorn does not have any strict requirements on currently released Rack versions. If it is not possible to only have one Rack version installed globally, then they should either use Isolate or Bundler and install a private version of Unicorn along with their preferred version of Rack. Users who install in this way are recommended to execute the isolated/bundled version of Unicorn, instead of what would normally be in $PATH. Feedback/tips to mailto:mongrel-unicorn@rubyforge.org from Isolate and Bundler users would be greatly appreciated.
2010-04-30add global Unicorn.listener_names method
This is useful as a :listeners argument when setting up Raindrops::Middleware (http://raindrops.bogomips.org/), as it can be done automatically.
2010-04-19avoid nuking workers after waking up from suspend/hibernate
Trying to fix this issue again, as it seems to have been broken again.
2010-04-19prefer explicit requires when possible
There is no need to be using autoload except for HttpResponse which depends on Rack (which we want to load as late as possible).
2010-04-08unicorn: load constants after app has loaded
This will help ensure we use the same version of Rack the application uses and avoid loading conflicting/incompatible versions.
2010-03-01load the Etc module up front
It's part of the standard Ruby library and will always be loaded by various modules (Rack::Utils, Tmpdir) so there's no point in deferring it.
2010-03-01configurator "user" directive outside of after_fork
Allowing the "user" directive outside of after_fork reduces the cognitive overhead for folks that do not need the complexity of *_fork hooks. Using Worker#user remains supported as it offers fine-grained control of user switching.
2010-02-28respect user's encoding in config.ru in 1.9
Do not assume the user wants config.ru to be Encoding::BINARY for 1.9.
2010-02-28put switch parsing for config.ru into Unicorn.builder
This lets us reuse code for Zbatery and Rainbows!, too.
2010-02-18Unicorn::builder to wrap our Rack::Builder usage
This should make it easier to reuse code in derivative servers like Rainbows! and Zbatery. Unfortunately, we can't depend on Rack::Builder/Rack::Server yet since Rack 1.1 just got them and notable frameworks (like Rails 2.3.x) do not fully work with Rack 1.1 yet). This also fixes subtle issue with config.ru files that could have variables that conflict with the Unicorn-specific namespace (this bug still affects "unicorn_rails", which could use some reworking as well).
2010-02-18graceful handling of bad config.ru + HUP w/ preload_app
If preload_app is true and Unicorn is HUP-ed with a bad config.ru, then it would be possible to have Unicorn in a bad state and constantly throw 500 errors. We now detect syntax and load errors since they're likely to appear in modified Rackup files, and will restore the original app if reloading failed.
2010-02-09reorder commonly accessed HttpServer struct members
Earlier elements of a Struct (in both Ruby and C) are faster to access, so put more-often accessed elements like :app first. This does not noticeably affect most applications, but may matter to some micro benchmarks somewhere...
2010-01-19initialize signal handlers before writing pid file
This prevents trigger-happy init scripts from reading the pid file (and thus sending signals) to a not-fully initialized master process to handle them. This does NOT fix anything if other processes are sending signals prematurely without relying on the presence of the pid file. It's not possible to prevent all cases of this in one process, even in a purely C application, so we won't bother trying. We continue to always defer signal handling to the main loop anyways, and signals sent to the master process will be deferred/ignored until Unicorn::HttpServer#join is run.
2010-01-07Merge branch 'rack-1.1'
* rack-1.1: http_response: disallow blank, multi-value headers local.mk.sample: use rack-1.1.0 bump "rack.version" env to [1,1] set env["rack.logger"] for applications
2009-12-28clarify errors when listeners fail to bind
When using multiple listeners, the log messages can be potentially misleading as to which listener fails to bind: Before: INFO -- : unlinking existing socket=/tmp/unicorn.sock INFO -- : listening on addr=/tmp/unicorn.sock fd=3 unicorn/socket_helper.rb:110:in `initialize': Permission denied - bind(2) (Errno::EACCES) After: INFO -- : unlinking existing socket=/tmp/openxdms.sock INFO -- : listening on addr=/tmp/openxdms.sock fd=3 FATAL -- : error adding listener addr=0.0.0.0:84 unicorn/socket_helper.rb:110:in `initialize': Permission denied - bind(2) (Errno::EACCES)
2009-12-27Avoid leaking ready pipe file descriptor to workers
Otherwise the original spawner process may not notice the close as it's still being shared by workers. While we're at it, avoid confusing the original spawner by using readpartial instead of sysread.
2009-12-26exit with failure if master dies when daemonized
This behavior change also means our grandparent (launched from a controlling terminal or script) will wait until the master process is ready before returning. Thanks to IƱaki Baz Castillo for the initial implementations and inspiration.