ruby - 如果Net::HTTP的open_timeout和read_timeout没有设置的话,会无限等待吗?

标签 ruby net-http

如果Net::HTTPopen_timeoutread_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/

相关文章:

ruby-on-rails - Rake 12.3 仍然产生 NoMethodError : undefined method 'last_comment'

ruby-on-rails - Hartl Rails 教程 5.3.4 : 'read' : Is a directory @ io_fread) error

更新证书后,Ruby Net::HTTP 响应 OpenSSL::SSL::SSLError "certificate verify failed"

ruby-on-rails - OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv3 read server hello A

ruby-on-rails - Rails 不使用生成的 CSS 文件

ruby - 从方法内部的 block 方法返回 true 或 false

ruby-on-rails - 模型中匹配所有关联值的方法

http - Go Web 服务器的进程管理

ruby jiraSOAP net-http ssl 验证

ruby - 将 `Authorization Bearer` 哈希添加到 Net::HTTP 发布请求(Ruby)