Execution expired exception crashing Ruby thread, but Timeout::Error is handled

时间:2022-06-03 22:41:46

Can anyone explain why I might see this stack (caused by an HTTParty::post request) when the call to the method looks like this:

任何人都可以解释为什么我可能会看到这个堆栈(由HTTParty :: post请求引起)当方法的调用如下所示:

begin
  response = HTTParty::post(url, options)
rescue
  logger.warn("Could not post to #{url}")      
rescue Timeout::Error
  logger.warn("Could not post to #{url}: timeout")      
end

The stack:

/usr/local/lib/ruby/1.8/timeout.rb:64:in `timeout'
/usr/local/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill'
/usr/local/lib/ruby/1.8/net/protocol.rb:104:in `read_all'
/usr/local/lib/ruby/1.8/net/http.rb:2228:in `read_body_0'
/usr/local/lib/ruby/1.8/net/http.rb:2181:in `read_body'
/usr/local/lib/ruby/1.8/net/http.rb:2206:in `body'
/usr/local/lib/ruby/1.8/net/http.rb:2145:in `reading_body'
/usr/local/lib/ruby/1.8/net/http.rb:1053:in `request_without_newrelic_trace'
[GEM_ROOT]/gems/newrelic_rpm-3.1.1/lib/new_relic/agent/instrumentation/net.rb:20:in `request'
[GEM_ROOT]/gems/newrelic_rpm-3.1.1/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
[GEM_ROOT]/gems/newrelic_rpm-3.1.1/lib/new_relic/agent/instrumentation/net.rb:19:in `request'
/usr/local/lib/ruby/1.8/net/http.rb:1037:in `request_without_newrelic_trace'
/usr/local/lib/ruby/1.8/net/http.rb:543:in `start'
/usr/local/lib/ruby/1.8/net/http.rb:1035:in `request_without_newrelic_trace'
[GEM_ROOT]/gems/newrelic_rpm-3.1.1/lib/new_relic/agent/instrumentation/net.rb:20:in `request'
[GEM_ROOT]/gems/newrelic_rpm-3.1.1/lib/new_relic/agent/method_tracer.rb:242:in `trace_execution_scoped'
[GEM_ROOT]/gems/newrelic_rpm-3.1.1/lib/new_relic/agent/instrumentation/net.rb:19:in `request'
[GEM_ROOT]/gems/httparty-0.7.8/lib/httparty/request.rb:69:in `perform'
[GEM_ROOT]/gems/httparty-0.7.8/lib/httparty.rb:390:in `perform_request'
[GEM_ROOT]/gems/httparty-0.7.8/lib/httparty.rb:358:in `post'
[GEM_ROOT]/gems/httparty-0.7.8/lib/httparty.rb:426:in `post'

As you can see, I am handling the Timeout::Error exception. This is in Ruby 1.8.7. I am well aware that in Ruby 1.8.7, StandardException and TimeoutException have different inheritance trees, so I handle both, but it does not seem to make a difference.

如您所见,我正在处理Timeout :: Error异常。这是在Ruby 1.8.7中。我很清楚在Ruby 1.8.7中,StandardException和TimeoutException有不同的继承树,所以我处理两者,但它似乎没有什么区别。

1 个解决方案

#1


4  

When you omit the exception class in rescue, it will capture any StandardError. Since Timeout::Error is a subclass of StandardError, it will be captured by the first rescue statement. If you want to separately capture it, you have to place that before the omitted one:

当您在rescue中省略异常类时,它将捕获任何StandardError。由于Timeout :: Error是StandardError的子类,因此它将被第一个rescue语句捕获。如果要单独捕获它,则必须在省略之前放置它:

begin
  response = HTTParty::post(url, options)
rescue Timeout::Error
  logger.warn("Could not post to #{url}: timeout")      
rescue
  logger.warn("Could not post to #{url}")      
end

#1


4  

When you omit the exception class in rescue, it will capture any StandardError. Since Timeout::Error is a subclass of StandardError, it will be captured by the first rescue statement. If you want to separately capture it, you have to place that before the omitted one:

当您在rescue中省略异常类时,它将捕获任何StandardError。由于Timeout :: Error是StandardError的子类,因此它将被第一个rescue语句捕获。如果要单独捕获它,则必须在省略之前放置它:

begin
  response = HTTParty::post(url, options)
rescue Timeout::Error
  logger.warn("Could not post to #{url}: timeout")      
rescue
  logger.warn("Could not post to #{url}")      
end