curl學習(一):段錯誤

c語言使用curl時出現段錯誤

  • 問題發現
    在使用c語言編程時,按照一般流程使用curl進行http數據的獲取,沒有錯誤,但是當開啓線程調用該函數頻繁時,會出現段錯誤,並且復現起來較爲困難,經後期排查,發現問題出現在了curl上面,處理完該bug後,稍加整理,作爲以後提醒

  • 原因

  • 原來libcurl在configure默認配置編譯的情況下,它是使用alarm+siglongjmp實現域名解析超時。當多個線程都使用超時處理的時候,同時主線程中有sleep或是wait等操作。libcurl將會發信號打斷這個wait從而導致程序退出

  • 解決方法

  • *在設置curl屬性時添加一條
  • curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
  • 這樣域名解析就沒了超時機制*

  • 官網說明
    **`CURLOPT_NOSIGNAL

Pass a long. If it is 1, libcurl will not use any functions that install
signal handlers or any functions that cause signals to be sent to
unix applications to still set/use all timeout options etc, without
risking getting signals. (Added in 7.10)

If this option is set and libcurl has been built with the standard
name resolver, timeouts will not occur while the name resolve
takes place. Consider building libcurl with c-ares support to enable
asynchronous DNS lookups, which enables nice timeouts for name
resolves without signals.

Setting CURLOPT_NOSIGNAL to 1 makes libcurl NOT ask the
system to ignore SIGPIPE signals, which otherwise are sent by the
system when trying to send data to a socket which is closed in the
other end. libcurl makes an effort to never cause such SIGPIPEs to
trigger, but some operating systems have no way to avoid them
and even on those that have there are some corner cases when
they may still happen, contrary to our desire. In addition, using
CURLAUTH_NTLM_WB authentication could cause a SIGCHLD
signal to be raised.`**

參考鏈接
http://www.xuebuyuan.com/126531.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章