如果Net::HTTP
的open_timeout
和read_timeout
没有设置,会无限等待吗?
According to the docs ,open_timeout定义为:
Number of seconds to wait for the connection to open. Any number may be used, including Floats for fractional seconds. If the HTTP object cannot open a connection in this many seconds, it raises a Net::OpenTimeout exception. The default value is nil.
好的,所以默认值为 nil。如果服务器(例如,恶意服务器)从不响应并且客户端未设置 open_timeout
,会发生什么情况?客户会无限期地等待吗?
我问这个问题是因为我正在尝试调试一个场景,其中我的所有进程都无限期地挂起,并且 SSL 连接请求位于堆栈顶部:
#0 0x00007f33a07acb13 in ppoll () from /lib/x86_64-linux-gnu/libc.so.6
#1 0x00007f33a1962558 in rb_wait_for_single_fd (fd=<optimized out>, events=<optimized out>, tv=0x0) at thread.c:3583
#2 0x00007f33a1966ea2 in rb_thread_wait_fd_rw (read=1, fd=<optimized out>) at thread.c:3422
#3 rb_thread_wait_fd (fd=<optimized out>) at thread.c:3433
#4 0x00007f33a1854ebf in rb_io_wait_readable (f=40) at io.c:1081
#5 0x00007f339ed72184 in ossl_start_ssl (self=139859849103720, func=0x7f339eb1ab20 <SSL_connect>, funcname=0x7f339ed82d89 "SSL_connect", nonblock=<optimized out>) at ossl_ssl.c:1278
这显然是由这个调用触发的:
from /usr/local/lib/ruby/gems/2.0.0/gems/httparty-0.11.0/lib/httparty.rb:497:in `post'
from /usr/local/lib/ruby/gems/2.0.0/gems/httparty-0.11.0/lib/httparty.rb:414:in `post'
from /usr/local/lib/ruby/gems/2.0.0/gems/httparty-0.11.0/lib/httparty.rb:461:in `perform_request'
from /usr/local/lib/ruby/gems/2.0.0/gems/httparty-0.11.0/lib/httparty/request.rb:92:in `perform'
from /usr/local/lib/ruby/2.0.0/net/http.rb:1367:in `request'
from /usr/local/lib/ruby/2.0.0/net/http.rb:851:in `start'
from /usr/local/lib/ruby/2.0.0/net/http.rb:862:in `do_start'
from /usr/local/lib/ruby/2.0.0/net/http.rb:918:in `connect'
from /usr/local/lib/ruby/2.0.0/timeout.rb:52:in `timeout'
from /usr/local/lib/ruby/2.0.0/net/http.rb:918:in `block in connect'
from /usr/local/lib/ruby/2.0.0/net/http.rb:918:in `connect'
最佳答案
这实际上意味着服务器存在并接受请求,但不返回数据,仍然保持连接打开。是的,在这种恶意服务器的情况下,客户端将会挂断。
Net::HTTP
中没有默认超时。正常情况下,它会收到HTTPRequestTimeOut
响应(哪个服务器负责)。如果您觉得可能有问题,您可以自己设置超时。但有时你确实需要无限等待(服务器正在处理大量数据,你知道这一点。)
实际上,我认为这不是 Net::HTTP 问题
。马茨has already had problems通过rb_wait_for_single_fd 调用
。它实际上看起来像是 ruby 线程同步中的死锁,而不是服务器挂起。
关于ruby - 如果Net::HTTP的open_timeout和read_timeout没有设置的话,会无限等待吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20450314/