title: Gracefully exiting from thread in Ruby
body: <p>I am trying out Mongrel and using the following code:</p>
  
  <pre><code>require 'rubygems'
  require 'mongrel'
  
  class SimpleHandler &lt; Mongrel::HttpHandler
      def process(request, response)
          response.start(200) do |head, out|
              head["Content-Type"] = "text/plain"
              out.write("Hello World!\n")
          end
      end
  end
  
  h = Mongrel::HttpServer.new("0.0.0.0", "3000")
  h.register("/test", SimpleHandler.new)
  puts "Press Control-C to exit"
  h.run.join
  
  trap("INT") do
      puts "Exiting..."
  end
  </code></pre>
  
  <p>Basically, this just prints out "Hello World!" when I go to localhost:3000/test. It works fine, and I can close the program with Control-C. But when I press Control-C, this gets outputted:</p>
  
  <pre><code>my_web_server.rb:17:in `join': Interrupt
  from my_web_server.rb:17
  </code></pre>
  
  <p>So I tried putting that <code>trap("INT")</code> statement at the end, but it isn't getting called. Solution?</p>
  
  <p>Thanks.</p>