curl-and-python
Re: OpenSSL thread locking callbacks and segfaults
Date: Mon, 3 Jul 2006 09:52:32 +0100
This is my first foray into C, so do please excuse any messiness (patch
adapted from the PHP wrapper patch):
--- ../../pycurl-7.15.1-stable/src/pycurl.c 2006-01-31 09:03:
46.000000000 +0000
+++ pycurl.c 2006-06-23 15:34:42.000000000 +0100
@@ -45,6 +45,30 @@
#undef NDEBUG
#include <assert.h>
+/*for ssl locking*/
+#include <pthread.h>
+#include <openssl/crypto.h>
+#define MUTEX_T pthread_mutex_t *
+
+static MUTEX_T *py_curl_openssl_tsl = NULL;
+
+static void py_curl_ssl_lock(int mode, int n, const char * file, int line)
+{
+ if (mode & CRYPTO_LOCK) {
+ pthread_mutex_lock(py_curl_openssl_tsl[n]);
+ } else {
+ pthread_mutex_unlock(py_curl_openssl_tsl[n]);
+ }
+}
+
+
+static unsigned long py_curl_ssl_id(void)
+{
+ return (unsigned long) pthread_self();
+}
+
+
+
/* Ensure we have updated versions */
#if !defined(PY_VERSION_HEX) || (PY_VERSION_HEX < 0x02020000)
# error "Need Python version 2.2 or greater to compile pycurl."
@@ -2293,6 +2317,22 @@
UNUSED(dummy);
curl_global_cleanup();
Py_INCREF(Py_None);
+
+ /*cleanup for openssl thread locking funcs*/
+ if (py_curl_openssl_tsl) {
+ int i, c = CRYPTO_num_locks();
+ CRYPTO_set_id_callback(NULL);
+ CRYPTO_set_locking_callback(NULL);
+
+ for (i = 0; i < c; ++i) {
+ pthread_mutex_destroy(py_curl_openssl_tsl[i]);
+ free(py_curl_openssl_tsl[i]);
+ }
+
+ free(py_curl_openssl_tsl);
+ py_curl_openssl_tsl = NULL;
+ }
+
return Py_None;
}
@@ -2495,6 +2535,18 @@
PyObject *m, *d;
const curl_version_info_data *vi;
+ /*set locking functions to make openssl threadsafe*/
+ int i, c = CRYPTO_num_locks();
+ py_curl_openssl_tsl = malloc(c * sizeof(pthread_mutex_t));
+ for (i = 0; i < c; ++i) {
+ pthread_mutex_t * mutexp;
+ mutexp = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
+ pthread_mutex_init(mutexp,NULL);
+ py_curl_openssl_tsl[i] = mutexp;
+ }
+ CRYPTO_set_id_callback(py_curl_ssl_id);
+ CRYPTO_set_locking_callback(py_curl_ssl_lock);
+
/* Initialize the type of the new type objects here; doing it here
* is required for portability to Windows without requiring C++. */
p_Curl_Type = &Curl_Type;
On 6/24/06, Daniel Stenberg <daniel_at_haxx.se> wrote:
>
>
> I would assume that will be useful to other pycurl users in a similar
> situation, so please post!
>
> --
> Commercial curl and libcurl Technical Support: http://haxx.se/curl.html
> _______________________________________________
> http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
>
_______________________________________________
http://cool.haxx.se/cgi-bin/mailman/listinfo/curl-and-python
Received on 2006-07-03