Re: Probable memory leak with ECPG and AIX

From: Noah Misch <noah(at)leadboat(dot)com>
To: Guillaume Lelarge <guillaume(at)lelarge(dot)info>, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Benoit Lobréau <benoit(dot)lobreau(at)gmail(dot)com>, PostgreSQL Developers <pgsql-hackers(at)postgresql(dot)org>, "Dr(dot) Michael Meskes" <michael(dot)meskes(at)credativ(dot)com>
Subject: Re: Probable memory leak with ECPG and AIX
Date: 2022-04-18 04:16:45
Message-ID: 20220418041645.GA1134228@rfd.leadboat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Jan 01, 2022 at 04:07:50PM -0800, Noah Misch wrote:
> On Sat, Jan 01, 2022 at 11:35:02AM -0500, Tom Lane wrote:
> > Noah Misch <noah(at)leadboat(dot)com> writes:
> > > I get the same results. The leak arises because AIX freelocale() doesn't free
> > > all memory allocated in newlocale(). The following program uses trivial
> > > memory on GNU/Linux, but it leaks like you're seeing on AIX:
> >
> > Bleah.
> >
> > > If you have access to file an AIX bug, I recommend doing so. If we want
> > > PostgreSQL to work around this, one idea is to have ECPG do this newlocale()
> > > less often. For example, do it once per process or once per connection
> > > instead of once per ecpg_do_prologue().
> >
> > It's worse than that: see also ECPGget_desc(). Seems like a case
> > could be made for doing something about this just on the basis
> > of cycles expended, never mind freelocale() bugs.
>
> Agreed. Once per process seems best. I only hesitated before since it means
> nothing will free this storage, which could be annoying in the context of
> Valgrind and similar. However, ECPG already has bits of never-freed memory in
> the form of pthread_key_create() calls having no pthread_key_delete(), so I
> don't mind adding a bit more.

The comparison to pthread_key_create() wasn't completely fair. While POSIX
welcomes pthread_key_create() to fail with ENOMEM, the glibc implementation
appears not to allocate memory. Even so, I'm okay leaking one newlocale() per
process lifetime.

I had expected to use pthread_once() for the newlocale() call, but there would
be no useful way to report failure and try again later. Instead, I called
newlocale() while ECPGconnect() holds connections_mutex. See log message and
comments for details. I tested "./configure ac_cv_func_uselocale=no ..." and
tested the scenario of newlocale() failing every time.

Attachment Content-Type Size
newlocale-once-ecpg-v1.patch text/plain 6.7 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kunal Kashyap 2022-04-18 04:19:49 GSoC: New & Improved Website for PgJDBC
Previous Message Amit Kapila 2022-04-18 04:01:45 Re: Logical replication timeout problem