From 20465f9dbd4b1e453ac8cf156672645389f5ea59 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sun, 29 Mar 2026 23:05:30 +0100 Subject: [PATCH 1/2] Fix GH-21575: Memory leak with notices in pgsql persistent connections. --- ext/pgsql/pgsql.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c index 99f2e3cf563fe..0d271d5417ad3 100644 --- a/ext/pgsql/pgsql.c +++ b/ext/pgsql/pgsql.c @@ -172,6 +172,8 @@ static zend_function *pgsql_link_get_constructor(zend_object *object) { return NULL; } +static void _php_pgsql_notice_handler(void *l, const char *message); + static void pgsql_link_free(pgsql_link_handle *link) { PGresult *res; @@ -187,6 +189,12 @@ static void pgsql_link_free(pgsql_link_handle *link) zend_hash_del(&PGG(connections), link->hash); + if (link->persistent) { + /* Reset the notice processor context to NULL so that notices emitted + * during a subsequent PQreset on this persistent PGconn are safely + * ignored rather than written to a stale link handle. */ + PQsetNoticeProcessor(link->conn, _php_pgsql_notice_handler, NULL); + } link->conn = NULL; zend_string_release(link->hash); From f9129ebe8bcb88fbd192e69a80343906648b7915 Mon Sep 17 00:00:00 2001 From: David Carlier Date: Tue, 31 Mar 2026 07:00:35 +0100 Subject: [PATCH 2/2] attempt of a test --- ext/pgsql/tests/gh21575.phpt | 67 ++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 ext/pgsql/tests/gh21575.phpt diff --git a/ext/pgsql/tests/gh21575.phpt b/ext/pgsql/tests/gh21575.phpt new file mode 100644 index 0000000000000..b5cd4afb8188a --- /dev/null +++ b/ext/pgsql/tests/gh21575.phpt @@ -0,0 +1,67 @@ +--TEST-- +GH-21575 - Memory leak with notices in pgsql persistent connections. +--EXTENSIONS-- +pgsql +--SKIPIF-- + +--INI-- +pgsql.ignore_notice=0 +--FILE-- + +--CLEAN-- + +--EXPECTF-- +string(%d) "NOTICE: test notice" +bool(true) +string(%d) "NOTICE: test notice" +Done