]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Set pycurl.NOSIGNAL when multiple threads are detected.
authorBen Darnell <ben@bendarnell.com>
Thu, 26 Aug 2010 18:09:56 +0000 (11:09 -0700)
committerBen Darnell <ben@bendarnell.com>
Thu, 26 Aug 2010 18:10:10 +0000 (11:10 -0700)
Closes #121.

tornado/httpclient.py

index 8d0402c97f5e0a47f0a16c7380cb576f34593992..cfa0b4c60250739287f175ca43adad36fb99ffa2 100644 (file)
@@ -27,6 +27,7 @@ import httplib
 import logging
 import pycurl
 import sys
+import threading
 import time
 import weakref
 
@@ -543,7 +544,7 @@ def _curl_setup_request(curl, request, buffer, headers):
         curl.setopt(pycurl.PROXY, request.proxy_host)
         curl.setopt(pycurl.PROXYPORT, request.proxy_port)
         if request.proxy_username:
-            credentials = '%s:%s' % (request.proxy_username, 
+            credentials = '%s:%s' % (request.proxy_username,
                     request.proxy_password)
             curl.setopt(pycurl.PROXYUSERPWD, credentials)
 
@@ -588,6 +589,16 @@ def _curl_setup_request(curl, request, buffer, headers):
     else:
         curl.unsetopt(pycurl.USERPWD)
         logging.info("%s %s", request.method, request.url)
+    if threading.active_count() > 1:
+        # libcurl/pycurl is not thread-safe by default.  When multiple threads
+        # are used, signals should be disabled.  This has the side effect
+        # of disabling DNS timeouts in some environments (when libcurl is
+        # not linked against ares), so we don't do it when there is only one
+        # thread.  Applications that use many short-lived threads may need
+        # to set NOSIGNAL manually in a prepare_curl_callback since
+        # there may not be any other threads running at the time we call
+        # threading.active_count.
+        curl.setopt(pycurl.NOSIGNAL, 1)
     if request.prepare_curl_callback is not None:
         request.prepare_curl_callback(curl)