Debian Codegraph @

doc/thread-safety.rst

raw
1 .. _thread-safety:
2
3 Thread Safety
4 =============
5
6 Per `libcurl thread safety documentation`_, libcurl is thread-safe but
7 has no internal thread synchronization.
8
9 For Python programs using PycURL, this means:
10
11 * Accessing the same PycURL object from different threads is OK when
12 this object is not involved in active transfers, as Python internally
13 has a Global Interpreter Lock and only one operating system thread can
14 be executing Python code at a time.
15
16 * Accessing a PycURL object that is involved in an active transfer from
17 Python code *inside a libcurl callback for the PycURL object in question*
18 is OK, because PycURL takes out the appropriate locks.
19
20 * Accessing a PycURL object that is involved in an active transfer from
21 Python code *outside of a libcurl callback for the PycURL object in question*
22 is unsafe.
23
24 * Closing a ``CurlShare`` object with ``detach_on_close=True`` (the
25 default) is **not thread-safe** with respect to the associated
26 ``Curl`` objects. During ``CurlShare.close()``, PycURL automatically
27 detaches all associated ``Curl`` objects by clearing their ``SHARE``
28 option. The caller must ensure that no other thread is using the
29 associated ``Curl`` objects while ``CurlShare.close()`` is executing.
30
31 * A WebSocket handle (``CONNECT_ONLY=2`` plus ``ws_send`` / ``ws_recv``)
32 follows the same one-handle-one-thread rule: do not call ``ws_send``
33 from one thread while ``ws_recv`` runs on another. Serialise access
34 with a lock. Calls from another thread while ``perform()`` is running
35 are unsafe unless they happen inside that handle's active
36 ``WRITEFUNCTION`` callback.
37
38 PycURL handles the necessary SSL locks for OpenSSL/LibreSSL/BoringSSL,
39 GnuTLS, NSS, mbedTLS and wolfSSL.
40
41 A special situation exists when libcurl uses the standard C library
42 name resolver (i.e., not threaded nor c-ares resolver). By default libcurl
43 uses signals for timeouts with the C library resolver, and signals do not
44 work properly in multi-threaded programs. When using PycURL objects from
45 multiple Python threads ``NOSIGNAL`` option `must be given`_.
46
47 .. _libcurl thread safety documentation: https://curl.haxx.se/libcurl/c/threadsafe.html
48 .. _must be given: https://github.com/curl/curl/issues/1003