1*6236dae4SAndroid Build Coastguard Worker--- 2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al. 3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl 4*6236dae4SAndroid Build Coastguard WorkerTitle: libcurl-thread 5*6236dae4SAndroid Build Coastguard WorkerSection: 3 6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl 7*6236dae4SAndroid Build Coastguard WorkerSee-also: 8*6236dae4SAndroid Build Coastguard Worker - libcurl-security (3) 9*6236dae4SAndroid Build Coastguard WorkerProtocol: 10*6236dae4SAndroid Build Coastguard Worker - All 11*6236dae4SAndroid Build Coastguard WorkerAdded-in: n/a 12*6236dae4SAndroid Build Coastguard Worker--- 13*6236dae4SAndroid Build Coastguard Worker 14*6236dae4SAndroid Build Coastguard Worker# NAME 15*6236dae4SAndroid Build Coastguard Worker 16*6236dae4SAndroid Build Coastguard Workerlibcurl-thread - libcurl thread safety 17*6236dae4SAndroid Build Coastguard Worker 18*6236dae4SAndroid Build Coastguard Worker# Multi-threading with libcurl 19*6236dae4SAndroid Build Coastguard Worker 20*6236dae4SAndroid Build Coastguard Workerlibcurl is thread safe but has no internal thread synchronization. You may have 21*6236dae4SAndroid Build Coastguard Workerto provide your own locking should you meet any of the thread safety exceptions 22*6236dae4SAndroid Build Coastguard Workerbelow. 23*6236dae4SAndroid Build Coastguard Worker 24*6236dae4SAndroid Build Coastguard Worker# Handles 25*6236dae4SAndroid Build Coastguard Worker 26*6236dae4SAndroid Build Coastguard WorkerYou must **never** share the same handle in multiple threads. You can pass the 27*6236dae4SAndroid Build Coastguard Workerhandles around among threads, but you must never use a single handle from more 28*6236dae4SAndroid Build Coastguard Workerthan one thread at any given time. 29*6236dae4SAndroid Build Coastguard Worker 30*6236dae4SAndroid Build Coastguard Worker# Shared objects 31*6236dae4SAndroid Build Coastguard Worker 32*6236dae4SAndroid Build Coastguard WorkerYou can share certain data between multiple handles by using the share 33*6236dae4SAndroid Build Coastguard Workerinterface but you must provide your own locking and set 34*6236dae4SAndroid Build Coastguard Workercurl_share_setopt(3) CURLSHOPT_LOCKFUNC and CURLSHOPT_UNLOCKFUNC. 35*6236dae4SAndroid Build Coastguard Worker 36*6236dae4SAndroid Build Coastguard WorkerNote that some items are specifically documented as not thread-safe in the 37*6236dae4SAndroid Build Coastguard Workershare API (the connection pool and HSTS cache for example). 38*6236dae4SAndroid Build Coastguard Worker 39*6236dae4SAndroid Build Coastguard Worker# TLS 40*6236dae4SAndroid Build Coastguard Worker 41*6236dae4SAndroid Build Coastguard WorkerAll current TLS libraries libcurl supports are thread-safe. 42*6236dae4SAndroid Build Coastguard Worker 43*6236dae4SAndroid Build Coastguard Worker## OpenSSL 44*6236dae4SAndroid Build Coastguard Worker 45*6236dae4SAndroid Build Coastguard WorkerOpenSSL 1.1.0+ can be safely used in multi-threaded applications provided that 46*6236dae4SAndroid Build Coastguard Workersupport for the underlying OS threading API is built-in. For older versions of 47*6236dae4SAndroid Build Coastguard WorkerOpenSSL, the user must set mutex callbacks. 48*6236dae4SAndroid Build Coastguard Worker 49*6236dae4SAndroid Build Coastguard Workerlibcurl may not be able to fully clean up after multi-threaded OpenSSL 50*6236dae4SAndroid Build Coastguard Workerdepending on how OpenSSL was built and loaded as a library. It is possible in 51*6236dae4SAndroid Build Coastguard Workersome rare circumstances a memory leak could occur unless you implement your own 52*6236dae4SAndroid Build Coastguard WorkerOpenSSL thread cleanup. 53*6236dae4SAndroid Build Coastguard Worker 54*6236dae4SAndroid Build Coastguard WorkerFor example, on Windows if both libcurl and OpenSSL are linked statically to a 55*6236dae4SAndroid Build Coastguard WorkerDLL or application then OpenSSL may leak memory unless the DLL or application 56*6236dae4SAndroid Build Coastguard Workercalls OPENSSL_thread_stop() before each thread terminates. If OpenSSL is built 57*6236dae4SAndroid Build Coastguard Workeras a DLL then it does this cleanup automatically and there is no leak. If 58*6236dae4SAndroid Build Coastguard Workerlibcurl is built as a DLL and OpenSSL is linked statically to it then libcurl 59*6236dae4SAndroid Build Coastguard Workerdoes this cleanup automatically and there is no leak (added in libcurl 8.8.0). 60*6236dae4SAndroid Build Coastguard Worker 61*6236dae4SAndroid Build Coastguard WorkerPlease review the OpenSSL documentation for a full list of circumstances: 62*6236dae4SAndroid Build Coastguard Workerhttps://docs.openssl.org/3.0/man3/OPENSSL_init_crypto/#notes 63*6236dae4SAndroid Build Coastguard Worker 64*6236dae4SAndroid Build Coastguard Worker# Signals 65*6236dae4SAndroid Build Coastguard Worker 66*6236dae4SAndroid Build Coastguard WorkerSignals are used for timing out name resolves (during DNS lookup) - when built 67*6236dae4SAndroid Build Coastguard Workerwithout using either the c-ares or threaded resolver backends. On systems that 68*6236dae4SAndroid Build Coastguard Workerhave a signal concept. 69*6236dae4SAndroid Build Coastguard Worker 70*6236dae4SAndroid Build Coastguard WorkerWhen using multiple threads you should set the CURLOPT_NOSIGNAL(3) 71*6236dae4SAndroid Build Coastguard Workeroption to 1L for all handles. Everything works fine except that timeouts 72*6236dae4SAndroid Build Coastguard Workercannot be honored during DNS lookups - which you can work around by building 73*6236dae4SAndroid Build Coastguard Workerlibcurl with c-ares or threaded-resolver support. c-ares is a library that 74*6236dae4SAndroid Build Coastguard Workerprovides asynchronous name resolves. On some platforms, libcurl simply cannot 75*6236dae4SAndroid Build Coastguard Workerfunction properly multi-threaded unless the CURLOPT_NOSIGNAL(3) option 76*6236dae4SAndroid Build Coastguard Workeris set. 77*6236dae4SAndroid Build Coastguard Worker 78*6236dae4SAndroid Build Coastguard WorkerWhen CURLOPT_NOSIGNAL(3) is set to 1L, your application needs to deal 79*6236dae4SAndroid Build Coastguard Workerwith the risk of a SIGPIPE (that at least the OpenSSL backend can 80*6236dae4SAndroid Build Coastguard Workertrigger). Note that setting CURLOPT_NOSIGNAL(3) to 0L does not work in a 81*6236dae4SAndroid Build Coastguard Workerthreaded situation as there is a race condition where libcurl risks restoring 82*6236dae4SAndroid Build Coastguard Workerthe former signal handler while another thread should still ignore it. 83*6236dae4SAndroid Build Coastguard Worker 84*6236dae4SAndroid Build Coastguard Worker# Name resolving 85*6236dae4SAndroid Build Coastguard Worker 86*6236dae4SAndroid Build Coastguard WorkerThe **gethostbyname** or **getaddrinfo** and other name resolving system 87*6236dae4SAndroid Build Coastguard Workercalls used by libcurl are provided by your operating system and must be thread 88*6236dae4SAndroid Build Coastguard Workersafe. It is important that libcurl can find and use thread safe versions of 89*6236dae4SAndroid Build Coastguard Workerthese and other system calls, as otherwise it cannot function fully thread 90*6236dae4SAndroid Build Coastguard Workersafe. Some operating systems are known to have faulty thread 91*6236dae4SAndroid Build Coastguard Workerimplementations. We have previously received problem reports on *BSD (at least 92*6236dae4SAndroid Build Coastguard Workerin the past, they may be working fine these days). Some operating systems that 93*6236dae4SAndroid Build Coastguard Workerare known to have solid and working thread support are Linux, Solaris and 94*6236dae4SAndroid Build Coastguard WorkerWindows. 95*6236dae4SAndroid Build Coastguard Worker 96*6236dae4SAndroid Build Coastguard Worker# curl_global_* functions 97*6236dae4SAndroid Build Coastguard Worker 98*6236dae4SAndroid Build Coastguard WorkerThese functions are thread-safe since libcurl 7.84.0 if 99*6236dae4SAndroid Build Coastguard Workercurl_version_info(3) has the **CURL_VERSION_THREADSAFE** feature bit 100*6236dae4SAndroid Build Coastguard Workerset (most platforms). 101*6236dae4SAndroid Build Coastguard Worker 102*6236dae4SAndroid Build Coastguard WorkerIf these functions are not thread-safe and you are using libcurl with multiple 103*6236dae4SAndroid Build Coastguard Workerthreads it is especially important that before use you call 104*6236dae4SAndroid Build Coastguard Workercurl_global_init(3) or curl_global_init_mem(3) to explicitly 105*6236dae4SAndroid Build Coastguard Workerinitialize the library and its dependents, rather than rely on the "lazy" 106*6236dae4SAndroid Build Coastguard Workerfail-safe initialization that takes place the first time 107*6236dae4SAndroid Build Coastguard Workercurl_easy_init(3) is called. For an in-depth explanation refer to 108*6236dae4SAndroid Build Coastguard Workerlibcurl(3) section **GLOBAL CONSTANTS**. 109*6236dae4SAndroid Build Coastguard Worker 110*6236dae4SAndroid Build Coastguard Worker# Memory functions 111*6236dae4SAndroid Build Coastguard Worker 112*6236dae4SAndroid Build Coastguard WorkerThese functions, provided either by your operating system or your own 113*6236dae4SAndroid Build Coastguard Workerreplacements, must be thread safe. You can use curl_global_init_mem(3) 114*6236dae4SAndroid Build Coastguard Workerto set your own replacement memory functions. 115*6236dae4SAndroid Build Coastguard Worker 116*6236dae4SAndroid Build Coastguard Worker# Non-safe functions 117*6236dae4SAndroid Build Coastguard Worker 118*6236dae4SAndroid Build Coastguard WorkerCURLOPT_DNS_USE_GLOBAL_CACHE(3) is not thread-safe. 119*6236dae4SAndroid Build Coastguard Worker 120*6236dae4SAndroid Build Coastguard Workercurl_version_info(3) is not thread-safe before libcurl initialization. 121