Skip to content

Instantly share code, notes, and snippets.

@myronmarston
Created October 14, 2012 05:36
Show Gist options
  • Save myronmarston/3887508 to your computer and use it in GitHub Desktop.
Save myronmarston/3887508 to your computer and use it in GitHub Desktop.

Typhoeus Thread Deadlock Example

It works when running a server in a separate process:

$ bundle exec ruby example.rb
Booted sinatra app
Using typhoeus to make a request to the app...
Response: hello world

...but if you run the server in a separate thread in the same process as the client, it deadlocks:

$ THREADED=1 bundle exec ruby example.rb

ctrl-c won't even kill it. You've got to kill -9 it to stop it.

This used to work with Typhoeus 0.3 and 0.4.

require './localhost_server'
require 'sinatra/base'
app = Sinatra.new do
get('/') { "hello world" }
end
LocalhostServer.new(app.new, 8888)
puts "Booted sinatra app"
require 'typhoeus'
puts "Using typhoeus to make a request to the app..."
request = Typhoeus::Request.new("http://localhost:8888/", :method => :get)
request.run
puts "Response: #{request.response.body}"
# A sample Gemfile
source "https://rubygems.org"
gem 'typhoeus', git: 'git://github.com/typhoeus/typhoeus.git'
gem 'sinatra'
GIT
remote: git://github.com/typhoeus/typhoeus.git
revision: 73ef1a00c2615cf77e92591c1efd885ffafb16b6
specs:
typhoeus (0.5.0.rc)
ethon (= 0.5.0)
GEM
remote: https://rubygems.org/
specs:
ethon (0.5.0)
ffi (~> 1.0.11)
mime-types (~> 1.18)
ffi (1.0.11)
mime-types (1.19)
rack (1.4.1)
rack-protection (1.2.0)
rack
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)
PLATFORMS
ruby
DEPENDENCIES
sinatra
typhoeus!
require 'rack'
require 'rack/handler/webrick'
require 'net/http'
# The code for this is inspired by Capybara's server:
# http://github.com/jnicklas/capybara/blob/0.3.9/lib/capybara/server.rb
class LocalhostServer
READY_MESSAGE = "Server ready"
class Identify
def initialize(app)
@app = app
end
def call(env)
if env["PATH_INFO"] == "/__identify__"
[200, {}, [LocalhostServer::READY_MESSAGE]]
else
@app.call(env)
end
end
end
attr_reader :port
def initialize(rack_app, port = nil)
@port = port || find_available_port
@rack_app = rack_app
concurrently { boot }
wait_until(10, "Boot failed.") { booted? }
end
private
def find_available_port
server = TCPServer.new('127.0.0.1', 0)
server.addr[1]
ensure
server.close if server
end
def boot
# Use WEBrick since it's part of the ruby standard library and is available on all ruby interpreters.
options = { :Port => port }
options.merge!(:AccessLog => [], :Logger => WEBrick::BasicLog.new(StringIO.new)) unless ENV['VERBOSE_SERVER']
Rack::Handler::WEBrick.run(Identify.new(@rack_app), options)
end
def booted?
res = ::Net::HTTP.get_response("localhost", '/__identify__', port)
if res.is_a?(::Net::HTTPSuccess) or res.is_a?(::Net::HTTPRedirection)
return res.body == READY_MESSAGE
end
rescue Errno::ECONNREFUSED, Errno::EBADF
return false
end
def concurrently
if should_use_subprocess?
pid = Process.fork do
trap(:INT) { ::Rack::Handler::WEBrick.shutdown }
yield
exit # manually exit; otherwise this sub-process will re-run the specs that haven't run yet.
end
at_exit do
Process.kill('INT', pid)
begin
Process.wait(pid)
rescue Errno::ECHILD
# ignore this error...I think it means the child process has already exited.
end
end
else
Thread.new { yield }
end
end
def should_use_subprocess?
!ENV['THREADED']
end
def wait_until(timeout, error_message, &block)
start_time = Time.now
while true
return if yield
raise TimeoutError.new(error_message) if (Time.now - start_time) > timeout
sleep(0.05)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment