Search code examples
rubysocketstimeout

Socket timeout in Ruby


I have the following code basically listening on port 443 using SSL (copying from another question here): How can I get info on an already closed SSL connection that caused an error?

#!/usr/bin/env ruby

require "socket"
require "openssl"

cert_path = Dir["/etc/letsencrypt/live/example.com/fullchain.pem"].first
key_path = Dir["/etc/letsencrypt/live/example.com/privkey.pem"].first
chain_path = Dir["/etc/letsencrypt/live/example.com/chain.pem"].first
context = OpenSSL::SSL::SSLContext.new
context.cert = OpenSSL::X509::Certificate.new( File.read( cert_path ))
context.key = OpenSSL::PKey::RSA.new( File.read( key_path ))
context.extra_chain_cert = [ OpenSSL::X509::Certificate.new( File.read( chain_path )) ]

port = 443
t = TCPServer.new( port )
s = OpenSSL::SSL::SSLServer.new( t, context )

begin
    r = s.accept
rescue
    # get client ip here
    p r.methods.sort
end

Client sends info through a post request to the server and the code runs over 35 minutes here and then the result about the operation is sent back to client. The call is a blocking one and I need it this way.

The problem is that if the code runs over 30 minutes then the server code cannot send back the info. If under 30 min, then it can without any problem. Operating system is Linux (Ubuntu 18 x64) and I keep checking the sockets with netstat and they are there till the end of the procedure with ESTABLISHED status, no suspicious things here.

I assume it has to do something with some kind of system timeout value. From the post call I set all timeout that I can to the HTTP object (open, read, keep_alive, continue, write, ssl), still no luck, the result does not get back to the client.

Could you give me any hints about what setting could cause this issue? Thank you.


Edit:

I've tested my code without the SSL layer and there is no timeout issue. I also tested the timeout and ssl_timeout properties of the Contex object above without any result. Still looking for a setting to avoid the problem caused by timeout in the SSL layer.


Edit:

I've tested the call through SSL using Python3 on client side instead of Ruby (2.6) and the same issue happens. Server side has Ruby. So it is either the Ruby implementaion of OpenSSL that causes it or OpenSSL itself.


Edit:

New progress: I noticed that initiating the procedure through SSL on the same host does not have a problem so I'm thinking that my hosting provider might put some limit to the longer connections.


Edit:

Solved. I would have never thought so but my ISP has something to do with the time limitation issues of my longer connections.


Solution

  • I have tested and my ISP has something to do with the time limitation issues of my longer connections.