Skip to content

Instantly share code, notes, and snippets.

@Teshootub7
Created November 25, 2010 05:34
Show Gist options
  • Select an option

  • Save Teshootub7/714955 to your computer and use it in GitHub Desktop.

Select an option

Save Teshootub7/714955 to your computer and use it in GitHub Desktop.
Patch for HTTPClient to fix the hang-up problem
diff --git a/lib/httpclient.rb b/lib/httpclient.rb
index 11a4da4..a250dfe 100644
--- a/lib/httpclient.rb
+++ b/lib/httpclient.rb
@@ -777,22 +777,26 @@ private
def do_request_async(method, uri, query, body, extheader)
conn = Connection.new
t = Thread.new(conn) { |tconn|
- if HTTP::Message.file?(body)
- pos = body.pos rescue nil
- end
- retry_count = @session_manager.protocol_retry_count
- proxy = no_proxy?(uri) ? nil : @proxy
- while retry_count > 0
- body.pos = pos if pos
- req = create_request(method, uri, query, body, extheader)
- begin
- protect_keep_alive_disconnected do
- do_get_stream(req, proxy, tconn)
+ begin
+ if HTTP::Message.file?(body)
+ pos = body.pos rescue nil
+ end
+ retry_count = @session_manager.protocol_retry_count
+ proxy = no_proxy?(uri) ? nil : @proxy
+ while retry_count > 0
+ body.pos = pos if pos
+ req = create_request(method, uri, query, body, extheader)
+ begin
+ protect_keep_alive_disconnected do
+ do_get_stream(req, proxy, tconn)
+ end
+ break
+ rescue RetryableResponse
+ retry_count -= 1
end
- break
- rescue RetryableResponse
- retry_count -= 1
end
+ rescue
+ conn.push $!
end
}
conn.async_thread = t
diff --git a/lib/httpclient/connection.rb b/lib/httpclient/connection.rb
index dc4052a..9fde5e8 100644
--- a/lib/httpclient/connection.rb
+++ b/lib/httpclient/connection.rb
@@ -64,7 +64,11 @@ class HTTPClient
# Retrieves a HTTP::Message instance of HTTP response. Do not invoke this
# method twice for now. The second invocation will be blocked.
def pop
- @queue.pop
+ response_or_exception = @queue.pop
+ if response_or_exception.is_a? Exception
+ raise response_or_exception
+ end
+ response_or_exception
end
def push(result) # :nodoc:
diff --git a/test/test_httpclient.rb b/test/test_httpclient.rb
index 94129c2..d5e00aa 100644
--- a/test/test_httpclient.rb
+++ b/test/test_httpclient.rb
@@ -825,6 +825,13 @@ EOS
assert_equal('hello', @client.post(@url + 'sleep', :sec => 2).content)
end
+ def test_async_error
+ assert_raise( SocketError ) do
+ conn = @client.get_async("http://non-existing-host/")
+ conn.pop
+ end
+ end
+
def test_reset
url = @url + 'servlet'
assert_nothing_raised do
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment