Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o6A8u-0007Rs-QC for pgsql-general@arkaria.postgresql.org; Tue, 28 Jun 2022 12:16:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o6A8t-0005TC-Ex for pgsql-general@arkaria.postgresql.org; Tue, 28 Jun 2022 12:16:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o6A8s-0005T3-SO for pgsql-general@lists.postgresql.org; Tue, 28 Jun 2022 12:16:31 +0000 Received: from colo1.denninger.net ([104.236.120.189]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o6A8p-0003Zm-TB for pgsql-general@lists.postgresql.org; Tue, 28 Jun 2022 12:16:29 +0000 Received: from denninger.net (097-081-026-048.res.spectrum.com [97.81.26.48]) by colo1.denninger.net (Postfix) with ESMTP id 396BE2110B2 for ; Tue, 28 Jun 2022 08:16:26 -0400 (EDT) Received: from [192.168.10.25] (D15.Denninger.Net [192.168.10.25]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by denninger.net (Postfix) with ESMTPSA id DEEFC3F79F5 for ; Tue, 28 Jun 2022 08:16:25 -0400 (EDT) Message-ID: <2070cb4e-d819-c3e3-005f-99fa548bbe4b@denninger.net> Date: Tue, 28 Jun 2022 08:16:25 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.10.0 Subject: Re: Libpq question related to allocated resources Content-Language: en-US To: pgsql-general@lists.postgresql.org References: <5cb156e4-0c40-d66e-5fcd-94556f58bb2b@denninger.net> <2246456.1656386557@sss.pgh.pa.us> From: Karl Denninger In-Reply-To: <2246456.1656386557@sss.pgh.pa.us> Content-Type: multipart/signed; protocol="application/pkcs7-signature"; micalg=sha-512; boundary="------------ms060003020407070800030003" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a cryptographically signed message in MIME format. --------------ms060003020407070800030003 Content-Type: multipart/alternative; boundary="------------97oRt3GZxrZKznQ20LHrt007" --------------97oRt3GZxrZKznQ20LHrt007 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 6/27/2022 23:22, Tom Lane wrote: > Karl Denninger writes: >> But -- I still have a /lot /of memory out on the heap according to >> jemalloc stats that is not being deallocated, and what's worse is that >> if I rig the code to call PQfinish and then PQconnect once again I get >> /even more /imbalanced allocate/free counts (and the memory use in said >> buckets to go with them.) > Hmmm ... I'm not aware of any memory leaks in libpq, but that doesn't > mean there are none. Of course, if you're forgetting to PQclear() > some PGresults, that's not libpq's fault ;-). Well, yes, which is why I wrapped those calls to make very sure that's not the case (internal reference count in the code when in "debugging mode", basically) along with all the uses of escape/unescape (e.g. bytea fields.)  All come back clean on each "round" through which makes it quite puzzling. I'll do more digging.  I've got wrappers around all memory allocation in my development libraries that for internal allocations make quite sure that they're both properly paired and sentinels are on the "bookends" so if the code does smash something it catches it, and there's nothing being flagged; the arena, as my code sees it from what it allocated and the calls it made to libpq, are empty when it comes back as it should be. Obviously there's leakage somewhere but at this point I'm quite certain its not in my code itself (its certainly possible FastCGI's lib has a problem somewhere since it has to construct the environment from the web server's CGI call for each call to the application, each of those is distinct, and if something goes wrong there it will leak like crazy since each of those constructs is unique and then must be properly released when that call is complete.) >> The obvious question, given the warnings in the FastCGI library: Does >> libpq /modify /the process environment? > No. At least, I see no setenv() calls in it, and I think that it'd > be pretty unfriendly for a library to do that to its host application. In this case it would be fatal if that was to happen since the environment is synthetic and different on each call; if any part of the environment gets modified then the release by the caller will either leak or, possibly, result in a SEGV. >> A quick grep implies that indeed it may in >> backend/libpq/auth.c at least, > backend/libpq is unrelated to interfaces/libpq. (I've seen hints > that they arose from a common code base, but if so, that was a > few decades and a lot of rewrites ago.) > > regards, tom lane Gotcha.  It wasn't clear that this was or wasn't implicated and I'm digging for potential sources, thus the question. Thanks. -- Karl Denninger karl@denninger.net /The Market Ticker/ /[S/MIME encrypted email preferred]/ --------------97oRt3GZxrZKznQ20LHrt007 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit
On 6/27/2022 23:22, Tom Lane wrote:
Karl Denninger <karl@denninger.net> writes:
But -- I still have a /lot /of memory out on the heap according to 
jemalloc stats that is not being deallocated, and what's worse is that 
if I rig the code to call PQfinish and then PQconnect once again I get 
/even more /imbalanced allocate/free counts (and the memory use in said 
buckets to go with them.)
Hmmm ... I'm not aware of any memory leaks in libpq, but that doesn't
mean there are none.  Of course, if you're forgetting to PQclear()
some PGresults, that's not libpq's fault ;-).

Well, yes, which is why I wrapped those calls to make very sure that's not the case (internal reference count in the code when in "debugging mode", basically) along with all the uses of escape/unescape (e.g. bytea fields.)  All come back clean on each "round" through which makes it quite puzzling.

I'll do more digging.  I've got wrappers around all memory allocation in my development libraries that for internal allocations make quite sure that they're both properly paired and sentinels are on the "bookends" so if the code does smash something it catches it, and there's nothing being flagged; the arena, as my code sees it from what it allocated and the calls it made to libpq, are empty when it comes back as it should be.  Obviously there's leakage somewhere but at this point I'm quite certain its not in my code itself (its certainly possible FastCGI's lib has a problem somewhere since it has to construct the environment from the web server's CGI call for each call to the application, each of those is distinct, and if something goes wrong there it will leak like crazy since each of those constructs is unique and then must be properly released when that call is complete.)

The obvious question, given the warnings in the FastCGI library: Does 
libpq /modify /the process environment?
No.  At least, I see no setenv() calls in it, and I think that it'd
be pretty unfriendly for a library to do that to its host application.
In this case it would be fatal if that was to happen since the environment is synthetic and different on each call; if any part of the environment gets modified then the release by the caller will either leak or, possibly, result in a SEGV.
A quick grep implies that indeed it may in 
backend/libpq/auth.c at least,
backend/libpq is unrelated to interfaces/libpq.  (I've seen hints
that they arose from a common code base, but if so, that was a
few decades and a lot of rewrites ago.)

			regards, tom lane

Gotcha.  It wasn't clear that this was or wasn't implicated and I'm digging for potential sources, thus the question.

Thanks.


    
--
Karl Denninger
karl@denninger.net
The Market Ticker
[S/MIME encrypted email preferred]
--------------97oRt3GZxrZKznQ20LHrt007-- --------------ms060003020407070800030003 Content-Type: application/pkcs7-signature; name="smime.p7s" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="smime.p7s" Content-Description: S/MIME Cryptographic Signature MIAGCSqGSIb3DQEHAqCAMIACAQExDzANBglghkgBZQMEAgMFADCABgkqhkiG9w0BBwEAAKCC DdgwggagMIIEiKADAgECAhMA5EiKghDOXrvfxYxjITXYDdhIMA0GCSqGSIb3DQEBCwUAMIGL MQswCQYDVQQGEwJVUzEQMA4GA1UECAwHRmxvcmlkYTESMBAGA1UEBwwJTmljZXZpbGxlMRkw FwYDVQQKDBBDdWRhIFN5c3RlbXMgTExDMRgwFgYDVQQLDA9DdWRhIFN5c3RlbXMgQ0ExITAf BgNVBAMMGEN1ZGEgU3lzdGVtcyBMTEMgMjAxNyBDQTAeFw0xNzA4MTcxNjQyMTdaFw0yNzA4 MTUxNjQyMTdaMHsxCzAJBgNVBAYTAlVTMRAwDgYDVQQIDAdGbG9yaWRhMRkwFwYDVQQKDBBD dWRhIFN5c3RlbXMgTExDMRgwFgYDVQQLDA9DdWRhIFN5c3RlbXMgQ0ExJTAjBgNVBAMMHEN1 ZGEgU3lzdGVtcyBMTEMgMjAxNyBJbnQgQ0EwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIK AoICAQC1aJotNUI+W4jP7xQDO8L/b4XiF4Rss9O0B+3vMH7Njk85fZ052QhZpMVlpaaO+sCI KqG3oNEbuOHzJB/NDJFnqh7ijBwhdWutdsq23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