requests库卡在getaddrinfo做dns查询
0x1 问题
最近在项目中,老是碰到代码卡死的问题,之前同事使用定时重启机器的办法来解决。
try:
rt = s.get(url, timeout=3, headers=header, verify=False)
except Exception as e:
logging.debug('request_err:%s'% e.message)
continue
一直以来,以为设置超时后,这段代码一定会再3秒内返回,抛出异常。
通过gdb调试后看到如下的堆栈:
(gdb) py-bt
#6 Frame 0x7feaf8045a00, for file /usr/lib/python2.7/site-packages/urllib3/util/connection.py, line 60, in create_connection (address=('m.weibo.cn', 443), timeout=3, source_address=None, socket_options=[(6, 1, 1)], host='m.weibo.cn', port=443, err=None, family=0)
for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
#11 Frame 0x7feb08009a10, for file /usr/lib/python2.7/site-packages/urllib3/connection.py, line 141, in _new_conn (self=<VerifiedHTTPSConnection(cert_file=None, ca_cert_dir=None, cert_reqs='CERT_NONE', port=443, assert_hostname=None, _tunnel_port=None, _buffer=[], sock=None, strict=True, _tunnel_headers={}, source_address=None, _tunnel_host=None, assert_fingerprint=None, ssl_context=