Heikki, all,

* Stephen Frost (sfr...@snowman.net) wrote:
> Very curious.  Out of time right now to look into it, but will probably
> be back at it later tonight.

Alright, I was back at this a bit today and decided to go with a hunch-
and it looks like I might have been right to try.

Leaving the locking callback hooks in place appears to prevent the
deadlocks from happening, at least with this little app.

IOW, in destroy_ssl_system(), simply arrange to not have
CRYPTO_set_locking_callback(NULL); and CRYPTO_set_id_callback(NULL);
called.  I've done this with the very simple attached patch.  Per the
comment above destroy_ssl_system(), this doesn't seem to be an
acceptable solution because libpq might get unloaded from the
application, but perhaps it points the way towards what the issue is.

My thought had been that there was an issue with regard to signal
handling, but I've been unable to confirm that, nor is it clear why that
would be the case.  In any case, I'm curious what others think of these
results and if anyone can reproduce the deadlock with this patch
applied.

        Thanks!

                Stephen
diff --git a/src/interfaces/libpq/fe-secure.c b/src/interfaces/libpq/fe-secure.c
new file mode 100644
index 3bd0113..e025b49 100644
*** a/src/interfaces/libpq/fe-secure.c
--- b/src/interfaces/libpq/fe-secure.c
*************** static SSL_CTX *SSL_context = NULL;
*** 102,107 ****
--- 102,108 ----
  
  #ifdef ENABLE_THREAD_SAFETY
  static long ssl_open_connections = 0;
+ static int initialized_hooks = 0;
  
  #ifndef WIN32
  static pthread_mutex_t ssl_config_mutex = PTHREAD_MUTEX_INITIALIZER;
*************** init_ssl_system(PGconn *conn)
*** 947,952 ****
--- 948,955 ----
  
  		if (ssl_open_connections++ == 0)
  		{
+ 			initialized_hooks = 1;
+ 
  			/* These are only required for threaded libcrypto applications */
  			CRYPTO_set_id_callback(pq_threadidcallback);
  			CRYPTO_set_locking_callback(pq_lockingcallback);
*************** destroy_ssl_system(void)
*** 1015,1021 ****
  	if (pq_init_crypto_lib && ssl_open_connections > 0)
  		--ssl_open_connections;
  
! 	if (pq_init_crypto_lib && ssl_open_connections == 0)
  	{
  		/* No connections left, unregister libcrypto callbacks */
  		CRYPTO_set_locking_callback(NULL);
--- 1018,1024 ----
  	if (pq_init_crypto_lib && ssl_open_connections > 0)
  		--ssl_open_connections;
  
! 	if (pq_init_crypto_lib && ssl_open_connections == 0 && initialized_hooks == 0)
  	{
  		/* No connections left, unregister libcrypto callbacks */
  		CRYPTO_set_locking_callback(NULL);

Attachment: signature.asc
Description: Digital signature

Reply via email to