unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: unicorn list <mongrel-unicorn@rubyforge.org>
Cc: Cedric Maion <cedric@maion.com>
Subject: Re: [PATCH] `kill -SIGTRAP <worker pid>`
Date: Sat, 23 Jun 2012 18:55:34 +0000	[thread overview]
Message-ID: <20120623185534.GA17517@dcvr.yhbt.net> (raw)
In-Reply-To: <1340467944-23621-2-git-send-email-cedric@maion.com>

Subject: [PATCH] `kill -SIGTRAP <worker pid>` to get a live ruby
 backtrace + generate backtrace when murdered worker due to timeout

Please keep Subject lines a reasonable length (git recommends the commit
message subject wrap at ~50 columns or so) and wrap code at <= 80
columns

Cedric Maion <cedric@maion.com> wrote:
> ---
>  lib/unicorn/http_server.rb |    3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
> index 14a6f9a..8507fe4 100644
> --- a/lib/unicorn/http_server.rb
> +++ b/lib/unicorn/http_server.rb
> @@ -457,6 +457,8 @@ class Unicorn::HttpServer
>        next_sleep = 0
>        logger.error "worker=#{worker.nr} PID:#{wpid} timeout " \
>                     "(#{diff}s > #{@timeout}s), killing"
> +      kill_worker(:TRAP, wpid)
> +      sleep(0.5)
>        kill_worker(:KILL, wpid) # take no prisoners for timeout violations

SIGKILL timeout is only a last line of defense when the Ruby VM itself
is completely broken.  Handling SIGTRAP implies the worker can still
respond (and /can/ be rescued), so your SIGTRAP handler is worthless if
SIGKILL is required to kill a process.

See http://unicorn.bogomips.org/Application_Timeouts.html

Sleeping here is also unacceptable since it blocks the main loop,
making masters signal handlers non-responsive for too long.

> @@ -594,6 +596,7 @@ class Unicorn::HttpServer
>      # closing anything we IO.select on will raise EBADF
>      trap(:USR1) { nr = -65536; SELF_PIPE[0].close rescue nil }
>      trap(:QUIT) { worker = nil; LISTENERS.each { |s| s.close rescue nil }.clear }
> +    trap(:TRAP) { logger.info("worker=#{worker.nr} pid:#{$$} received TRAP signal, showing backtrace:\n#{caller.join("\n")}") }
>      logger.info "worker=#{worker.nr} ready"

Using the Logger class inside a signal handler can deadlock.  Logger
attempts to acquire a non-reentrant lock when called.  Unicorn doesn't
use threads itself, but the Rack app may use threads internally.

Thanks for your interest in unicorn!
_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

  reply	other threads:[~2012-06-23 18:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-23 16:12 `kill -SIGTRAP <worker pid>` to get a live ruby backtrace + generate backtrace when murdering worker due to timeout Cedric Maion
2012-06-23 16:12 ` [PATCH] `kill -SIGTRAP <worker pid>` to get a live ruby backtrace + generate backtrace when murdered " Cedric Maion
2012-06-23 18:55   ` Eric Wong [this message]
2012-06-24 11:05     ` [PATCH] `kill -SIGTRAP <worker pid>` Cedric Maion
2012-06-25  3:59       ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20120623185534.GA17517@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=cedric@maion.com \
    --cc=mongrel-unicorn@rubyforge.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).