Thin doesn't respond to SIGINT or SIGTERM

2019-04-30 08:21发布

bundle exec thin start -p 3111 gives the following output:

Using rack adapter Thin web server (v1.2.11 codename Bat-Shit Crazy) Maximum connections set to 1024 Listening on 0.0.0.0:3111, CTRL+C to stop ^C

Ctrl-C doesn't do anything (SIGINT). Neither does kill (SIGTERM).

I've found a few references to this behavior, but no solutions. The problem seems to be either in eventmachine (bundled with latest thin), in ruby 1.9.2-r290, or in the linux kernel (Ubuntu 10.4 LTS, 2.6.38.3-linode32).

It happens with my project, but not with a brand new rails project.

References:

1条回答
劳资没心,怎么记你
2楼-- · 2019-04-30 08:31

My guess is that either something's tying up the EventMachine reactor loop preventing it from exiting, or something's trapping SIGINT.

As a simple example of the former, put this into config.ru and run with thin -p 4567 start:

require 'thin'
require 'sinatra'
require 'eventmachine'


get '/' do
  "hello world"
end

run Sinatra::Application

EventMachine.schedule do
  trap("INT") do
    puts "Caught SIGINT"
    EventMachine.stop # this is useless
    # exit # this stops the EventMachine
  end

  i = 0
  while i < 10
    puts "EM Running"
    i += 1
    sleep 1
  end
end

Without trapping the SIGINT, you get the same behavior as when trapping it and calling EM.stop. EM.stop (at least in the pure ruby version, which you can run with EVENTMACHINE_LIBRARY="pure_ruby" thin start) sets a flag that a stop has been requested, which is picked up inside the reactor loop. If the reactor loop is stuck on a step (as in the above case), then it won't exit.

So a couple options:

  1. use the workaround above of trapping SIGINT and forcing an exit. This could leave connections in an unclean state, but they don't call it quick & dirty for nothing ;)

  2. you could put the blocking code inside a Thread or a Fiber, which will allow the reactor to keep running.

  3. look for long-running tasks or loops inside your code, and convert these to be EventMachine aware. em-http-request is a great library for external http requests, and em-synchrony has several other protocols (for database connections, tcp connection pools, etc.). In the above example, this is straightforward: EventMachine.add_periodic_timer(1) { puts "EM Running" }

In your actual code, this might be harder to track down, but look for any places where you spawn threads and join them, or large loops. A profiling tool can help show what code is running when you try to exit, and lastly you can try disabling various parts of the system and libraries to figure out where the culprit is.

查看更多
登录 后发表回答