Re: BUG #18732: Segfault in pgbench on max_connections starvation - Mailing list pgsql-bugs

From Andres Freund
Subject Re: BUG #18732: Segfault in pgbench on max_connections starvation
Date
Msg-id zkxk3e4eomyh2emcuwe466agbx3xuduqv2mimeaz3hqq55mwap@h4fn53tb2v6n
Whole thread Raw
In response to Re: BUG #18732: Segfault in pgbench on max_connections starvation  (Heikki Linnakangas <hlinnaka@iki.fi>)
List pgsql-bugs
Hi,

On 2024-12-03 16:52:32 +0200, Heikki Linnakangas wrote:
> It looks like a race condition between OpenSSL's exit handler and the .
> HMAC_Init_ex() call in another thread. I think we could use the
> OPENSSL_INIT_NO_ATEXIT option to prevent the atexit handler from running.
> The OpenSSL man page on OPENSSL_init_crypto says:

Using exit() while another thread is running is, IIRC, undefined behaviour,
regardless of OPENSSL_INIT_NO_ATEXIT's pointlessness. The whole atexit()
mechanism is not threadsafe, two processes exit()ing at the same time can
cause a lot of havoc.

Short term it's probably easiest to just use _exit(). Medium term I think we
should just exit individual threads - which would probably require the main
thread to not run a benchmark itself.


> > By default OpenSSL will attempt to clean itself up when the process
> > exits via an "atexit" handler. Using this option suppresses that
> > behaviour. This means that the application will have to clean up
> > OpenSSL explicitly using OPENSSL_cleanup().
> 
> I don't understand why that cleanup would be needed. When the program exits,
> all resources are gone anyway.

Somewhat random aside: This is also bad for postgres performance. Postmaster
initializes openssl. When a child exits, it runs - completely pointlessly -
OPENSSL_cleanup(), which modifies a lot of datastructures that have been set
up in postmaster. Which, in turn, requires all those pages to be
copy-on-write'ed. Just for that copy to immediately be discarded, at process
exit.

Greetings,

Andres Freund



pgsql-bugs by date:

Previous
From: Heikki Linnakangas
Date:
Subject: Re: BUG #18732: Segfault in pgbench on max_connections starvation
Next
From: PG Bug reporting form
Date:
Subject: BUG #18733: Connection Timeout after upgrade