简体   繁体   中英

ssl`sysread_nonblock': end of file reached (EOFError)

I have written a code which uses ruby threads.

require 'rubygems'
require 'net/http'
require 'uri'

def get_response()

  uri = URI.parse('https://..........')
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  -----
  -----
end

t1 = []
15.times do |i|
t1[i] = Thread.new{
hit_mdm(i)
sleep(rand(0)/10.0)
}
end

t1.each {|t| t.join}

The code works fine, but when the programs reaches its end it throws following error:

ruby/2.0.0/openssl/buffering.rb:174:in `sysread_nonblock': end of file reached (EOFError)

How to overcome this problem.

def getHttp(uri)
    begin       
        http = Net::HTTP.new(uri.host, uri.port)        
    rescue
        p 'failed Net::HTTP.new', uri
        retry       
    end
    http
end 

based on the downvoted answer, I attached some code to show an catch exception example

You haven't specified what hit_mdm() is, but presumably it's something that calls get_response considering your Net::HTTP setup prior.

There's many places on the web where you can find evidence that Net::HTTP is probably thread safe, though nothing conclusive.

I've done lots of stress testing with Net::HTTP and threads and my experience is that the EOFErrors are common problems with multiple HTTP connections. Whether it's happening because of the server or the client or the connection or the Net::HTTP library is going to be very difficult to debug, especially using threaded code which does TCP communication, which is also threaded, in a sense.

You could use wireshark to figure out where the EOFError is coming from, or, you could save yourself a lot of headache and just rescue the EOFError on the sysread (your backtrace can tell you where to put the rescue so it's only effecting the Net::HTTP call, if that's where the EOFError is generated).

But without more info, we can't really tell you why the EOFError is happening for sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM