tags:

views:

306

answers:

3

I'm having trouble with Timeouts which do not work reliably under JRuby (Linux). More specifically, if I send a HEAD Request to a remote server and this server is busy (does not respond), the configured timeout does not fire and my application stalls for a long time. I tried both, setting read_timeout and using the timeout() function but none worked reliably:

  Timeout::timeout(5) do
    Net::HTTP.start(uri.host, uri.port) do |http|
      http.read_timeout = 5
      http.request_head(uri.request_uri)
    end
  end

For Ruby this problem is extensively document at the SystemTimer page. However, the proposed SystemTimer gem cannot be used with JRuby.

Does anyone have an idea how to get reliable timeouts for HTTP requests in JRuby?

PS: This is JRuby 1.1.6 / 32-bit Linux / Sun Java 1.6

+1  A: 

This problem has been specifically targeted and handled over the last few versions of JRuby. In particular, JRuby has been fixed to allow timeout.rb to work with blocking IO. You want to be on 1.3. JRuby 1.1.6 is pretty old and has a number of issues that have since been resolved.

Yehuda Katz
Thanks for your answer. This solves the problem though I've been reluctant to upgrade because of loads of (seemingly harmless) openssl-related warnings and a nasty character encoding problem with Hpricot: http://jira.codehaus.org/browse/JRUBY-3732
Pankrat
A: 

I don't see this as fixed in 1.3.1 either?

http://jira.codehaus.org/browse/JRUBY-3880

A: 

This isn't a great solution but I ended up wrapping Apache HTTPClient for this functionality.

Note that this code is VERY raw:

http://github.com/ikai/jruby-rest-client/tree/master

Ikai Lan